What is java-parser?
The java-parser npm package is a tool for parsing Java source code into an Abstract Syntax Tree (AST). This allows developers to analyze, transform, and manipulate Java code programmatically.
What are java-parser's main functionalities?
Parsing Java Code
This feature allows you to parse Java source code into an AST. The code sample demonstrates how to parse a simple Java class and print the resulting AST.
const javaParser = require('java-parser');
const javaCode = 'public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }';
const ast = javaParser.parse(javaCode);
console.log(JSON.stringify(ast, null, 2));
AST Traversal
This feature allows you to traverse the AST generated from Java source code. The code sample demonstrates a simple traversal that prints the type of each node in the AST.
const javaParser = require('java-parser');
const javaCode = 'public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }';
const ast = javaParser.parse(javaCode);
function traverse(node) {
console.log(node.type);
if (node.children) {
node.children.forEach(traverse);
}
}
traverse(ast);
Code Transformation
This feature allows you to transform the Java code by manipulating the AST. The code sample demonstrates how to rename a class in the parsed AST.
const javaParser = require('java-parser');
const javaCode = 'public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }';
const ast = javaParser.parse(javaCode);
// Example transformation: rename the class
ast.children[0].name = 'HelloUniverse';
console.log(JSON.stringify(ast, null, 2));
Other packages similar to java-parser
esprima
Esprima is a high-performance, standard-compliant ECMAScript parser. It parses JavaScript code into an AST, similar to how java-parser works for Java. Esprima is widely used for JavaScript code analysis and transformation.
antlr4
ANTLR (Another Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. ANTLR can be used to parse Java and many other languages, making it more versatile than java-parser, which is specific to Java.
Demo
Example Command Line:
~$ npm i -g java-parser
~$ echo '
class HelloWorld {
final int UNIVERSE = 42;
}
' > HelloWorld.java
~$ java-parser HelloWorld.java
{
"node": "CompilationUnit",
"types": [
{
"node": "TypeDeclaration",
"name": {
"identifier": "HelloWorld",
"node": "SimpleName"
},
"superInterfaceTypes": [],
"superclassType": null,
"bodyDeclarations": [
{
"node": "FieldDeclaration",
"fragments": [
{
"node": "VariableDeclarationFragment",
"name": {
"identifier": "UNIVERSE",
"node": "SimpleName"
},
"extraDimensions": 0,
"initializer": {
"node": "NumberLiteral",
"token": "42"
}
}
],
"type": {
"node": "PrimitiveType",
"primitiveTypeCode": "int"
},
"modifiers": [
{
"node": "Modifier",
"keyword": "final"
}
]
}
],
"typeParameters": [],
"interface": false,
"modifiers": []
}
],
"package": null,
"imports": []
}
Example Program:
~$ npm i java-parser
~$ echo '
var japa = require("java-parser");
console.log(japa.parse("package hello;"));
' | node
{ node: 'CompilationUnit',
types: [],
package:
{ node: 'PackageDeclaration',
name: { identifier: 'hello', node: 'SimpleName' },
annotations: [] },
imports: [] }