Chris K
Chris K

Reputation: 12357

Unable to retrieve private annotation via reflection from aspectj advice

I have an annotation, defined as so:

public @interface RestletResourceVariable {
    String name(); 
}

I have a restlet ServerResource defined as so:

public class QuestionResource extends ServerResource {
    static Logger logger = LoggerFactory.getLogger(QuestionResource.class);

    @RestletResourceVariable(name="questionId")
    public String questionId; 

    @Get("json")
    public JsonRepresentation doGet() {
        logger.info("QuestionResource::doGet()");

        return new JsonRepresentation("{ nil };");
    }

    @Post
    public Representation doPost(Representation entity) throws Exception {
        logger.info("QuestionResource::doPost:" + entity.getText());
        return null;
    }
}

Here is my aspect:

public aspect RestletResourceVariableAspect { 
    static Logger logger = LoggerFactory.getLogger(RestletResourceVariableAspect.class); 

    pointcut RESTMethods() :
        execution( @org.restlet.resource.Delete * packagename..*(..) ) ||
        execution( @org.restlet.resource.Get    * packagename..*(..) ) || 
        execution( @org.restlet.resource.Post   * packagename..*(..) ) || 
        execution( @org.restlet.resource.Put    * packagename..*(..) ) 
        ;

    before(): RESTMethods() {
        Signature sig = thisJoinPoint.getSignature();

        Object target = thisJoinPoint.getTarget();
        Class<?> c = target.getClass();
        Class<?> cdecl = sig.getDeclaringType(); 

        logger.info("Class name: {} cdecl {}", c.getName(), cdecl.getName());
        logger.info("Sig: {} in class {}", sig.getName(),sig.getDeclaringType().getName());
        Field[] fields = cdecl.getDeclaredFields() ;
        for ( Field field : fields ) {
            logger.info("Field name: {}", field.getName());
            Annotation[] annotations = field.getDeclaredAnnotations();
            logger.info("Annotations {} length {}", annotations, Integer.toString(annotations.length));
            for(Annotation annotation : annotations){

                if(annotation instanceof RestletResourceVariable){
                    RestletResourceVariable myAnnotation = (RestletResourceVariable) annotation;
                    System.out.println("name: " + myAnnotation.name());
                }
            }

        }
    }
}

I get output like so:

16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Class name: QuestionResource cdecl QuestionResource
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Sig: doGet in class QuestionResource
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Field name: logger
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Annotations [] length 0
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Field name: questionId
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Annotations [] length 0
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Field name: ajc$tjp_0
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Annotations [] length 0
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Field name: ajc$tjp_1
16:22:11.656 [24864323@qtp-6011238-0] INFO  RestletResourceVariableAspect - Annotations [] length 0
16:22:11.656 [24864323@qtp-6011238-0] INFO  QuestionResource - QuestionResource::doGet()

Any guidance on what I'm doing wrong? I want to find the fields marked with the annotation so that I can populate them on request.

Upvotes: 2

Views: 542

Answers (1)

anjosc
anjosc

Reputation: 973

Make sure you add @Retention(RetentionPolicy.RUNTIME) to the annotation definition like this:

@Retention(RetentionPolicy.RUNTIME) 
@Target ({ElementType.FIELD, ElementType.METHOD })
@Inherited
public @interface RestletResourceVariable {
    String name(); 
}

Upvotes: 2

Related Questions