How to force annotations to have an "id" field? - java

I have the following annotation:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface IdentifiableMethod {
String id() default "";
}
I will have to loop through a list of annotations and for each of them, perform a annotation.id().
Hence, I would have liked to use this "base" annotation to make it extended by other annotations:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface SpecificMethod extends IdentifiableMethod{
//invalid: annotation cannot have extends list
}
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface OtherSpecificMethod extends IdentifiableMethod{
//invalid: annotation cannot have extends list
}
... and then generically access the .id() method in a loop by getting in parameter a List<A extends IdentifiableMethod>, so that the compiler always makes me access that method.
However, I've just found out that in the Java specification, all Java annotations extend natively the interface Annotation and they cannot have an extends list [Source: this Stack Overflow question and its answers].
Is there any way to reach something similar?
Just to clarify the need, I need to get all the methods of all the classes within my package by reflection and scan for these annotations. They may be different (they may have more or less properties, different usages etc.), but they all need to have a String id field:
List<Class<?>> classes = getClasses(packageName);
for (Class<?> clazz : classes) {
for (Method method : clazz.getMethods()) {
for (Class<A> annotation : annotations) { //<-- annotations is a Collection<Class<A extends Annotation>>
if (method.isAnnotationPresent(annotation)) {
A targetAnnotation = method.getAnnotation(annotation);
String id = targetAnnotation.id(); //<-- this is not valid because A extends Annotation, not IdentifiableMethod
//rest of code (not relevant)
}
}
}
}
P.s. I already did this but I was looking for something cleaner:
String id = targetAnnotation.getClass().getMethod("id").invoke(targetAnnotation).toString();

Related

#Inherited annotation is not being inherited

I have a SubClass and a SuperClass, as well as an annotation DocAnnotation. I need a call to SubClass.foo() to get all class annotations from SubClass and SuperClass. The classes are defined like this:
SuperClass
package Code
import Annotations.DocAnnotation;
import java.util.Arrays;
#DocAnnotation("Super Annotation")
public class SuperClass {
public void foo() {
System.out.println(Arrays.toString(this.getClass().getAnnotations()));
System.out.println(Arrays.toString(this.getClass().getDeclaredAnnotations()));
}
}
SubClass
package Code;
import Annotations.DocAnnotation;
#DocAnnotation("Sub Annotation")
public class SubClass extends SuperClass{
}
DocAnnotation
package Annotations;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
#Inherited
#Retention(RetentionPolicy.RUNTIME)
#Repeatable(DocAnnotations.class)
public #interface DocAnnotation {
String value();
}
Running SubClass.foo I expect to see both "Super Annotation" and "Sub Annotation" but instead I see only [#Annotations.DocAnnotation(value=Sub Annotation)]. Am I misunderstanding what #inherited does, or am I doing something incorrectly?
Edit:
After adding the annotation #DocAnnotation("Super Annotation") to SubClass (that's the same one as in SuperClass) it actually shows up twice, once for its use in SubClass and once for its use in SuperClass! Now I'm nearly certain I'm misunderstanding something...
This seems to be the intended behavior, or at least it's specified to work this way (doc):
If [...] 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, since SubClass is already annotated with #DocAnnotation, the superclass is not queried.
On further inspection, the behavior seems a bit weird, though, especially after experimenting with presence of the containing annotation type. I came up with the following example which illustrates this (link):
import java.lang.annotation.*;
import java.util.*;
#Inherited
#Repeatable(Container.class)
#Retention(RetentionPolicy.RUNTIME)
#interface Ann {
String value();
}
#Inherited
#Retention(RetentionPolicy.RUNTIME)
#interface Container {
Ann[] value();
}
// Basic case. Result is that
// only #Ann("2") is present on
// ExhibitASub.
#Ann("1")
class ExhibitASuper {
}
#Ann("2")
class ExhibitASub extends ExhibitASuper {
}
// Because this case results in the
// #Container being present on ExhibitBSuper,
// rather than #Ann, all three annotations
// end up appearing on ExhibitBSub.
#Ann("1")
#Ann("2")
class ExhibitBSuper {
}
#Ann("3")
class ExhibitBSub extends ExhibitBSuper {
}
// Similar to the preceding case, by
// forcing the use of #Container, both
// annotations are present on ExhibitCSub.
#Container(#Ann("1"))
class ExhibitCSuper {
}
#Ann("2")
class ExhibitCSub extends ExhibitCSuper {
}
// Yet when we force both to use #Container,
// only #Container(#Ann("2")) is present on
// ExhibitDSub.
#Container(#Ann("1"))
class ExhibitDSuper {
}
#Container(#Ann("2"))
class ExhibitDSub extends ExhibitDSuper {
}
class Main {
public static void main(String[] args) {
for (Class<?> cls : Arrays.asList(ExhibitASub.class,
ExhibitBSub.class,
ExhibitCSub.class,
ExhibitDSub.class)) {
System.out.printf("%s:%n", cls);
for (Annotation ann : cls.getAnnotations()) {
System.out.printf(" %s%n", ann);
}
System.out.println();
}
}
}
The output of which is as follows:
class ExhibitASub:
#Ann(value=2)
class ExhibitBSub:
#Container(value=[#Ann(value=1), #Ann(value=2)])
#Ann(value=3)
class ExhibitCSub:
#Container(value=[#Ann(value=1)])
#Ann(value=2)
class ExhibitDSub:
#Container(value=[#Ann(value=2)])
Note that for B and C we see both the annotations on the superclass and subclass. Presumably this is because (in a strict sense) the annotation present on the superclass is of a different type than the annotation present on the subclass. Note that for D we return to only seeing the subclass annotation because both classes use the container type.
Using the containing annotation type explicitly could be a workaround for some cases, but it's not a general solution because of case D.
I might file a bug report for this tomorrow since this seems pretty undesirable. Since #Inherited predates #Repeatable, this behavior could be from previous versions where this situation couldn't occur.
You are getting this annotation wrong. The javadoc clearly states:
Indicates that an annotation type is automatically 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: if you query the subclass, then you would find the super class being annotated. But this thing is not meant for inheritance in the "OO sense". If you want to see both annotations, you have to write code that checks each class in the class inheritance tree manually.

Use #Parameters in TestNg on class level

The #Parameters annotation implementation from org.testng.annotations looks like this:
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
public #interface Parameters {
String[] value() default {};
}
So, it should allow me to use it on a ElementType.TYPE => it could also be used on a class.
When I use it on a method, I simply take the value using:
#Parameters("value")
public void m(String value) {
...
}
But if I use
#Parameters("value")
public class A {
...
}
how can I get the value inside the class?
If you want to use it for initialising class variables you can put in on constructor of class and use it.
ElementType.TYPE also means applicable to interfaces and enums - may be that one is specified if you want to extend the annotation.

AbstractProcessor, how to claim for annotations which target is another annotation?

Given this annotation:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.ANNOTATION_TYPE)
public #interface Interceptor {
Class<? extends Behaviour> value();
}
The users of my library can extend its API creating custom annotations annotated with #Interceptor, as follows:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
#Interceptor(BypassInterceptor.class)
public #interface Bypass {
}
AbstractProcessor provides a method called getSupportedAnnotationTypes which returns the names of the annotation types supported by the processor. But if I specify the name of #Interceptor, as follows:
#Override public Set<String> getSupportedAnnotationTypes() {
Set<String> annotations = new LinkedHashSet();
annotations.add(Interceptor.class.getCanonicalName());
return annotations;
}
The processor#process method will not be notified when a class is annotated with #Bypass annotation.
So, when using an AbstractProcessor, how to claim for annotations which target is another annotation?
If your annotation processor is scanning for all annotations that are meta-annotated with your annotation, you'll need to specify "*" for your supported annotation types, and then inspect each annotation's declaration (using ProcessingEnvironment.getElements() to determine whether it has the meta-annotation of interest.
You should use the #SupportedAnnotationTypes annotation on your processor, and not override the getSupportedAnnotationTypes() method, for example:
#SupportedAnnotationTypes({"com.test.Interceptor"})
public class AnnotationProcessor extends AbstractProcessor {
...
The Processor.getSupportedAnnotationTypes() method can construct its
result from the value of this annotation, as done by
AbstractProcessor.getSupportedAnnotationTypes().
Javadoc:
https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/SupportedAnnotationTypes.html

How to access an annotation inside another one using AbstractProcessor -not reflection

Given these types:
#Retention(RUNTIME)
#Target(ANNOTATION_TYPE)
public #interface Annotation1 {
}
#Retention(RUNTIME)
#Target(TYPE)
#Annotation1
public #interface Annotation2 {
}
#Annotation2
public class Mock {
}
I'm able to access the Annotation2 from Mock class using an AbstractProcessor, as follows:
Element element = //obtained from RoundEnvironment instance.
AnnotationMirror annotationMirror = element.getAnnotationMirrors().get(0);
But when I query for the annotations annotated in the pevious annotationMirror- which is a mirror of Annotation2, I got an empty list.
annotationMirror
.getAnnotationType()
.asElement()
.getAnnotationMirrors();
I think somehow this question is related to this one.
The code posted in the question works fine.
annotationMirror
.getAnnotationType()
.asElement()
.getAnnotationMirrors();
The problem was related with a missing import in the source code used for testing purpose.

Annotation member which holds other annotations?

I want to create a custom annotation (using Java) which would accept other annotations as parameter, something like:
public #interface ExclusiveOr {
Annotation[] value();
}
But this causes compiler error "invalid type for annotation member".
Object[] also doesn't work.
Is there a way to do what I want?
The error is produced because you can't use interfaces as annotation values (change it to Comparable and you'll get the same error). From the JLS:
It is a compile-time error if the return type of a method declared in an annotation type is any type other than one of the following: one of the primitive types, String, Class and any invocation of Class, an enum type, an annotation type, or an array of one of the preceding types. It is also a compile-time error if any method declared in an annotation type has a signature that is override-equivalent to that of any public or protected method declared in class Object or in the interface annotation.Annotation.
I'm afraid I don't know of a good workaround, but now at least you know why you get the error.
Depending on the reason why you would want to specify other annotations there are multiple solutions:
An array of instances of a single annotation type
Probably not what you meant in your question, but if you want to specify multiple instances of a single annotation type it's certainly possible:
public #interface Test {
SomeAnnotation[] value();
}
An array of annotation types instead of instances
If you do not need to specify any parameters on the individual annotations you can just user their class objects instead of instances.
public #interface Test {
Class<? extends Annotation>[] value();
}
But an enum would of course also do the trick in most situations.
Use multiple arrays
If the set of possible annotation types you want to use is limited, you can create a separate parameter for each one.
public #interface Test {
SomeAnnotation[] somes() default { };
ThisAnnotation[] thiss() default { };
ThatAnnotation[] thats() default { };
}
Giving a default value to each member makes it possible to only specify arrays for the types you need.
You can do:
Class<? extends Annotation>[] value();
Not sure if that helps, but . . .
I myself hereby propose a workaround for the given problem:
Well, what I wanted to make possible was something like that:
#Contract({
#ExclusiveOr({
#IsType(IAtomicType.class),
#Or({
#IsType(IListType.class),
#IsType(ISetType.class)
})
})
})
Proposed workaround:
Define a class with parameter-less constructor (which will be called by your own annotation processor later) in following way:
final class MyContract extends Contract{
// parameter-less ctor will be handeled by annotation processor
public MyContract(){
super(
new ExclusiveOr(
new IsType(IAtomicType.class),
new Or(
new IsType(IListType.class),
new IsType(ISetType.class)
)
)
);
}
}
usage:
#Contract(MyContract.class)
class MyClass{
// ...
}
I just ran into this exact problem, but (inspired by #ivan_ivanovich_ivanoff) I have discovered a way to specify a bundle of any combination of Annotations as an annotation member: use a prototype / template class.
In this example I define a WhereOr (i.e. a "where clause" for my model annotation) which I need to contain arbitrary Spring meta-annotations (like #Qualifier meta-annotations).
The minor (?) defect in this is the forced dereferencing that separates the implementation of the where clause with the concrete type that it describes.
#Target({})
#Retention(RetentionPolicy.RUNTIME)
public #interface WhereOr {
Class<?>[] value() default {};
}
#Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
#Retention(RetentionPolicy.RUNTIME)
public #interface JsonModel {
Class<?> value();
WhereOr where() default #WhereOr;
}
public class Prototypes {
#Qualifier("myContext")
#PreAuthorize("hasRole('ROLE_ADMINISTRATOR')")
public static class ExampleAnd {
}
}
#JsonModel(
value = MusicLibrary.class,
where = #WhereOr(Prototypes.ExampleAnd.class)
)
public interface JsonMusicLibrary {
#JsonIgnore
int getMajorVersion();
// ...
}
I will programmatically extract the possible valid configurations from the "where clause" annotation. In this case I also use the prototypes class as a logical AND grouping and the array of classes as the logical OR.

Categories

Resources