Friday 23 July 2021

Javapoet: Add java documentation

'addJavadoc' method is used to add java documentation.

 

Example

MethodSpec toStringSpec = MethodSpec.methodBuilder("connectionString").addModifiers(Modifier.PUBLIC)
            .addStatement("return jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;")
            .addJavadoc("This represent a {@link $T} to connect to the database", Resource.class)
            .returns(String.class).addAnnotation(AnnotationSpec.builder(Resource.class)
                    .addMember("name", "$S", "datasource").addMember("shareable", "false").build())
            .build();

 

Above snippet generates following code.

  /**
   * This represent a {@link Resource} to connect to the database
   */
  @Resource(
      name = "datasource",
      shareable = false
  )
  public String connectionString() {
    return jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;;
  }

 

Find the below working application.

 

JavaDocDemo.java

 

package com.sample.app;

import java.io.IOException;

import javax.annotation.Resource;
import javax.lang.model.element.Modifier;

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

public class JavaDocDemo {

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

    MethodSpec toStringSpec = MethodSpec.methodBuilder("connectionString").addModifiers(Modifier.PUBLIC)
        .addStatement("return jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;")
        .addJavadoc("This represent a {@link $T} to connect to the database", Resource.class)
        .returns(String.class).addAnnotation(AnnotationSpec.builder(Resource.class)
            .addMember("name", "$S", "datasource").addMember("shareable", "false").build())
        .build();

    String className = "HelloWorld";
    TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className);
    classBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
    classBuilder.addMethod(toStringSpec);
    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;
import javax.annotation.Resource;

public final class HelloWorld {
  /**
   * This represent a {@link Resource} to connect to the database
   */
  @Resource(
      name = "datasource",
      shareable = false
  )
  public String connectionString() {
    return jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;;
  }
}

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment