This is a demo extension to the extensible Java compiler ExtendJ.
This extension is a tool that prints an outline for input Java classes. This extension demonstrates JastAdd synthesized attributes, inter-type declarations, and collection attributes.
Cloning this Project
To clone this project you will need Git installed.
Use this command to clone the project using Git:
git clone --recursive email@example.com:extendj/outliner.git
--recursive flag makes Git also clone the ExtendJ submodule while cloning
If you forgot the
--recursive flag you can manually clone the ExtendJ
submodule using these commands:
cd outliner git submodule init git submodule update
This should download the ExtendJ Git repository into a local directory named
Build and Run
If you have Gradle installed you can issue the following commands to build and test the minimal extension:
gradle jar java -jar outliner.jar testfiles/Test.java
If you do not have Gradle installed you can use the
gradlew.bat (on Windows)
gradlew (Mac/Linux) script instead. For example to build on Windows run the
following in a command prompt:
gradlew scripts are wrapper scripts that will download Gradle locally and
How it Works
src/jastadd/ExtensionBase.jrag file contains all code for printing Java
CompilationUnit.process() method is called for each Java source file
parsed with no semantic errors. This inter-type declaration iterates over each
type declaration in each compilation unit and prints the outline.
TypeDecl.printOutline() method is the method used to print the outline
for a type declaration (i.e. a class, enum, interface etc.). It uses a collection attribute
methods() attribute is declared like this:
coll Collection<MethodDecl> TypeDecl.methods() [new LinkedList<>()] with add root TypeDecl; MethodDecl contributes this when isPublic() to TypeDecl.methods() for hostType();
The first line declares the attribute, giving the type for the collection, the
initialization expression (
new LinkedList<>()), the method used to update the
with add), and the root node for the collection (
The second line gives a contribution to the attribute for
nodes. Each method declaration contributes itself to the
attribute. The last part of the contributes statement gives an expression for
which node the contribution contributes to, in this case the node referred to
by the inherited
The architecture for ExtendJ extensions is documented in the Extension Base project.
Although the Gradle plugin can handle some automatic rebuilding when a source file changes,
it does not handle all cases well, so in some cases you will need to force Gradle to
rebuild your project. This can be done passing the
--rerun-tasks option to Gradle:
$ gradle --rerun-tasks
Changes to the
jastadd_modules file always require a rebuild to ensure that everything
is generated from the current sources.
More examples on how to build ExtendJ-like projects with the JastAdd Gradle plugin can be found here: