[hibernate-dev] Accessing annotation properties values

Łukasz Antoniak lukasz.antoniak at gmail.com
Thu Oct 4 12:01:25 EDT 2012


Hello Team,

In Envers code we use ReflactionManager and XProperty#getAnnotation(Class<T>) quite often. While working on metamodel branch, I 
have started replacing all those calls with JandexHelper#getValue(AnnotationInstance, String, Class<T>) method. The code became 
less type safe and grew in size. The only solution that comes to my mind is to create annotation proxy that directs calls to 
underlying AnnotationInstance. Something like:
public static <T> T createAnnotationProxy(final AnnotationInstance annotationInstance, final Class<T> annotation) {
	try {
		final ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setInterfaces( new Class[] { annotation.getClassLoader().loadClass( annotation.getName() ) } );
		final Class proxyClass = proxyFactory.createClass();
		final ProxyObject proxyObject = (ProxyObject) proxyClass.newInstance();
		proxyObject.setHandler( new MethodHandler() {
			@Override
			public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
				String executedMethodName = thisMethod.getName();
				if ( "toString".equals( executedMethodName ) ) {
					return proxyClass.getName() + "@" + System.identityHashCode( self );
				}
				return JandexHelper.getValue( annotationInstance, executedMethodName, Object.class );
			}
		} );
		return (T) proxyObject;
	}
	catch ( Exception e ) {
		throw new HibernateException( e );
	}
}

This allows to:
Audited audited = createAnnotationProxy( auditedAnnotationInstance, Audited.class );
ModificationStore modStore = audited.modStore();
RelationTargetAuditMode relationTarget = audited.targetAuditMode();

I have initially shown the above implementation to Hardy and we were wondering what you all think of this approach. Of course 
executing such method increases memory usage and decreases performance, but from design point of view, I can think of situations 
where it fits.

Regards,
Lukasz


More information about the hibernate-dev mailing list