sruthi ganesh
sruthi ganesh

Reputation: 31

AST of spoon in text file

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

Answers (2)

ARno
ARno

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:

  • showing/masking implicit elements of the AST
  • clicking on a Java code element automatically selects the corresponding AST element in the tree view
  • clicking on an AST element in the tree view selects the corresponding Java code chunk in the text area
  • specifying the kind of Java code you write (a class, a class element, a statement, or an expression)

Screenshot of showMeYourSpoon

Upvotes: -2

El Marce
El Marce

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

Related Questions