If you are using Maven, add the following dependencies to your project:
<properties> <version.roaster>2.30.3.Final</version.roaster> </properties> <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> <scope>runtime</scope> </dependency>Otherwise, download and extract (or build from sources) the most recent distribution containing the Roaster distribution and command line tools
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 exitRoaster provides a fluent API to generate java classes. Here an example:
finalJavaClassSourcejavaClass = 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.class, "id").setMutable(false); javaClass.addProperty(String.class, "firstName"); javaClass.addProperty("String", "lastName"); javaClass.addMethod() .setConstructor(true) .setPublic() .setBody("this.id = id;") .addParameter(Integer.class, "id");Will produce:
packagecom.company.example; importjava.io.Serializable; publicclassPersonimplementsSerializable{privatestaticfinallongserialVersionUID = 1L; privatefinalIntegerid; privateStringfirstName; privateStringlastName; publicIntegergetId(){returnid} publicStringgetFirstName(){returnfirstName} publicvoidsetFirstName(StringfirstName){this.firstName = firstName} publicStringgetLastName(){returnlastName} publicvoidsetLastName(StringlastName){this.lastName = lastName} publicPerson(Integerid){this.id = id} }Of course it is possible to mix both approaches (parser and writer) to modify Java code programmatically:
JavaClassSourcejavaClass = 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);Here is an example on how to add JavaDoc to a class:
JavaClassSourcejavaClass = Roaster.parse(JavaClassSource.class, "public class SomeClass{}"); JavaDocSourcejavaDoc = javaClass.getJavaDoc(); javaDoc.setFullText("Full class documentation"); // orjavaDoc.setText("Class documentation text"); javaDoc.addTagValue("@author","George Gastaldi"); System.out.println(javaClass);Roaster formats the Java Source Code by calling the format() method:
StringjavaCode = "public class MyClass{private String field}"; StringformattedCode = Roaster.format(javaCode); System.out.println(formattedCode);The Java Language Specification allows you to define multiple classes in the same .java file. Roaster supports parsing the entire unit by calling the parseUnit() method:
StringjavaCode = "public class MyClass{private String field} public class AnotherClass{}"; JavaUnitunit = Roaster.parseUnit(javaCode); JavaClassSourcemyClass = unit.getGoverningType(); JavaClassSourceanotherClass = (JavaClassSource) unit.getTopLevelTypes().get(1);Roaster validates Java snippets and reports as Problem objects by calling the validateSnippet() method:
Example:
List<Problem> problem = Roaster.validateSnippet("public class HelloWorld{}"); // problem.size() == 0List<Problem> problem = Roaster.validateSnippet("public class MyClass{"); // problem.size() == 1 containing a new Problem("Syntax error, insert \"}\" to complete ClassBody", 21, 21, 1)Just run mvn clean install to build the sources
File an issue in our GitHub Issue Tracker
Roaster uses the same forum and mailing lists as the JBoss Forge project. See the JBoss Forge Community page.
For the writer part: