[
https://issues.jboss.org/browse/FORGE-369?page=com.atlassian.jira.plugin....
]
Richard Kennard updated FORGE-369:
----------------------------------
Description:
The existing org.jboss.forge.parser.java.Annotation API is a bit unsymmetrical:
public Annotation<O> getAnnotation(final Class<? extends
java.lang.annotation.Annotation> type);
You ask it for an Object that implements java.lang.annotation.Annotation, but you get back
an Object that implements org.jboss.forge.parser.java.Annotation. It would be nice if you
got back a 'real' annotation the same as java.lang.reflect.AnnotatedElement:
public <T extends java.lang.annotation.Annotation> T getAnnotation(final
Class<T> type);
This is a little tricky because java.lang.annotation.Annotation is an interface. If it
helps, I am using the attached rough code for now.
was:
The existing org.jboss.forge.parser.java.Annotation API is a bit unsymmetrical:
public Annotation<O> getAnnotation(final Class<? extends
java.lang.annotation.Annotation> type);
You ask it for an Object that implements java.lang.annotation.Annotation, but you get back
an Object that implements org.jboss.forge.parser.java.Annotation. It would be nice if you
got back a 'real' annotation the same as java.lang.reflect.AnnotatedElement:
public <T extends java.lang.annotation.Annotation> T getAnnotation(final
Class<T> type);
This is a little tricky because java.lang.annotation.Annotation is an interface. If it
helps, I am using this rough code for now:
/**
* Java annotations are defined as interfaces. Therefore in order to instantiate one,
we must create a Proxy.
*/
public static class AnnotationProxy<T extends Annotation> implements
InvocationHandler
{
//
// Private statics
//
private final org.jboss.forge.parser.java.Annotation<?> annotationSource;
private final Class<T> annotationClass;
//
// Public statics
//
@SuppressWarnings("unchecked")
public static <T extends Annotation> T
newInstance(org.jboss.forge.parser.java.Annotation<?> annotationSource)
{
try
{
Class<T> annotationClass = (Class<T>)
Class.forName("javax.persistence." + annotationSource.getName());
return (T) java.lang.reflect.Proxy.newProxyInstance(
annotationSource.getClass().getClassLoader(),
new Class[] { annotationClass },
new AnnotationProxy<T>(annotationClass, annotationSource));
}
catch (Exception e)
{
throw InspectorException.newException(e);
}
}
//
// Constructor
//
private AnnotationProxy(Class<T> 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);
}
}
}
Return real annotations from
org.jboss.forge.parser.java.AnnotationTarget.getAnnotation
---------------------------------------------------------------------------------------
Key: FORGE-369
URL:
https://issues.jboss.org/browse/FORGE-369
Project: Forge
Issue Type: Enhancement
Reporter: Richard Kennard
Assignee: Lincoln Baxter III
Attachments: AnnotationProxy.java
The existing org.jboss.forge.parser.java.Annotation API is a bit unsymmetrical:
public Annotation<O> getAnnotation(final Class<? extends
java.lang.annotation.Annotation> type);
You ask it for an Object that implements java.lang.annotation.Annotation, but you get
back an Object that implements org.jboss.forge.parser.java.Annotation. It would be nice if
you got back a 'real' annotation the same as java.lang.reflect.AnnotatedElement:
public <T extends java.lang.annotation.Annotation> T getAnnotation(final
Class<T> type);
This is a little tricky because java.lang.annotation.Annotation is an interface. If it
helps, I am using the attached rough code for now.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.jboss.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see:
http://www.atlassian.com/software/jira