ObjectosCode
Java source code generation and annotation processing library.
Example
The following code:
PackageName examplesPackage = PackageName.named("br.com.objectos.code.examples");
ClassName helloWorld = examplesPackage.nestedClass("HelloWorld");
Identifier obj = id("obj");
MethodCode mainMethod = method(
_public(), _static(), _void(), id("main"),
param(a(cn(String.class)), id("args")),
_var(helloWorld, obj, _new(helloWorld)),
invoke(obj, "sayHello")
);
MethodCode sayHelloMethod = method(
_public(), _final(), _void(), id("sayHello"),
cn(System.class).id("out").invoke("println", invoke("helloMessage"))
);
MethodCode helloMessageMethod = method(
_private(), cn(String.class), id("helloMessage"),
_return(l("Hello world!"))
);
ClassCode helloWorldClass = _class(
_public(), _final(), id("HelloWorld"),
mainMethod,
sayHelloMethod,
helloMessageMethod
);
JavaFile helloWorldFile = javaFile(
examplesPackage,
helloWorldClass
);
System.out.println(helloWorldFile.toString());
produces the following output:
package br.com.objectos.code.examples;
public final class HelloWorld {
public static void main(String[] args) {
HelloWorld obj = new HelloWorld();
obj.sayHello();
}
public final void sayHello() {
System.out.println(helloMessage());
}
private String helloMessage() {
return "Hello world!";
}
}
Documentation
A proper documentation is on the way.
In the meantime you can browse some examples here.
Maven
objectos::code is at Maven Central.
<dependency>
<groupId>br.com.objectos.code</groupId>
<artifactId>code</artifactId>
<version>0.29.1</version>
</dependency>
License
Copyright (C) 2014-2020 Objectos Software LTDA
Licensed under the GNU Affero General Public License v3.
Notice
This library includes code from other open-source projects. See attached NOTICE.txt.