Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Java API to generate Java source files [closed]

People also ask

What is JavaPoet?

JavaPoet is developed by Square, which provides APIs to generate Java source code. It can generate primitive types, reference types and their variants (such as classes, interfaces, enumerated types, anonymous inner classes), fields, methods, parameters, annotations, and Javadocs.

Where can I find Java source files?

A typical location is at the top of the source tree. Oracle Solaris, Linux, and macOS: For example, if the source files for the java. math package are contained in the /home/user/src/java/math directory, then you could create an overview comment file in /home/user/src/overview.

How do I create a Java file?

java File file = new File("JavaFile. java"); We then use the createNewFile() method of the File class to create new file to the specified path.

What do Java source files end with?

The Java compiler, javac, requires that the name of a Java source file end with a . java extension. If the source file contains a class or interface that is declared with the keyword public, the filename must be the name of that class or interface. There can be at most one such class or interface in a source file.


Sun provides an API called CodeModel for generating Java source files using an API. It's not the easiest thing to get information on, but it's there and it works extremely well.

The easiest way to get hold of it is as part of the JAXB 2 RI - the XJC schema-to-java generator uses CodeModel to generate its java source, and it's part of the XJC jars. You can use it just for the CodeModel.

Grab it from http://codemodel.java.net/


Solution found with CodeModel
Thanks, skaffman.

For example, with this code:

JCodeModel cm = new JCodeModel();
JDefinedClass dc = cm._class("foo.Bar");
JMethod m = dc.method(0, int.class, "foo");
m.body()._return(JExpr.lit(5));

File file = new File("./target/classes");
file.mkdirs();
cm.build(file);

I can get this output:

package foo;
public class Bar {
    int foo() {
        return  5;
    }
}

Solution found with Eclipse JDT's AST
Thanks, Giles.

For example, with this code:

AST ast = AST.newAST(AST.JLS3);
CompilationUnit cu = ast.newCompilationUnit();

PackageDeclaration p1 = ast.newPackageDeclaration();
p1.setName(ast.newSimpleName("foo"));
cu.setPackage(p1);

ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(new String[] { "java", "util", "Set" }));
cu.imports().add(id);

TypeDeclaration td = ast.newTypeDeclaration();
td.setName(ast.newSimpleName("Foo"));
TypeParameter tp = ast.newTypeParameter();
tp.setName(ast.newSimpleName("X"));
td.typeParameters().add(tp);
cu.types().add(td);

MethodDeclaration md = ast.newMethodDeclaration();
td.bodyDeclarations().add(md);

Block block = ast.newBlock();
md.setBody(block);

MethodInvocation mi = ast.newMethodInvocation();
mi.setName(ast.newSimpleName("x"));

ExpressionStatement e = ast.newExpressionStatement(mi);
block.statements().add(e);

System.out.println(cu);

I can get this output:

package foo;
import java.util.Set;
class Foo<X> {
  void MISSING(){
    x();
  }
}

You can use Roaster (https://github.com/forge/roaster) to do code generation.

Here is an example:

JavaClassSource source = Roaster.create(JavaClassSource.class);
source.setName("MyClass").setPublic();
source.addMethod().setName("testMethod").setPrivate().setBody("return null;")
           .setReturnType(String.class).addAnnotation(MyAnnotation.class);
System.out.println(source);

will display the following output:

public class MyClass {
   private String testMethod() {
       return null;
   }
}

Another alternative is Eclipse JDT's AST which is good if you need to rewrite arbitrary Java source code rather than just generate source code. (and I believe it can be used independently from eclipse).


The Eclipse JET project can be used to do source generation. I don't think it's API is exactly like the one you described, but every time I've heard of a project doing Java source generation they've used JET or a homegrown tool.


Don't know of a library, but a generic template engine might be all you need. There are a bunch of them, I personally have had good experience with FreeMarker