Using Javapoet, how to implement the following:
class A extends class B
class C implements Interface D
In the javadoc, it is mentioned how to create interfaces.
Use TypeSpec.Builder.superclass()
for extends, and TypeSpec.Builder.addSuperinterface()
for implements.
Suppose you want to generate a Dummy
class that extends Exception
class and implements the Serializable
interface. The generate code is:
...
TypeSpec typeSpec = TypeSpec.classBuilder("Dummy")
.addSuperinterface(Serializable.class)
.superclass(Exception.class)
.build();
JavaFile javaFile = JavaFile.builder("sample.javapoet", typeSpec).build();
...
And the generated code will be:
package sample.javapoet;
import java.io.Serializable;
import java.lang.Exception;
class Hoge extends Exception implements Serializable {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With