java - 无法通过 aspectj 建议的反射检索私有(private)注释

标签 java reflection annotations aspectj restlet

我有一个注释,定义如下:

public @interface RestletResourceVariable {
    String name(); 
}

我有一个这样定义的 reSTLet ServerResource:

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;
    }
}

这是我的方面:

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());
                }
            }

        }
    }
}

我得到这样的输出:

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()

关于我做错了什么的任何指导?我想找到标有注释的字段,以便我可以根据要求填充它们。

最佳答案

确保将 @Retention(RetentionPolicy.RUNTIME) 添加到注释定义中,如下所示:

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

关于java - 无法通过 aspectj 建议的反射检索私有(private)注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6375562/

相关文章:

c# - 我可以使用反射更改 C# 中的私有(private)只读继承字段吗?

java - 获取类的字段

java - 通过 JQuery 调用外部域上的 RESTful 服务

java - Java 中的可变长度(动态)数组

c# - 如何通过仅发送类的名称而不是类本身作为参数来获取类的类型?

java - hibernate注释和连接

java - JUnit @Test 预期的注释不起作用

java - java函数描述如何在eclipse代码建议中工作

java - Kotlin 检查参数的类型

java - 为什么减少循环次数并不能加快程序速度?