简体   繁体   English

从使用JDT的方法开始深入访问所有方法调用

[英]Visit all method calls deep down starting from method with JDT

I need to parse java method, looking inside all other method calls inside it (and inside them and deeper and so on) in order to find all occurences of some string, let say "System.out.println ("Blabla");" 我需要解析java方法,在其中查找所有其他方法调用(并在其中进行更深入的查找,以此类推),以便查找某个字符串的所有出现情况,例如说“ System.out.println(“ Blabla”);“

How can I use JDT ( http://www.programcreek.com/2011/01/best-java-development-tooling-jdt-and-astparser-tutorials ) for that and what are the other alternatives? 我该如何使用JDT( http://www.programcreek.com/2011/01/best-java-development-tooling-jdt-and-astparser-tutorials )以及其他替代方法?

If I have a code like this: 如果我有这样的代码:

public void A() {

    "System.out.println ("Blabla");
    B();        

}

public void B() {

    "System.out.println ("Blabla");
    C();

}

public void C() {

    "System.out.println ("Blabla");

}

I would like just to specify the name of the method ("A") and as an output I want: 我只想指定方法的名称(“ A”),并希望将其作为输出:

"System.out.println ("Blabla");
"System.out.println ("Blabla");
"System.out.println ("Blabla");

You want to do that by hand or programmatically? 您要手动还是以编程方式执行此操作?

I would suggest using the Open Call Hierarchy option in Eclipse, just press Ctrl + Alt + H to check if that's what you need. 我建议使用Eclipse中的Open Call Hierarchy选项,只需按Ctrl + Alt + H来检查是否需要此功能。 It traces back all the way up where the given method is called. 它一直追溯到调用给定方法的位置。 If you're familiar with JDT, you must have some experience in how to find the implementation of that Eclipse feature so you can analyze it further (I'd suggest using the Plug-in Spy ). 如果您熟悉JDT,则必须在如何找到该Eclipse功能的实现方面有一定的经验,以便您可以进一步分析它(我建议使用Plug-in Spy )。

Hope that helps something. 希望能有所帮助。

This is possible with JDT/AST. 使用JDT / AST可以做到这一点。 I have done similar things. 我做过类似的事情。 My favorite approach is to create a plugin for Eclipse and process files (compilation units) inside of the projects. 我最喜欢的方法是为Eclipse创建一个插件,并在项目内部处理文件(编译单元)。

The basic steps to follow are: 遵循的基本步骤是:

  1. You can use AST to parse the code (ie of a JDT ICompilationUnit) into an AST model. 您可以使用AST将代码(即JDT ICompilationUnit的代码)解析为AST模型。

  2. You can then use the visitor pattern to find the MethodDeclaration you want to start your search. 然后,您可以使用访问者模式找到要开始搜索的MethodDeclaration。

  3. Once you have that, you can use the visitor again on the MethodDeclaration to find all MethodInvocations inside the method body. 一旦有了它,就可以在MethodDeclaration上再次使用访问者来查找方法主体内的所有MethodInvocations。

  4. The binding of the MethodInvocation will point you to the MethodDeclaration pf the method beeing called. MethodInvocation的绑定将使您指向方法beeing所调用的MethodDeclaration。 (Be sure to enable bindings while parsing.) (确保在解析时启用绑定。)

A realy nice tool to use, when working with AST, is the AST View. 与AST一起使用时,真正非常好的工具是AST View。 It shows you the AST model of files opened with the Eclipse Java editor. 它显示了使用Eclipse Java编辑器打开的文件的AST模型。 You can install the AST View from this update site: http://www.eclipse.org/jdt/ui/update-site 您可以从以下更新站点安装AST视图: http : //www.eclipse.org/jdt/ui/update-site

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM