我的答案令人讨厌的部分是我无法扩展自定义注释(在继承的意义上)。那将简化 equals 方法。
First.java
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface First {
String name() default "";
String value() default "";
}
Second.java
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Second {
String name() default "";
String value() default "";
}
Thing1.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;
}
Example.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 持有者,因为这与“示例 input():”不匹配 完全强>!
有关此样式/解决方案的详细信息,请参阅此堆栈跟踪。
How to extend Java annotation?
这个问题已有 10 年历史,但没有按照 Roman 的要求回答该问题。 4k 观看次数,我希望这对某人有所帮助!!