I have tried a lot of things online but nothing seem to work for me. I want to know whether an annotation method has been @Override
n (either with the same value as its default
).
Take a look at this example:
public class AnnoTest {
@Anno
private String something;
public static void main(String[] args) throws NoSuchFieldException, SecurityException, NoSuchMethodException {
Field field = AnnoTest.class.getDeclaredField("something");
field.setAccessible(true);
boolean isDefault= field.getAnnotation(Anno.class).annotationType().getDeclaredMethod("include").isDefault();
System.out.println(isDefault); //returns false
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface Anno {
boolean include() default false;
}
}
For some reason it returns false. When i change it to:
@Anno(include = false)
private String something;
It returns false
again. Is there a way to know whether the value has been declared in the annotation?
I know i could just compare the default value and its current value, but it will not work for me. I want to know if it has been declared.
With other words I need some kind of magic boolean that does the following:
@Anno
private String something;
return false
.
@Anno(include = true)
private String something;
return true
.
@Anno(include = false)
private String something;
return true
.
The reason of this is that i am wishing to add a method (to my annotation) named "parent". When a parent (a String) has been declared the annotation, this field will inherit the annotation of the field named parent. Take a look at this example:
public class AnnoTest {
@Anno(include = false)
private Something something = new Something();
@Anno(parent = "something")
private Something somethingElse = new Something();
public static void main(String[] args) throws NoSuchFieldException, SecurityException, NoSuchMethodException {
AnnoTest test = new AnnoTest();
Field somethingField = AnnoTest.class.getDeclaredField("something");
somethingField.setAccessible(true);
Field somethingElseField = AnnoTest.class.getDeclaredField("somethingElse");
somethingField.setAccessible(true);
Anno anno = somethingElseField.getAnnotation(Anno.class);
if (anno.parent().equals("something")) {
boolean include = somethingField.getAnnotation(Anno.class).include();
test.somethingElse.isIncluded = include;
}
//If not declared it will return true, which it should be false, because "something" field has it false.
boolean include = somethingElseField.getAnnotation(Anno.class).include();
//if somethingElse has declared "include", dominate the value, else keep it from the parent
test.somethingElse.isIncluded = include;
}
public class Something {
boolean isIncluded;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface Anno {
boolean include() default false;
String parent() default "";
}
}