Java 8 has feature called Type annotations (JSR 308). I would like to use it for simple Object to Object mapper framework. I would like define annotation @ExpectedType like this
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExpectedType {
public Class<?> value();
}
And then use it in my code like this:
public class SomeServiceImpl() {
public @ExpectedType(ObjectA_DTO.class) IObjectA doSomething(@ExpectedType(ObjectA_Entity.class) IObjectA obj) {
return (ObjectA_Entity) obj; // it's correct
}
}
IObjectA
is an interface implemented by classes ObjectA_DTO
and ObjectA_Entity
. The service I would like to use this way:
// it's correct
assert someService.doSomething(new ObjectA_DTO()).getClass() == ObjectA_DTO.class;
I would like change call of SomeServiceImpl methods to use Object mapper. It could be achieved by generated code using JSR 269 or by AOP.
The problem is I wrote simple annotations processor and it doesn't handle type annotations at all. The source of simple annotations processor looks like this:
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class SimpleAnnotationsProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Messager messager = processingEnv.getMessager();
try {
for (TypeElement e : annotations) {
messager.printMessage(Diagnostic.Kind.NOTE, e.toString());
for (Element elem : roundEnv.getElementsAnnotatedWith(e)) {
messager.printMessage(Diagnostic.Kind.NOTE, elem.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
Any ideas how to use or how to access type annotations by SimpleAnnotationsProcessor? Usage of Pluggable Annotation Processing API is not necessary for me I think it would has better performance than Java reflection. Anyway I don't know how to access type annotation via Java Reflection too.