Extracting class usage from within Java methods using ctags
Asked Answered
P

1

6

I would like to know if it is possible to use ctags to extract class usage from within Java methods. So far, I was able to only use it to get a listing of methods and instance variables but not which classes are utilized within methods.

E.g.

...
public void doSomething(MyClass myClassInst) {
     int someVar = myClassInst.getSomeVar();
     System.out.println("Some var is " + someVar);
}
...

Right now all I get is a recognition of the method declaration. But I would like to extract that doSomething is also utilizing the classes MyClass and System but not just to parse the text but also know the full class address including the package (java.lang.System and com.mypackage.MyClass).

In order to be able to extract this kind of metadata, ctags would need to have access to the compiler's abstract syntax tree and I am not sure if it does that or it simply does text parsing of the source code.

Is there a way to accomplish what I am trying to do using ctags or does it eclipse the scope of its functionality?

Parcae answered 20/12, 2013 at 21:46 Comment(7)
How important is it to use ctags? You can accomplish some code analysis tasks by extending a static analysis tool such as PMD. PMD should provide an abstract syntax tree.Metrics
not important. my goal is static code analysisParcae
In that case, I recommend checking out PMD. It was not created specifically for your use case, but it should give you the capability you need.Metrics
i installed it and trying to figure out how to get out of it what i need.Parcae
@Los, do you have any idea which command line options to use to accomplish what i am looking for ?Parcae
it is not very well documentedParcae
You will need to write a custom rule to do exactly what you need. Documentation is here: pmd.sourceforge.net/pmd-5.0.0/howtowritearule.html . It uses the visitor pattern. That page includes the command line usage.Metrics
C
1

You can use asm or BCEL to achieve this.

Corneliuscornell answered 10/7, 2014 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.