Ondrej11
Ondrej11

Reputation: 63

How create call-graph using SPOON API

If somebody used SPOON API can you give me some clue how to create call-graph and what I need to do it?

I think that I need to create some Processor like this:

public class InvocationProcessor extends AbstractProcessor<CtInvocation> {
    @Override
    public void process(CtInvocation element) {
        System.out.println(element.getActualTypeArguments());
    }
}

and then use Launcher for run analyse

public void getCallers(){

    final Launcher launcher = new Launcher();
    launcher.setArgs("-i D:\\IntelliJ_projects\\ComprehensionTool\\ -p comprehensionTool.analyse.processor.InvocationProcessor".split(" "));
    launcher.run();
}

but I am not sure about it... and I want to ask that do I need some special dependencies for using Launcher?

I assume that I am wrong because when I executed it this error throw:

Exception in thread "main" java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.batch.Main.<init>(Ljava/io/PrintWriter;Ljava/io/PrintWriter;ZLjava/util/Map;Lorg/eclipse/jdt/core/compiler/CompilationProgress;)V
    at spoon.support.compiler.jdt.JDTBatchCompiler.<init>(JDTBatchCompiler.java:58)
    at spoon.support.compiler.jdt.JDTBatchCompiler.<init>(JDTBatchCompiler.java:54)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.createBatchCompiler(JDTBasedSpoonCompiler.java:352)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.createBatchCompiler(JDTBasedSpoonCompiler.java:356)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnits(JDTBasedSpoonCompiler.java:388)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:372)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:348)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:119)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:102)
    at spoon.Launcher.buildModel(Launcher.java:700)
    at spoon.Launcher.run(Launcher.java:651)

Upvotes: 0

Views: 1457

Answers (2)

Tom Orsi
Tom Orsi

Reputation: 51

I use recursion and start with a CtInvocation.

  1. Find enclosing CtMethod.
  2. Find N CtInvocations of CtMethod.
  3. For Each CtInvocation in N recurse to step 1.

For better or worse, I used the GetParent() in a loop to find the CtMethod of the CtInvocation, seems like it would find the CtBody and then the CtMethod, but I think you need this for the case where the the CtInvocation is in other blocks ( if-statement ), while-loop, etc.

Upvotes: 1

Simon Urli
Simon Urli

Reputation: 459

You need a dependency to eclipse JDT, we are using this one:

<dependency>
  <groupId>org.eclipse.tycho</groupId>
  <artifactId>org.eclipse.jdt.core</artifactId>
  <version>3.12.0.v20160516-2131</version>
</dependency>

The way you are launching Spoon seems good, however your processor will traverse the entire model and print invocation types: it does not follow the invocations. Creating a call-graph is not that easy: you have first to get the starting point (for example a method) and then to follow the invocation.

Getting the starting point is easy: have a look at Spoon documentation, but then you need to manually traverse the call-graph to build it.

Upvotes: 2

Related Questions