/** * Java annotations are defined as interfaces. Therefore in order to instantiate one, we must create a Proxy. */ public static class AnnotationProxy implements InvocationHandler { // // Private statics // private final org.jboss.forge.parser.java.Annotation annotationSource; private final Class annotationClass; // // Public statics // @SuppressWarnings("unchecked") public static T newInstance(org.jboss.forge.parser.java.Annotation annotationSource) { try { Class annotationClass = (Class) Class.forName("javax.persistence." + annotationSource.getName()); return (T) java.lang.reflect.Proxy.newProxyInstance( annotationSource.getClass().getClassLoader(), new Class[] { annotationClass }, new AnnotationProxy(annotationClass, annotationSource)); } catch (Exception e) { throw InspectorException.newException(e); } } // // Constructor // private AnnotationProxy(Class annotationClass, org.jboss.forge.parser.java.Annotation annotationSource) { this.annotationSource = annotationSource; this.annotationClass = annotationClass; } // // Public methods // @Override public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable { try { String methodName = method.getName(); // Get the value from the Forge Annotation class... String value = this.annotationSource.getStringValue(methodName); // ...if no value, return the default... java.lang.reflect.Method annotationMethod = this.annotationClass.getMethod(methodName); if ( value == null ) { return annotationMethod.getDefaultValue(); } // ...otherwise cast it to the correct class Class returnType = annotationMethod.getReturnType(); if (boolean.class.equals(returnType)) { return Boolean.valueOf(value); } else if (int.class.equals(returnType)) { return Integer.valueOf(value); } return value; } catch (Exception e) { throw InspectorException.newException(e); } } }