roaster

Project Url: forge/roaster
Introduction: A Java Parser library that allows easy parsing and formatting of Java source files
More: Author   ReportBugs   
Tags:

image:https://forge.ci.cloudbees.com/job/roaster/badge/icon["Build Status", link="https://forge.ci.cloudbees.com/job/roaster/"] image:http://img.shields.io/:license-EPL-blue.svg["License", link="https://www.eclipse.org/legal/epl-v10.html"] image:https://maven-badges.herokuapp.com/maven-central/org.jboss.forge.roaster/roaster-api/badge.svg["Maven Central", link="https://maven-badges.herokuapp.com/maven-central/org.jboss.forge.roaster/roaster-api"]

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.

Installation

  • If you are using Maven, add the following dependencies to your project:
<properties>
  <version.roaster>2.20.1.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>

Usage

CLI

Execute roaster by running the following script (add these to your $PATH for convenience):

[source]

bin/roaster (Unix/Linux/OSX)

bin/roaster.bat (Windows)

Options described here:

[source,cmd]

$ 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 

Java Parser API

Example:

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

package com.company.example;

import java.io.Serializable;

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) {
      this.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 {}");
javaClass.addMethod()
  .setPublic()
  .setStatic(true)
  .setName("main")
  .setReturnTypeVoid()
  .setBody("System.out.println(\"Hello World\");")
  .addParameter("java.lang.String[]", "args");
System.out.println(javaClass);

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");

System.out.println(javaClass);

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);
System.out.println(formattedCode);

Parsing the java unit

The link:http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.3[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);

Validate Code Snippets

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() == 0

List<Problem> problem = Roaster.validateSnippet("public class MyClass {");
// problem.size() == 1 containing a new Problem("Syntax error, insert \"}\" to complete ClassBody", 21, 21, 1)

Issue tracker

https://issues.jboss.org/browse/ROASTER[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 http://forge.jboss.org/[JBoss Forge] project. See the http://forge.jboss.org/community[JBoss Forge Community] page.

Related / Similar projects

For the writer part:

License

http://www.eclipse.org/legal/epl-v10.html[Eclipse Public License - v 1.0]

Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea