Reputation: 31
In the spoon code analysis tool, the AST is visualized in a GUI using the command:
$ java -cp spoon-core-5.9.0-jar-with-dependencies.jar spoon.Launcher -i MyClass.java --gui --noclasspath
I am trying to run the same command without the -gui but, I dont get any output. Is there anyway i can get the AST in a text file.
Upvotes: 0
Views: 742
Reputation: 396
You can use the JavaFX app called showMeYourSpoon
dedicated for visualising the Spoon AST of some Java code.
This tool, however, cannot take as input all the sources of a project (at max one class, for example your class MyClass.java
).
https://github.com/inspectorguidget/showMeYourSpoon
Using the application you can write or copy-paste your Java code in the dedicated text area and the corresponding Spoon AST will then be computed and displayed.
To export the Spoon AST in a text file, you can click on the save
button.
This application has other features such as:
Upvotes: -2
Reputation: 3344
Use Spoon processors. Any Spoon processor will visit all the elements of the AST in pre-order, so you could simply create a CtElement processor and print the element being visited:
@Override
public void process(CtElement element) {
//Find the level in the Syntax Tree of the element
int n = 0;
CtElement parent = element.getParent();
while (parent != null) {
n++;
parent = parent.getParent();
}
// Print the element
try {
String s = "";
if (n > 0) s = String.format("%0" + n + "d", 0).replace("0","-");
System.out.println(s + element.getClass().getSimpleName());
} catch (NullPointerException ex) {
System.out.println("Unknown Element");
}
}
Upvotes: 1