annotation-processing

Getting the qualified class name of generic type with Java 6 annotation processor

两盒软妹~` 提交于 2019-12-03 23:57:14
I am developing a small code generator using JDK 6's Annotation Processing API and am stuck trying to get the actual generic type of a field in the class. To be clearer, let's say I have a class like this: @MyAnnotation public class User { private String id; private String username; private String password; private Set<Role> roles = new HashSet<Role>(); private UserProfile profile; } and here is my annotation processor class: @SupportedAnnotationTypes({ "xxx.MyAnnotation" }) @SupportedSourceVersion(SourceVersion.RELEASE_6) public class MongoDocumentAnnotationProcessor extends AbstractProcessor

How to get method body from ExecutableElement

家住魔仙堡 提交于 2019-12-03 15:29:24
In my AbstractProcessor I am able to get all methods from a class annotated with some annotation, I have created: List<? extends Element> allElements = processingEnv.getElementUtils().getAllMembers((TypeElement) bean); List<ExecutableElement> methods = ElementFilter.methodsIn(allElements); Is is possible to get the body of the method/ ExecutableElement ? The API only seem to deal with the signature and modifiers. I could probably use some variation of this answer: https://stackoverflow.com/a/34568708/6095334 , to access classes from the proprietary *.sun.** package, such as com.sun.tools.javac

How to use custom type annotations in Java

筅森魡賤 提交于 2019-12-03 12:37:55
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

How can I refer to implementations of a method in annotation processing?

你说的曾经没有我的故事 提交于 2019-12-03 09:53:45
I am playing around with Java (javax) annotation processing. Suppose I have an annotation for methods: @Target(ElementType.METHOD) public @interface MethodAnnotation { } Now I want to process all the methods which are overridden from a type with the annotated method: interface MyInterface() { @MethodAnnotation void f() } class MyClass implements MyInterface { override void f() { } // <- I want to process this method } @Inherited meta-annotation seems not to be suitable here: Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class.

Forward compatible Java 6 annotation processor and SupportedSourceVersion

帅比萌擦擦* 提交于 2019-12-03 09:41:47
I am trying out Java 7 for one project and getting warnings from annotation processors (Bindgen and Hibernate JPA modelgen) of this sort: warning: Supported source version 'RELEASE_6' from annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' less than -source '1.7' This is caused by the @SupportedSourceVersion(SourceVersion.RELEASE_6) annotation on the annotation processor classes. Since they are compiled with Java 6, the highest value of SourceVersion available to them is RELEASE_6 . The Java 7 version of SourceVersion introduces RELEASE_7 . My questions: How are

Setting the generated source directory for annotation processors in Maven

倖福魔咒の 提交于 2019-12-03 07:55:36
I'm trying to move a build which generates sources using an annotation processor to Maven. I've tried configuring the maven-compiler-plugin as follows: <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <fork>true</fork> <compilerArgument>-s ${project.build.directory}/target/generated-sources/annotation-processing</compilerArgument> </configuration> </plugin> </plugins> But javac fails with [INFO] Compilation failure Failure executing javac, but could not parse the error: javac: invalid flag: -s /home/robert/workspaces

Gradle deprecated annotation processor warnings for lombok

不羁的心 提交于 2019-12-03 01:30:17
After upgrading to gradle 4.7, my previously warning-free build now emits this warning: The following annotation processors were detected on the compile classpath: 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor' and 'lombok.launch.AnnotationProcessorHider$ClaimingProcessor'. Detecting annotation processors on the compile classpath is deprecated and Gradle 5.0 will ignore them . Please add them to the annotation processor path instead. If you did not intend to use annotation processors, you can use the '-proc:none' compiler argument to ignore them. It seems that annotation

How to write automated unit tests for java annotation processor?

て烟熏妆下的殇ゞ 提交于 2019-12-02 17:32:25
I'm experimenting with java annotation processors. I'm able to write integration tests using the "JavaCompiler" (in fact I'm using "hickory" at the moment). I can run the compile process and analyse the output. The Problem: a single test runs for about half a second even without any code in my annotation processor. This is way too long to using it in TDD style. Mocking away the dependencies seems very hard for me (I would have to mock out the entire "javax.lang.model.element" package). Have someone succeed to write unit tests for an annotation processor (Java 6)? If not ... what would be your

How to create an instance out of a TypeMirror

岁酱吖の 提交于 2019-12-02 17:14:49
问题 I have an annotation that receives a "dynamic" parameter according to this idiom, i.e. a parameter of an interface type. In short: public interface MyInterface {} public @interface MyAnnotation { Class<? extends MyInterface> value(); } Now, to evaluate this parameter I need to create an instance of the provided implementation. The answer linked above does this at runtime. I am, however, writing a "real" (i.e. compile-time) annotation processor following this tutorial. When working with types

How to create an instance out of a TypeMirror

瘦欲@ 提交于 2019-12-02 10:40:47
I have an annotation that receives a "dynamic" parameter according to this idiom , i.e. a parameter of an interface type. In short: public interface MyInterface {} public @interface MyAnnotation { Class<? extends MyInterface> value(); } Now, to evaluate this parameter I need to create an instance of the provided implementation. The answer linked above does this at runtime. I am, however, writing a "real" (i.e. compile-time) annotation processor following this tutorial . When working with types you have to consider that they may not be compiled yet. The tutorial handles that (in that case to