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.
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 {
}
© 2022 - 2024 — McMap. All rights reserved.