[hibernate-dev] Accessing annotation properties values
Emmanuel Bernard
emmanuel at hibernate.org
Fri Oct 5 18:41:56 EDT 2012
Because the set of annotations is well defined, I won't if you could use an actual real implementation instead of a proxy. That's mor work but this will be much faster as we would not need to build the proxy.
Alternatively, I wonder if we could cache the serialized proxy somewhere or even build it at compile time and bundle it in the jar.
Emmanuel
On 4 oct. 2012, at 09:01, Łukasz Antoniak <lukasz.antoniak at gmail.com> wrote:
> 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
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev
More information about the hibernate-dev
mailing list