Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class Main {
private static final String PACKAGE_NAME = "xyz.ivankocijan.generated";
public static void main(String[] args) {
generateSuperHeroClass();
generateIronManClass();
generateSuperPowerInterface();
}
private static void generateSuperHeroClass() {
File sourcePath = new File("src");
//Specification for getName() method
MethodSpec getNameMethod = MethodSpec.methodBuilder("getName")
.returns(String.class)
.addModifiers(Modifier.ABSTRACT, Modifier.PROTECTED)
.build();
TypeSpec superHero = TypeSpec.classBuilder("SuperHero")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) //SuperHero class will be PUBLIC ABSTRACT
.addMethod(getNameMethod) //Add getName() method to SuperHero class
.addField(String.class, "name", Modifier.PROTECTED)
.build();
JavaFile superHeroFile = JavaFile.builder(PACKAGE_NAME, superHero)
.build();
try {
superHeroFile.writeTo(sourcePath);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void generateIronManClass() {
//Generated class will be located in "src" folder
File sourcePath = new File("src");
//getName() and getSuperPower() methods
MethodSpec getNameMethod = MethodSpec.methodBuilder("getName")
.returns(String.class)
.addAnnotation(Override.class)
.addStatement("return this.$N", "name")
.addModifiers(Modifier.PROTECTED)
.build();
MethodSpec getSuperPowerMethod = MethodSpec.methodBuilder("getSuperPower")
.returns(String.class)
.addAnnotation(Override.class)
.addStatement("return $S", "hasMoney")
.addModifiers(Modifier.PUBLIC)
.build();
//Constructor for IronManClass
MethodSpec constructor = MethodSpec.constructorBuilder()
.addParameter(String.class, "name")
.addStatement("this.$N = $N", "name", "name")
.addModifiers(Modifier.PUBLIC).build();
//Get name for superclass and interface
ClassName superClass = ClassName.get(PACKAGE_NAME, "SuperHero");
ClassName superPowerInterface = ClassName.get(PACKAGE_NAME, "SuperPower");
//Building IronMan class
TypeSpec ironMan = TypeSpec.classBuilder("IronMan")
.addModifiers(Modifier.PUBLIC)
.superclass(superClass)
.addMethod(constructor)
.addMethod(getNameMethod)
.addMethod(getSuperPowerMethod)
.addSuperinterface(superPowerInterface)
.build();
JavaFile ironManFile = JavaFile.builder(PACKAGE_NAME, ironMan)
.build();
try {
ironManFile.writeTo(sourcePath);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void generateSuperPowerInterface() {
File sourcePath = new File("src");
MethodSpec getSuperPowerMethod = MethodSpec.methodBuilder("getSuperPower")
.returns(String.class)
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.build();
TypeSpec superPowerInterface = TypeSpec.interfaceBuilder("SuperPower")
.addMethod(getSuperPowerMethod)
.addModifiers(Modifier.PUBLIC).build();
JavaFile superPowerFile = JavaFile.builder(PACKAGE_NAME, superPowerInterface)
.build();
try {
superPowerFile.writeTo(sourcePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment