繁体   English   中英

如何检查注释的相等性?

[英]How to check equality of annotations?

你将如何实现这个方法:

public boolean equal(Annotation a1, Annotation a2) {
   ...
}

样本输入():

@First(name="1", value="1"), @Second(name="1", value="1")
@First(value="2"),           @First(name="2")
@First(value="3"),           @First(value="3")
@Second(name="4", value="4), @Second(name="4", value="4")

示例输出:

false
false
true
true

如您所见, equal的预期行为是明确的,类似于 java 中常规对象的标准equals方法的预期行为(问题是我们不能覆盖注释的equals )。

是否有任何库或标准实现?

覆盖是否等于 Annotation工作? 也许我不明白你的问题。

如果要检查a1和a2是否是相同的注释。 试试这个:a1.annotationType()。equals(a2.annotationType())

我的答案中令人讨厌的部分是我无法扩展自定义注释(在继承的意义上)。 这将简化 equals 方法。

首先.java

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface First  {
  String name() default "";
  String value() default "";
}

第二个.java

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Second {
    String name() default "";
    String value() default "";
}

东西1.java

public class Thing1 {
     @First(name = "1", value ="1")
     Object leftSideCase1;
     @Second(name = "1", value ="1")
     Object rightSideCase1;

     @First(value ="2")
     Object leftSideCase2;
     @First(name = "2")
     Object rightSideCase2;

     @First(value ="3")
     Object leftSideCase3;
     @First(value ="3")
     Object rightSideCase3;

     @First(name = "4", value ="4")
     Object leftSideCase4;
     @First(name = "4", value ="4")
     Object rightSideCase4;
}

例子.java

public class Example {


    public static void main(String[] args) throws NoSuchFieldException {
        String [][] fieldNameOfCases = {
                {"leftSideCase1","rightSideCase1"},
                {"leftSideCase2","rightSideCase2"},
                {"leftSideCase3","rightSideCase3"},
                {"leftSideCase4","rightSideCase4"},
        };

        // Loop through the list of field names, paired up by test case
        // Note: It's the construction of Thing1 that matches your question's
        // "sample input():"
        for(int index=0; index < fieldNameOfCases.length; index++) {
            Annotation leftSideAnnotation = getAnnotation(Thing1.class, fieldNameOfCases[index][0]);
            Annotation rightSideAnnotation = getAnnotation(Thing1.class, fieldNameOfCases[index][1]);
            System.out.println(equal(leftSideAnnotation, rightSideAnnotation));
        }
    }

    private static Annotation getAnnotation(Class<Thing1> thing1Class, String fieldName) throws NoSuchFieldException {
        Field classMemberField = Thing1.class.getDeclaredField(fieldName);
        Annotation[] annotations = classMemberField.getAnnotations();
        // ASSUME ONE ANNOTATION PER FIELD
        return annotations[0];
    }

    // This is my solution to the question
    public static boolean equal(Annotation a1, Annotation a2) {
       if(a1.getClass() != a2.getClass()) {
           return false;
       }
       if(a1 instanceof First) {
           if( ((First)a1).name().equals(((First)a2).name())  &&
               ((First)a1).value().equals(((First)a2).value()) ) {
               return true;
           }
           return false;
       }
       // Its annoying we can't leverage inheritance with the custom annotation
       // to remove duplicate code!! 
       if(a1 instanceof Second) {
            if( ((Second)a1).name().equals(((Second)a2).name())  &&
                ((Second)a1).value().equals(((Second)a2).value()) ) {
                return true;
            }
            return false;
        }
        return false;
    }
}

注意:我没有在 First 和 Second(自定义注释)中引入 NamePairValue 持有者,因为这与“示例输入():”不完全匹配!

有关此样式/解决方案的详细信息,请参阅此堆栈跟踪。 如何扩展Java注解?

这个问题已经有 10 年的历史了,但问题没有按照 Roman 的要求回答。 拥有4k 观看次数,我希望这对某人有所帮助!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM