Thursday 22 July 2021

Javapoet: Define an abstract class

Using Abstract modifier, you can define an abstract class.

 

Example

String className = "Test";
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className);
classBuilder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
classBuilder.addMethod(mainMethod);
TypeSpec helloWorldTypeSpec = classBuilder.build();

 

Find the below working application.

 

AbstractClassDemo.java
package com.sample.app;

import java.io.IOException;

import javax.lang.model.element.Modifier;

import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

public class AbstractClassDemo {

    public static void main(String args[]) throws IOException {

        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("connect");
        methodBuilder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
        methodBuilder.returns(void.class);
        methodBuilder.addParameter(String[].class, "args");
        MethodSpec mainMethod = methodBuilder.build();

        String className = "Test";
        TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className);
        classBuilder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
        classBuilder.addMethod(mainMethod);
        TypeSpec helloWorldTypeSpec = classBuilder.build();

        String packageName = "com.sample.app";
        JavaFile javaFile = JavaFile.builder(packageName, helloWorldTypeSpec).build();

        javaFile.writeTo(System.out);
    }

}

 

Generated code

package com.sample.app;

import java.lang.String;

public abstract class Test {
  public abstract void connect(String[] args);
}

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment