【问题标题】:Check if annotation is contained in list of annotations [duplicate]检查注释是否包含在注释列表中[重复]
【发布时间】:2014-05-12 15:00:15
【问题描述】:

我有一个注释列表,并想检查我的自定义注释“MyAnnotation”是否包含在此列表中:

List<java.lang.annotation.Annotation>

我的注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) 
public @interface MyAnnotation {

}

检查的最佳方法是什么。我是否必须对列表进行交互并比较名称,还是有什么方法可以让“contains()” - 列表的方法在这里工作?

【问题讨论】:

    标签: java collections annotations


    【解决方案1】:

    如果我正确地回答了您的问题,那么根据Annotation.equals(java.lang.Object) 上的文档,您应该可以使用列表中的contains() 方法。

    如果指定的对象表示一个在逻辑上等同于这个的注解,则返回真。换句话说,如果指定的对象是与此实例相同的注解类型的实例,则返回true,其所有成员都等于此注解的对应成员,如下定义:

    • 如果 x == y,则两个对应的原始类型成员的值为 x 和 y 被视为相等,除非它们的类型是浮点数或双精度数。
    • 如果 Float.valueOf(x).equals(Float.valueOf(y)),则两个对应的浮点成员的值为 x 和 y 被视为相等。 (与 == 运算符不同,NaN 被认为等于自身,0.0f 不等于 -0.0f。)
    • 如果 Double.valueOf(x).equals(Double.valueOf(y)),则两个对应的值为 x 和 y 的双精度成员被认为相等。 (与 == 运算符不同,NaN 被认为等于自身,0.0 不等于 -0.0。)
    • 如果 x.equals(y),则两个对应的 String、Class、enum 或 annotation 类型成员的值为 x 和 y 被视为相等。 (请注意,此定义对于注解类型的成员是递归的。)
    • 如果 Arrays.equals(x, y),则两个对应的数组类型成员 x 和 y 被视为相等,以用于 Arrays.equals(long[], long[]) 的适当重载。

    下面的演示 sn-p 及其输出似乎证明了这一点 -

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD) 
    @interface MyAnnotation {
        String value();
    }
    
    public class Main {
        @MyAnnotation(value = "onM1") public static void m1() { }
        @MyAnnotation(value = "onM1") public static void m() { }
        @MyAnnotation(value = "onM2") public static void m2() { }
        @MyAnnotation(value = "onM3") public static void m3() { }
    
    
        public static void main(String[] args) throws Exception {
            Method m = Main.class.getMethod("m1", new Class<?>[] {});
            Annotation onM1 = m.getAnnotation(MyAnnotation.class);
    
            m = Main.class.getMethod("m2", new Class<?>[] {});
            Annotation onM2 = m.getAnnotation(MyAnnotation.class);
    
            m = Main.class.getMethod("m3", new Class<?>[] {});
            Annotation onM3 = m.getAnnotation(MyAnnotation.class);
    
            m = Main.class.getMethod("m", new Class<?>[] {});
            Annotation onM = m.getAnnotation(MyAnnotation.class);
    
            List<Annotation> annots = Arrays.asList(onM1, onM2);
    
            System.out.println(annots);
            System.out.println(annots.contains(onM3));
            System.out.println(annots.contains(onM1));
            System.out.println(annots.contains(onM2));
            System.out.println(annots.contains(onM));
        }
    }
    

    输出

    错误的 真的 真的 真的

    【讨论】:

    • 谢谢,这是正确的,这个例子帮助我理解了我的问题
    【解决方案2】:
    boolean containsMyAnnotation = false;
    // For each annotation in the list...
    for(Annotation annotation : myListOfAnnotations) {
        // If they are MyAnnotation
        if(annotation.class == MyAnnotation.class) {
            // Then it exists in the list
            containsMyAnnotation = true; 
            // Break the for loop
            break;
        }
    }
    

    【讨论】:

    • 这不会编译。
    • 代替“if(annotation.class== MyAnnotation.class)”使用“if (annotation instanceof MyAnnotation)”
    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2022-10-02
    • 2018-10-04
    • 2010-10-15
    • 2014-11-12
    相关资源
    最近更新 更多