Roaster (formerly known as java-parser) is a library that allows easy parsing of java source files, introducing a fluent interface to manipulate Java source files, like adding fields, methods, annotations and so on.
-
Download (or build from sources) the most recent roaster-distribution-VERSION.zip.
-
Download JBoss Forge - Roaster (Latest)
-
This ZIP contains the Roaster distribution and command line tools
-
-
-
Extract the ZIP to a directory and navigate to roaster-distribution-VERSION/bin directory
Execute roaster by running the following script (add these to your $PATH for convenience):
bin/roaster (UNIX/linux/osx)
bin/roaster.bat (windows)
Options described here:
$ roaster -h
Usage: roaster [OPTION]... FILES ...
The fastest way to build applications, share your software, and enjoy doing it.
-c, --config [CONFIG_FILE]
specify the path to the Eclipse code format profile (usually found at '$PROJECT/.settings/org.eclipse.jdt.core.prefs')
-r, --recursive
format files in found sub-directories recursively
FILES...
specify one or more space-separated files or directories to format
-h, --help
display this help and exit
Roaster provides a fluent API to generate java classes. Here an example:
final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
javaClass.setPackage("com.company.example").setName("Person");
javaClass.addInterface(Serializable.class);
javaClass.addField()
.setName("serialVersionUID")
.setType("long")
.setLiteralInitializer("1L")
.setPrivate()
.setStatic(true)
.setFinal(true);
javaClass.addProperty("Integer", "id").setMutable(false);
javaClass.addProperty("String", "fistName");
javaClass.addProperty("String", "lastName");
javaClass.addMethod()
.setConstructor(true)
.setPublic()
.setBody("this.id = id;")
.addParameter(Integer.class, "id");
Will produce:
package com.company.example;
import java.io.Serializable;
import java.lang.Integer;
public class Person implements Serializable
{
private static final long serialVersionUID = 1L;
private final Integer id;
private String fistName;
private String lastName;
public Integer getId()
{
return id;
}
public String getFistName()
{
return fistName;
}
public void setFistName(String fistName)
{
this.fistName = fistName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public Person(Integer id)
{
this.id = id;
}
}
Of course it is possible to mix both approaches (parser and writer) to modify Java code programmatically:
JavaClassSource javaClass =
Roaster.parse(JavaClassSource.class, "public class SomeClass {}");
javaClass.addMethod()
.setPublic()
.setStatic(true)
.setName("main")
.setReturnTypeVoid()
.setBody("System.out.println(\"Hello World\");")
.addParameter("java.lang.String[]", "args");
System.out.println(javaClass);
Download the latest .jar or depend via Maven:
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-api</artifactId>
<version>${version.roaster}</version>
</dependency>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-jdt</artifactId>
<version>${version.roaster}</version>
</dependency>
ROASTER on JBossDeveloper. You might need to log in, in order to view the issues.
Roaster uses the same forum and mailing lists as the JBoss Forge project. See the JBoss Forge Community page.
For the writer part: