{"id":21154949,"url":"https://github.com/forge/roaster","last_synced_at":"2025-11-24T01:03:54.412Z","repository":{"id":7599945,"uuid":"8957185","full_name":"forge/roaster","owner":"forge","description":"A Java Parser library that allows easy parsing and formatting of Java source files","archived":false,"fork":false,"pushed_at":"2025-04-14T11:54:55.000Z","size":2056,"stargazers_count":683,"open_issues_count":17,"forks_count":105,"subscribers_count":45,"default_branch":"master","last_synced_at":"2025-04-28T13:59:30.098Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/forge.png","metadata":{"files":{"readme":"README.asciidoc","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-03-22T17:29:33.000Z","updated_at":"2025-04-14T11:54:58.000Z","dependencies_parsed_at":"2023-12-12T22:31:20.030Z","dependency_job_id":"1e975522-e1a4-42e0-a7a9-ff579e721eb2","html_url":"https://github.com/forge/roaster","commit_stats":{"total_commits":873,"total_committers":38,"mean_commits":"22.973684210526315","dds":0.4169530355097365,"last_synced_commit":"40e32ad43e1f2312a52fb7138b7e59333532bdf5"},"previous_names":["forge/java-parser"],"tags_count":89,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forge%2Froaster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forge%2Froaster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forge%2Froaster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forge%2Froaster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/forge","download_url":"https://codeload.github.com/forge/roaster/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254028798,"owners_count":22002277,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T11:08:31.667Z","updated_at":"2025-11-24T01:03:54.399Z","avatar_url":"https://github.com/forge.png","language":"Java","readme":"Roaster - The only Java source parser library you'll ever need\n===============================================================\n\n\nimage:https://img.shields.io/github/actions/workflow/status/forge/roaster/maven.yml?style=for-the-badge\u0026logo=github[\"Actions Status\", link=\"https://github.com/forge/roaster/actions\"]\nimage:http://img.shields.io/:license-EPL-blue.svg?style=for-the-badge\u0026logo=apache[\"License\", link=\"https://www.eclipse.org/legal/epl-v10.html\"]\nimage:https://img.shields.io/github/tag/forge/roaster?logo=apache-maven\u0026label=Maven%20Central\u0026style=for-the-badge[\"Maven Central\", link=\"https://search.maven.org/artifact/org.jboss.forge.roaster/roaster-jdt\"]\nimage:https://img.shields.io/badge/JVM-17-brightgreen.svg?style=for-the-badge\u0026logo=eclipseadoptium[\"Supported JVM Versions\"]\n\nRoaster (formerly known as java-parser) is a library that allows easy parsing and formatting of java source files. \nRoaster introduces a fluent interface to manipulate Java source files, like adding fields, methods, annotations and so on.\n\nInstallation\n============\n\n* If you are using Maven, add the following dependencies to your project: \n\n```xml\n\u003cproperties\u003e\n  \u003cversion.roaster\u003e2.30.3.Final\u003c/version.roaster\u003e\n\u003c/properties\u003e\n\n\u003cdependency\u003e\n  \u003cgroupId\u003eorg.jboss.forge.roaster\u003c/groupId\u003e\n  \u003cartifactId\u003eroaster-api\u003c/artifactId\u003e\n  \u003cversion\u003e${version.roaster}\u003c/version\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eorg.jboss.forge.roaster\u003c/groupId\u003e\n  \u003cartifactId\u003eroaster-jdt\u003c/artifactId\u003e\n  \u003cversion\u003e${version.roaster}\u003c/version\u003e\n  \u003cscope\u003eruntime\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\n* Otherwise, download and extract (or build from sources) the most recent http://search.maven.org/#search|ga|1|a%3A%22roaster-distribution%22[distribution] containing the Roaster distribution and command line tools\n\nUsage\n=====\n\nCLI\n---\nExecute roaster by running the following script (add these to your $PATH for convenience):\n\n[source]\n----\nbin/roaster     (Unix/Linux/OSX)\nbin/roaster.bat (Windows)\n----\n\nOptions described here:\n\n[source,cmd]\n----\n$ roaster -h\n\nUsage: roaster [OPTION]... FILES ... \nThe fastest way to build applications, share your software, and enjoy doing it. \n\n-c, --config [CONFIG_FILE]\n\t specify the path to the Eclipse code format profile (usually found at '$PROJECT/.settings/org.eclipse.jdt.core.prefs') \n\n-r, --recursive\n\t format files in found sub-directories recursively \n\nFILES... \n\t specify one or more space-separated files or directories to format \n\n-h, --help\n\t display this help and exit \n----\n\nJava Parser API\n---------------\n\nExample:\n```java\nRoaster.parse(JavaClassSource.class, \"public class HelloWorld {}\");\n```\n\nJava Source Code Generation API\n-------------------------------\n\nRoaster provides a fluent API to generate java classes. Here an example:\n\n```java\nfinal JavaClassSource javaClass = Roaster.create(JavaClassSource.class);\njavaClass.setPackage(\"com.company.example\").setName(\"Person\");\n\njavaClass.addInterface(Serializable.class);\njavaClass.addField()\n  .setName(\"serialVersionUID\")\n  .setType(\"long\")\n  .setLiteralInitializer(\"1L\")\n  .setPrivate()\n  .setStatic(true)\n  .setFinal(true);\n\njavaClass.addProperty(Integer.class, \"id\").setMutable(false);\njavaClass.addProperty(String.class, \"firstName\");\njavaClass.addProperty(\"String\", \"lastName\");\n\njavaClass.addMethod()\n  .setConstructor(true)\n  .setPublic()\n  .setBody(\"this.id = id;\")\n  .addParameter(Integer.class, \"id\");\n```\n\nWill produce:\n\n```java\npackage com.company.example;\n\nimport java.io.Serializable;\n\npublic class Person implements Serializable {\n\n   private static final long serialVersionUID = 1L;\n   private final Integer id;\n   private String firstName;\n   private String lastName;\n\n   public Integer getId() {\n      return id;\n   }\n\n   public String getFirstName() {\n      return firstName;\n   }\n\n   public void setFirstName(String firstName) {\n      this.firstName = firstName;\n   }\n\n   public String getLastName() {\n      return lastName;\n   }\n\n   public void setLastName(String lastName) {\n      this.lastName = lastName;\n   }\n\n   public Person(Integer id) {\n      this.id = id;\n   }\n}\n```\n\nJava Source Code Modification API\n---------------------------------\n\nOf course it is possible to mix both approaches (parser and writer) to modify Java code programmatically:\n\n```java\nJavaClassSource javaClass = \n  Roaster.parse(JavaClassSource.class, \"public class SomeClass {}\");\njavaClass.addMethod()\n  .setPublic()\n  .setStatic(true)\n  .setName(\"main\")\n  .setReturnTypeVoid()\n  .setBody(\"System.out.println(\\\"Hello World\\\");\")\n  .addParameter(\"java.lang.String[]\", \"args\");\nSystem.out.println(javaClass);\n```\n\nJavaDoc creation and parsing\n----------------------------\n \nHere is an example on how to add JavaDoc to a class:\n\n```java\nJavaClassSource javaClass = \n  Roaster.parse(JavaClassSource.class, \"public class SomeClass {}\");\nJavaDocSource javaDoc = javaClass.getJavaDoc();\n\njavaDoc.setFullText(\"Full class documentation\");\n// or \njavaDoc.setText(\"Class documentation text\");\njavaDoc.addTagValue(\"@author\",\"George Gastaldi\");\n\nSystem.out.println(javaClass);\n```\n\nFormatting the Java Source Code\n-------------------------------\n\nRoaster formats the Java Source Code by calling the `format()` method:\n\n```java\nString javaCode = \"public class MyClass{ private String field;}\";\nString formattedCode = Roaster.format(javaCode);\nSystem.out.println(formattedCode);\n```\n\nParsing the java unit \n----------------------\n\nThe 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:\n\n```java\nString javaCode = \"public class MyClass{ private String field;} public class AnotherClass {}\";\n\nJavaUnit unit = Roaster.parseUnit(javaCode);\n\nJavaClassSource myClass = unit.getGoverningType();\nJavaClassSource anotherClass = (JavaClassSource) unit.getTopLevelTypes().get(1);\n```\n\n\nValidate Code Snippets\n----------------------\n\nRoaster validates Java snippets and reports as Problem objects by calling the `validateSnippet()` method: \n\nExample:\n```java\nList\u003cProblem\u003e problem = Roaster.validateSnippet(\"public class HelloWorld {}\");\n// problem.size() == 0\n\nList\u003cProblem\u003e problem = Roaster.validateSnippet(\"public class MyClass {\");\n// problem.size() == 1 containing a new Problem(\"Syntax error, insert \\\"}\\\" to complete ClassBody\", 21, 21, 1)\n\n```\n\nBuilding from sources\n=====================\n\nJust run `mvn clean install` to build the sources\n\n\nIssue tracker\n=============\n\nFile an issue in our https://github.com/forge/roaster/issues[GitHub Issue Tracker]\n\n\nGet in touch\n============\n\nRoaster 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.\n\n* https://developer.jboss.org/en/forge[User forums]\n* https://developer.jboss.org/en/forge/dev[Developer forums]\n\n\nRelated / Similar projects\n==========================\n\nFor the writer part:\n\n* https://github.com/square/javapoet[square/javapoet]\n\n\nLicense\n=======\nhttp://www.eclipse.org/legal/epl-v10.html[Eclipse Public License - v 1.0]\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforge%2Froaster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforge%2Froaster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforge%2Froaster/lists"}