Git Product home page Git Product logo

roaster's Introduction

Roaster

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

  • Download (or build from sources) the most recent roaster-distribution-VERSION.zip.

    1. Download JBoss Forge - 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

Usage

CLI

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

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", "id").setMutable(false);
javaClass.addProperty("String", "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;
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)
   {
      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);

Maven Artifacts

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>

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:

License

roaster's People

Contributors

gastaldi avatar mbenson avatar lincolnthree avatar vineetreynolds avatar wmedvede avatar sotty avatar jmini avatar adam-wyluda avatar woodcoder avatar danielsoro avatar davecahill avatar selrahal avatar myannou avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.