Search code examples
javainria-spoon

Convert Spoon model into something else


I am trying to convert a Spoon representation of a Java code into something else ; let say a JSON or XML representation or into other Java model.

I am not sure how and where to start.

  • Do I have to write a processor for each leaf of Spoon's metamodel (which is a lot of processors to write)?
  • Do I have to write a processor that analyse each Class and has a top/down approach (which will contain a lot of instanceof checks)?
  • Do I have to write a Scanner that is based on a tree-based representation?

Last question: when converting an element (let say a CtIf) how identifying its parent (a CtBlock) in a unique way?

Let say that it will output the following:

[
  {
    type: block,
    id: ???,
    ...,
  },
  ...,
  {
    type: if,
    parent: ???, (the block above)
    ...,
  }
]

I am a bit lost and need some help :) Thanks!


Solution

  • The easiest way to do that is to implement a custom pretty-printer (look on https://github.com/INRIA/spoon/blob/master/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java) and to pass it to a Launcher by overriding it.