The JLS states:
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;
#Target(ElementType.TYPE)
#Repeatable(FooContainer.class)
#interface Foo {}
#Target(ElementType.ANNOTATION_TYPE)
#interface FooContainer {
Foo[] value();
}
#Foo can appear on any class or interface declaration while #FooContainer can appear on only annotation interface declarations. Therefore, the following annotation interface declaration is legal:
#Foo #Foo
#interface Anno {}
while the following interface declaration is illegal:
#Foo #Foo
interface Intf {}
I don't understand why the latter one is illegal.
Because #Foo #Foo is actually #FooContainer({#Foo, #Foo}) and #FooContainer is only allowed on annotation types, not on interfaces (as declared by #Target(ElementType.ANNOTATION_TYPE)).
#Foo itself is only valid on types (#Target(ElementType.TYPE)), which includes annotation types. Annotations can always be used without their container annotation, but different targets might apply.
Related
I want to implement a custom validator in a Spring Boot v1.5.14.RELEASE app. First I create a custom constraint annotation:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
#Retention(RetentionPolicy.RUNTIME)
public interface CreateBook {
}
However, I receive this compilation error:
#Retention not applicable to type
The problem is with how you define a new annotation. It should be like this:
#Retention(RetentionPolicy.RUNTIME)
public #interface CreateBook {
}
Note the # character in #interface
In Java annotations marked as #Inherited will only work when annotating classes:
Note that this meta-annotation type has no effect if the annotated
type is used to annotate anything other than a class. Note also that
this meta-annotation only causes annotations to be inherited from
superclasses; annotations on implemented interfaces have no effect.
So interfaces or methods annotated with an #Inherited annotation will not result in implementing classes/methods to also be annotated with the annotation. The reason for this is most likely, that the compiler would'n know which of the annotations to choose, if there are multiple annotations in the class hierarchy as described here.
Now Java 8 introduced the new annotation #Repeatable. I think it would have been natural to remove the above restrictions for annotations that are both marked as #Inherited and #Repeatable, because the compiler should then be able to add the conflicting annotations to the #Repeatable annotation.
Given the following example:
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
#Inherited
#interface RepeatableAnnotations {
RepeatableAnnotation[] value();
}
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
#Inherited
#Repeatable(RepeatableAnnotations.class)
#interface RepeatableAnnotation {
String value();
}
#RepeatableAnnotation("A")
interface IntefaceA {}
#RepeatableAnnotation("B")
interface IntefaceB {}
#RepeatableAnnotation("C")
#RepeatableAnnotation("D")
public class TestClass implements IntefaceA, IntefaceB {
public static void main(String[] args) {
for (RepeatableAnnotation a : TestClass.class.getAnnotation(RepeatableAnnotations.class).value()) {
System.out.print(a.value());
}
}
}
I would have hoped the output to be ABCD but it is "just" CD (i.e. #Inherited is working exactly like pre Java 8).
Does anyone know if there was good reason for not removing the #Inherited restrictions regarding interfaces and methods in the case of #Repeatable annotations for Java 8?
Is there any workaround to achieve the ABCD output for the above type hierarchy? (other than using reflection to scan the super interfaces for annotations...)
Please recall the documentation of #Inherited:
If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type.
In other words, #Inherited never was intended to be a feature for collecting multiple annotations on a type hierarchy. Instead, you will get the annotation of the most specific type which has an explicit annotation.
In other words, if you change your declaration to
#RepeatableAnnotation("FOO") #RepeatableAnnotation("BAR") class Base {}
#RepeatableAnnotation("C") #RepeatableAnnotation("D")
public class TestClass extends Base implements IntefaceA, IntefaceB {
it won’t change the result; FOO and BAR of Base are not inherited by TestClass as it has the explicit annotation values C and D.
Expanding this to the interface hierarchy would be awkward due to the multiple inheritance and the fact that a super-interface may turn out to be a sub-interface of another super-interface so finding the most specific one is not trivial. This differs heavily from the linear search of the superclass hierarchy.
You may encounter the situation where multiple unrelated annotated interfaces exist but it’s not clear why this ambiguity should be resolved by joining them into one repeated annotation. This would not harmonize with the behavior in all other scenarios.
Note that the answer you have linked is a bit odd as it shows code using a method annotation but method annotations are never inherited, regardless of whether you specified #Inherited or not (an audit tool should generate a warning when you combine #Target(ElementType.METHOD) with #Inherited, imho). #Inherited is relevant for type annotations only.
but I found this thread searching for a similar solution. In the end I wrote this helper method:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface Labels
{
Label[] value();
}
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
#Repeatable(Labels.class)
public #interface Label
{
String value();
}
#Label("A")
class A
{
}
#Label("B")
class B extends A
{
}
#Test
void LabelStackTest()
{
var labels = ClassUtils.getAnnotatedLabels(B.class);
assertThat(labels).contains("A");
assertThat(labels).contains("B");
}
public static List<String> getAnnotatedLabels(Class<?> labeledClass)
{
var labels = new ArrayList<String>();
do
{
labels.addAll(Arrays.asList(labeledClass.getAnnotationsByType(Label.class))
.stream()
.map(labelAnnotations -> labelAnnotations.value())
.toList());
labeledClass = labeledClass.getSuperclass();
} while (labeledClass != Object.class);
return labels;
}
I'm trying to create an annotation which can accept multiple classes as input. Typical usage would be
#Prerequisites{FirstPrerequisite.class, SecondPrerequisite.class}
For this I can create an annotation as shown below
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
#Inherited
public #interface Prerequisites {
public Class<?>[] value();
}
I want to restrict the types that are allowed in the prerequisites annotation. If I give something like public Class<? extends Dependency>[] value(); It is working however, the problem is My FirstPrerequisite and SecondsPrerequisite may bot be extended from same type. I tried the following but none seemed to work, they are giving compilation errors.
public Class<? extends Dependency & TestCaseStep>[] value();
public Class<? extends Dependency , TestCaseStep>[] value();
public Class<T extends Dependency & TestCaseStep>[] value();
How to bound Generics to take inputs of two different types?
One option is to create a marker interface and have your classes implement the interface. You can then provide a bound with that interface type.
Another alternative is to simply move the constraint checking at runtime instead of at compile time. Have your annotation processor validate the Class arguments that were provided.
I am wanting to generically retrieve annotations based on where a provided is retrieving values. Either from a field or getters/setters. So, a provider needs to return information in a Field annotation. There are other annotations planned, so the implementations needs to be generic.
The method is exactly what is in the java.lang.Class class.
import java.lang.annotation.Annotation;
public interface Setter<Instance, Type>
{
<A extends Annotation> A getAnnotation(Class<A> annotationClass);
}
But, when I use the method..
final Field annotation = setter.getAnnotation(Field.class);
I get a complication error..
incompatible types
found : java.lang.annotation.Annotation
required: Field
This makes nearly no sense because Field is an Annotation!
Are there special considerations to give to annotations while implementing generics?
Why isn't this working?? It is exactly like the Java API.
Just for more information, here is the Field annotation.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.FIELD, ElementType.METHOD})
public #interface Field
{
/**
* The effective path of the local or result set value to load into the field.
*/
String value() default "";
}
import java.lang.annotation.ElementType.*;
import java.lang.annotation.Target;
#Target({TYPE, FIELD, METHOD})
public #interface Inter1 {
}
I've seen in other annotations like SuppressWarnings and Deprecated that they use an array of ElementType enum directly without using the enum like the above. But when I try to use it in one of my custom annotations, it gives me an error. I need to give it as ElementType.TYPE or ElementType.FIELD for it to work. What is wrong with this code?
you want to use that annotation this way:
import static java.lang.annotation.ElementType.*;
otherwise, this way:
#Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})