Javapoet is a library to generate .java source files.
Where can I use source generation?
a. It is helpful while interacting with metadata files like database schemas, xsd, json schema etc.,
b. Helpful while doing annotation processing.
Dependencies used
<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
</dependency>
</dependencies>
You can download all the applications of this tutorial from below github link.
https://github.com/harikrishna553/java-libs/tree/master/javapoet-demo
- hello world application
- Add code to a method
- Add statements to the method
- Add control flow statements
- Generate method definitions with some configurations
- Generate if-else if ladder
- $T to refer types
- Define collections
- static imports
- $N: Refer another generated declaration by name
- Generate try-catch block
- $L: Emit a literal value in the output
- $s: emit a string
- Working with CodeBlock
- CodeBlock: Relative arguments
- CodeBlock: Positional Arguments
- CodeBlock: Named arguments
- Define an abstract class
- CodeBlock: Named arguments
- Define an abstract class
- Define a constructor
- add fields to the class
- Define parameters using ParameterSpec
- Add parameters using addParameter method
- Add generic parameters to a constructor
- Add generic parameters to a method
- Define interface
- Define enums
- Define fields to an enum constant
- enum: override super class methods of enum value
- Define Anonymous inner classes
- Add an annotation
- Set properties on annotations
- Add java documentation
- Define inner class
- Define nested class
- Add a field of type inner class
- Add static block
- Add initiaizer block
- Add a super class
- Add generic super class
- Add generic type to a class
- Add generic super class
- Add generic type to a class
- Define wildcard method parameters
- implement an interface
No comments:
Post a Comment