有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java元编程难题:获取所有本身由给定注释a注释的注释

您认为自己是java向导吗

您是否精通反射API的秘密

public @interface @a {}
public @interface @b {}
@Mark public @interface @c {}    
@Mark public @interface @d {}
public @interface @e {}

public Class C
{
    @a @b @c @d @e public void x();
}

public class Solver
{
    public Annotation[] solve(Method m, Class c);
}

您必须编写方法solve,以便在方法C.x()上调用它并标记。类返回{c,d}

(这不是家庭作业,而是我试图开发的框架元编程框架的真正编程任务)


共 (2) 个答案

  1. # 1 楼答案

    事实上,我不明白这有多么棘手

    更新时,忘记包含contains函数,并且在切换注释时出错。带有注释的getClass()。annotationType()。这个代码有效

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    @Mark
    public @interface A {}
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface B {}
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(value = ElementType.TYPE)
    public @interface Mark {}
    
    public class C {
    @A @B public void f() {}
    }
    
    public class Solver {
    public static boolean  contains(Annotation a, Class<?> targetAnnotation) {
        Class<?> c = a.annotationType();
        Annotation[] cas = c.getAnnotations();
        for (Annotation aa : cas) {
            if (aa.annotationType().equals(targetAnnotation)) {
                return true;
            }
        }
        return false;
    }
    
    public static Annotation[] getMarked(Method m) {
        List<Annotation> retVal = new ArrayList<Annotation>();
        for (Annotation a : m.getAnnotations()) {
            if (contains(a.getClass().getAnnotations(), Mark.class) {
                retVal.add(a);
            }
        }
        return retVal.toArray(new Annotation[]{});
    }
    
    public static void main(String[] args) throws SecurityException, NoSuchMethodException {
        Annotation[] result = getMarked(C.class.getMethod("f"));    
    }
    } // solver
    

    请注意,这要求所有注释都标记为运行时级别的保留,并且返回注释[]可能不是您想要的。您可能希望返回一个包含实际类型的类[](在我的示例中是a.Class)

  2. # 2 楼答案

    这是经过测试的工作。这确实比本来应该的更难

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface a{}
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface b{}
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
    public @interface Mark{}
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    @Mark
    public @interface c{}
    
    public static class D {
        @a @b @c
        public void x() {}
    }
    
    public static void main(String[] args) throws Exception {
        Method m = D.class.getMethod("x");
    
        Collection<Annotation> ret = new HashSet<Annotation>();
        Annotation[] annotations = m.getAnnotations();
        for (Annotation annotation : annotations) {
            Annotation subAnnots = annotation.annotationType().getAnnotation(Mark.class);
            if (subAnnots != null) {
                ret.add(annotation);
            }
        }
        System.out.println(ret);
    }
    

    我想这就引出了一个问题:为什么annotationType()有效,而getClass()无效