开发者

How can I resolve the type of identifiers when parsing Java source?

I'm parsing Java source files and I need to extract type information to guess at signatures of called methods.

e.g. I have foo.x(bar) and I need to figure out th开发者_开发百科e type of foo and bar.

I'm using a java parser that gives me a complete AST, but I'm running into problems with scoping. Is there a different parser I could use that resolves this?

This can't be resolved perfectly because of reflection, but I'm hoping a good parser can deal with scoping and casting issues in the least.

edit: I can't assume that other source files will be present, so I can't simply follow the method call to its source and read the signature from the method declaration


Java's Pluggable Annotation Processing Framework (and its associated APIs) are designed to model Java code in the way you're talking about. You can invoke the Java compiler at runtime, giving you access to the model of the source using the APIs in the javax.lang.model packages. An introductory article is available here.


Basically getting types of identifier would require you to run a big part of a Java compiler yourself. In Java there is a long way from parsing to resolving types, so implementing this is a challenging task even for good programmers.

Perhaps your best way through this would be to take the Java Compiler from OpenJDK, run the relevant compiler phases and extract the types from that.


What you need is all the name and type resolution machinery. As another poster observed, one way you can get that is to abuse the Java compiler.

But you likely have some goal in mind other than compiling java; once you have those names, you want to do something with them. The Java compiler is unlikely to help you here.

What you really want is a foundation for building a tool that processes the Java language, including name and type resolution, that will help you do the rest of your task.

Our DMS Software Reengineering Toolkit is generalized program analysis and transformation machinery. It parses code, builds ASTs, manages symbol tables, provides generic flow analysis mechnisms, supports AST modification (or construction) both procedurally and in terms of surface syntax pattens, including (re)generation of compilable text from the ASTs including any comments.

DMS has a Java Front End that enables DMS to process Java, build Java ASTs, do all that name and type resolution you want. Yes, that's a lot of machinery, equivalent to what the Java compiler has, go read your latest Java reference manual. You can build whatever custom tool you need on top of that foundation.

What you won't be able to do as a practical issue is full, accurate name and type resolution without the rest of the Java source files (or corresponding class files), no matter how you tackle it. You might be able to produce some heuristic guess, but that's all it would be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜