GitHub user zregvart opened a pull request: https://github.com/apache/camel/pull/1594
CAMEL-11087 Create a Annotation processor that creates REST DSL source code from Swagger specification **This crazy implementation submitted for review, thanks for looking at it ð¥** I would specifically like to know: - is there a need for this (I have a Maven plugin in mind also)? - is this too convoluted and too hacky? - as usual any other suggestions on code style/implementation choices This is an implementation of the Annotation processor to generate REST DSL RouteBuilder source code from Swagger specification. The processor is triggered by the presence of @SwaggerRestDsl annotation: @SwaggerRestDsl(className = "MyRestRoute", packageName = "com.example", specificationUri = "specification.json" destinationGenerator = CustomDestinationGenerator.class) This would generate source code for `com.example.MyRestRoute` from `specification.json` using `CustomDestinationGenerator` to generate `to` route endpoint. `@SwaggerRestDsl` annotation can be placed on a Java type (`ElementType.TYPE`), and it is not present in the compiled class (`RetentionPolicy.SOURCE`). Annotation processors do not _in general_ access resources outside Java type system, this can be done but not with 100% reliability, most notably when used in IDEs accessing resources can fail. This is mostly due to a fact that `Filer::getResource`[1] does not mandate the support for `StandardLocation.SOURCE_PATH`[2] location. So this implementation tries to search for the specified `specificationUri`. There is no support for remote (i.e. http) `specificationUri`s. The use of `destinationGenerator` is also very contrary to annotation processor architecture. Accessing `Class` objects that are being compiled is not possible as one has access to a partial AST (with com.sun.* full AST) through `javax.lang.model.element` and `javax.lang.model.type` packages. So if the `destinationGenerator` is not present on the output (usually target/classes with Maven), the implementation locates the source code for the DestinationGenerator and spawns another compiler to output the compiled class into a temp directory. The classpath needed for this step is computed by enumerating all elements within `StandardLocation.CLASS_PATH` and computing paths to either JAR files or directories (`bin`, and directories containing the word `classes` are picked). If the class is found compiled within output directory (incremental compile) or class has been compiled into the temp directory (clean compile) it can be loaded and instantiated and used in REST DSL generation. [1] https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Filer.html#getResource-javax.tools.JavaFileManager.Location-java.lang.CharSequence-java.lang.CharSequence- [2] https://docs.oracle.com/javase/8/docs/api/javax/tools/StandardLocation.html#SOURCE_PATH You can merge this pull request into a Git repository by running: $ git pull https://github.com/zregvart/camel CAMEL-11087 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/camel/pull/1594.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #1594 ---- commit 58b77c2ffbf9a36f3d43ccdde2e13ae79eafde90 Author: Zoran Regvart <zregv...@apache.org> Date: 2017-04-03T19:47:45Z CAMEL-11087 Create a Annotation processor that ... ...creates REST DSL source code from Swagger specification This is an implementation of the Annotation processor to generate REST DSL RouteBuilder source code from Swagger specification. The processor is triggered by the presence of @SwaggerRestDsl annotation: @SwaggerRestDsl(className = "MyRestRoute", packageName = "com.example", specificationUri = "specification.json" destinationGenerator = CustomDestinationGenerator.class) This would generate source code for `com.example.MyRestRoute` from `specification.json` using `CustomDestinationGenerator` to generate `to` route endpoint. `@SwaggerRestDsl` annotation can be placed on a Java type (`ElementType.TYPE`), and it is not present in the compiled class (`RetentionPolicy.SOURCE`). Annotation processors do not _in general_ access resources outside Java type system, this can be done but not with 100% reliability, most notably when used in IDEs accessing resources can fail. This is mostly due to a fact that `Filer::getResource`[1] does not mandate the support for `StandardLocation.SOURCE_PATH`[2] location. So this implementation tries to search for the specified `specificationUri`. There is no support for remote (i.e. http) `specificationUri`s. The use of `destinationGenerator` is also very contrary to annotation processor architecture. Accessing `Class` objects that are being compiled is not possible as one has access to a partial AST (with com.sun.* full AST) through `javax.lang.model.element` and `javax.lang.model.type` packages. So if the `destinationGenerator` is not present on the output (usually target/classes with Maven), the implementation locates the source code for the DestinationGenerator and spawns another compiler to output the compiled class into a temp directory. The classpath needed for this step is computed by enumerating all elements within `StandardLocation.CLASS_PATH` and computing paths to either JAR files or directories (`bin`, and directories containing the word `classes` are picked). If the class is found compiled within output directory (incremental compile) or class has been compiled into the temp directory (clean compile) it can be loaded and instantiated and used in REST DSL generation. [1] https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Filer.html#getResource-javax.tools.JavaFileManager.Location-java.lang.CharSequence-java.lang.CharSequence- [2] https://docs.oracle.com/javase/8/docs/api/javax/tools/StandardLocation.html#SOURCE_PATH ---- --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---