java - How do I get the type of the expression in a MemberSelectTree from a javac plugin? -
I am trying to write an annotation processor in JSR 269 format which will be used by javac compiler tree API uses it. I am interested in member selection expression, such as call method.
I can easily choose the name of the method (or field, etc.). But I want to know how the member is being chosen, and I can not find a simple way of doing this. tree .getTypeMirror
returns null
for everything I try to call it (and Javadoc does not give any signals).
I think I can completely analyze every kind, select the expression on the left side of the member and determine the constant type of expression by recursive analysis: NewClassTree
, TypeCastTree
, MethodInvocationTree
, array asset
, and many others. But it seems like a lot of error-prone work, and clearly Javac already knows the steady type of expression because it requires this information for many purposes. But how do I get this kind of information?
Whatever I have done so far:
import com.sun.source.tree.MemberSelectTree; Import com.sun.source.tree.MethodInvocationTree; Import com.sun.source.util.TreePath; Import com.sun.source.util.TreePathScanner; Import com.sun.source.util.Trees; Import java.util.Set; Import javax.annotation.processing.AbstractProcessor; Import javax.annotation.processing.RoundEnvironment; Import javax.annotation.processing.SupportedAnnotationTypes; Import javax.annotation.processing.SupportedSourceVersion; Import javax.lang.model.SourceVersion; Import javax.lang.model.element.Element; Import javax.lang.model.element.TypeElement; @ Supported Onoteshn type ( "*") @SupportedSourceVersion (SourceVersion.RELEASE_6) public class Sarwajnikprosesr expanded by Abbropprosesr {public @ override Boolean process (set & lt; type Element & gt; annotation, round Annornment round ENV) {For (element E: round ANV.Getroot Elements ()) {tree of the last tree = tree Difference (processing ANV); Last trippy root = tree .getPath (e); New Tree Scanner & lt; Zero, zero> () {Public @ override zero travel method periods (method invocationTree node, zero P) {System.err.println ("Visiting method greetings:" + node + "type:" + node.getMethodSelect () getKind ()); Tree Plant expr = TreePath.getPath (Route, Node); System.err.println ("Type:" + tree .getTypeMirror (expr)); Return super.visitMethodInvocation (node, p); } Public @ Override Wide VislemmemberSllect (MembershipTree Node, Wide P) {System.err.println ("Member Use:" + NodeGet Identifier ()); System.err.println ("From:" + getCurrentPath (). GetCompilationUnit (). GetSourceFile (.) ToUri ()); TreePath expr = TreePath.getPath (root, node.getExpression ()); System.err.println (in "expr:" + expr.getLeaf ()); System.err.println ("Type:" + tree .getTypeMirror (expr)); Return super.visitMemberSelect (node, p); }} .scan (root, faucet); } Back true; }}
and some go on to simplify code method calls, her prints:
method of the invention: The New Class (). () Method Type: MEMBER_SELECT: zero to Member Access Method: ... / in whatever.java expr: type of new Class (): null
the very addressing similar questions, so I tried to use the advice went the unspoken Unfortunately, this does not seem obvious, and the use of the com.sun.tools.javac
package is required.
Comments
Post a Comment