Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A Java Parser library that allows easy parsing and formatting of Java source files
Java Other

Merge pull request #41 from gastaldi/ROASTER-79

ROASTER-79: Annotation.getString/LiteralValue should return FQN when element type is a class
latest commit 307032de4b
@gastaldi gastaldi authored



Build Status

Roaster (formerly known as java-parser) is a library that allows easy parsing and formatting of java source files. Roaster introduces a fluent interface to manipulate Java source files, like adding fields, methods, annotations and so on.


  • Download (or build from sources) the most recent

    1. Download Roaster (Latest)

      1. 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

	 specify one or more space-separated files or directories to format

-h, --help
	 display this help and exit

Java Parser API


Roaster.parse(JavaClassSource.class, "public class HelloWorld {}");

Java Source Code Generation API

Roaster provides a fluent API to generate java classes. Here an example:

final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);


javaClass.addProperty("Integer", "id").setMutable(false);
javaClass.addProperty("String", "firstName");
javaClass.addProperty("String", "lastName");

  .setBody(" = id;")
  .addParameter(Integer.class, "id");

Will produce:


import java.lang.Integer;

public class Person implements Serializable {

   private static final long serialVersionUID = 1L;
   private final Integer id;
   private String firstName;
   private String lastName;

   public Integer getId() {
      return id;

   public String getFirstName() {
      return firstName;

   public void setFirstName(String firstName) {
      this.firstName = firstName;

   public String getLastName() {
      return lastName;

   public void setLastName(String lastName) {
      this.lastName = lastName;

   public Person(Integer id) { = id;

Java Source Code Modification API

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 {}");
  .setBody("System.out.println(\"Hello World\");")
  .addParameter("java.lang.String[]", "args");

JavaDoc creation and parsing

Here is an example on how to add JavaDoc to a class:

JavaClassSource javaClass =
  Roaster.parse(JavaClassSource.class, "public class SomeClass {}");
JavaDocSource javaDoc = javaClass.getJavaDoc();

javaDoc.setFullText("Full class documentation");
// or
javaDoc.setText("Class documentation text");
javaDoc.addTagValue("@author","George Gastaldi");


Formatting the Java Source Code

Roaster formats the Java Source Code by calling the format() method:

String javaCode = "public class MyClass{ private String field;}";
String formattedCode = Roaster.format(javaCode);

Parsing the java unit

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:

String javaCode = "public class MyClass{ private String field;} public class AnotherClass {}";

JavaUnit unit = Roaster.parseUnit(javaCode);

JavaClassSource myClass = unit.getGoverningType();
JavaClassSource anotherClass = (JavaClassSource) unit.getTopLevelTypes().get(1);

Maven Artifacts

Download the latest .jar or depend via Maven:


Issue tracker

ROASTER on JBossDeveloper. You might need to log in, in order to view the issues.

Get in touch

Roaster uses the same forum and mailing lists as the JBoss Forge project. See the JBoss Forge Community page.

Related / Similar projects

For the writer part:


Something went wrong with that request. Please try again.