Metadata-Version: 2.2
Name: java-ast
Version: 0.0.1
Summary: jAST: Analyzing and Modifying Java ASTs with Python
Author-email: Marius Smytzek <marius.smytzek@cispa.de>, Martin Eberlein <ebermart@informatik.hu-berlin.de>
Project-URL: Homepage, https://github.com/smythi93/jast
Project-URL: Bug Tracker, https://github.com/smythi93/jast/issues
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3.10
Classifier: License :: OSI Approved :: MIT License
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: antlr4-python3-runtime>=4.13.2
Provides-Extra: test
Requires-Dist: pytest>=8.3.4; extra == "test"
Requires-Dist: pytest-cov>=6.0.0; extra == "test"
Requires-Dist: pytest-html>=4.1.1; extra == "test"

# jAST: Analyzing and Modifying Java ASTs with Python

The `jast` module helps Python applications to process trees of the Java
abstract syntax grammar.

An abstract syntax tree can be generated by using the `parse()`
function from this module.  The result will be a tree of objects whose
classes all inherit from `jast.JAST`.

A modified abstract syntax tree can be written back to Java source code
by using the `unparse()` function.  This function takes a tree of objects
and returns a string with the Java source code.

Additionally various helper functions are provided that make working with
the trees simpler. The main intention of the helper functions and this
module in general is to provide an easy-to-use interface for libraries
that work tightly with Java.

## Installation

The `jast` module can be installed from the Python Package Index (PyPI)
by running the following command:

```bash
pip install jast
```

Depending on your system configuration you might need to use `pip3`:

```bash
pip3 install jast
```

## Usage

`jast` provides a simple interface to parse Java source code and work with
the resulting abstract syntax tree. 

### Parsing Java Source Code

The following example demonstrates how
to parse a Java source file and print the names of all classes:

```python
import jast

# Parse the Java source file
with open("HelloWorld.java") as file:
    tree = jast.parse(file.read())
```

The `tree` object is now a tree of objects that represent the Java source as an abstract syntax tree. 

### Visiting Nodes
The following code snippet demonstrates how to print the names of all classes in the tree:

```python
# Print the names of all classes
class NameVisitor(jast.JNodeVisitor):
    def visit_Identifier(self, node):
        print(node.name)

visitor = NameVisitor()
visitor.visit(tree)
```

The `NameVisitor` class is a simple visitor that prints the name of all `Identifier` nodes in the tree.
The `visit()` method is called with the root node of the tree to start the traversal.

### Modifying Nodes

The following code snippet demonstrates how to modify the tree:

```python
# Modify the tree
class NameModifier(jast.JNodeTransformer):
    def visit_Identifier(self, node):
        if node.name == "HelloWorld":
            node.name = "HelloWorld2"
        return node
       
modifier = NameModifier()
tree = modifier.visit(tree)
```

The `NameModifier` class is a simple transformer that changes the name of the class `HelloWorld` to `HelloWorld2`.
The `visit()` method is called with the root node of the tree to start the transformation.

### Writing Java Source Code

The following code snippet demonstrates how to write the modified tree back to Java source code:

```python
# Write the modified tree back to Java source code
with open("HelloWorld2.java", "w") as file:
    file.write(jast.unparse(tree))
```

The `unparse()` function takes the modified tree and returns a string with the Java source code.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
