eclipse中的AST处理不能解析绑定 [英] bindings not resolving with AST processing in eclipse

查看:544
本文介绍了eclipse中的AST处理不能解析绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eclipse JDT AST解析器处理一些Java代码,并尝试提取字段和方法声明的类型绑定。这样做的逻辑是在我的访客类中(见下文)。不幸的是,我没有任何运气,没有一个绑定解决(他们一直是null)。有趣的是,绑定使用与eclipse ASTView插件相同的代码。我做错了什么?



这是一些相关的代码片段,希望能帮助某人弄清楚发生了什么!

  ASTParser parser = ASTParser.newParser(AST.JLS3); 
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source);
parser.setResolveBindings(true);
CompilationUnit unit =(CompilationUnit)parser.createAST(null);

GenericVisitor visitor = new GenericVisitor(outDir +//+ file.getName()+.xml);
visitor.process(unit);


public class GenericVisitor extends ASTVisitor
{
public void endVisit(FieldDeclaration node)
{
String bindingInfo =;
ITypeBinding binding = node.getType()。resolveBinding();

if(binding == null)
{
System.out.println(field declaration binding = null);
}
else
{
bindingInfo = binding.getQualifiedName();
}

endVisitNode(node,bindingInfo);
}

public void endVisit(MethodInvocation node)
{
String bindingInfo =;
IMethodBinding binding = node.resolveMethodBinding();

if(binding == null)
{
System.out.println(method binding = null);
}
else
{
bindingInfo = binding.toString();
}

endVisitNode(node,bindingInfo);
}
}


解决方案

你使用:
parser.setSource(source);
paramsource的类型是什么?


绑定信息是从
获得的Java模型。这意味着
编译单元必须位于
相对于Java模型。当
代码来源于
setSource(ICompilationUnit)或
setSource(IClassFile)时,
会自动发生。当$ s
由setSource(char [])提供时,
位置必须通过调用
setProject(IJavaProject)和
setUnitName(String)来显式地被引入
。这个来自 http://help.eclipse.org/galileo/index.jsp? topic = / org.eclipse.jdt.doc.isv / reference / api / org / eclipse / jdt / core / dom / ASTParser.html
我想也许你只是使用setSource(char [])不调用setProject(IJavaProject)和setUnitName(String)


I'm using the eclipse JDT AST parser to process some Java code and am trying to extract the type bindings for fields and method declarations. The logic for doing that is inside my Visitor class (see below). Unfortunately, I'm not having any luck and none of the bindings are resolving (they are consistently null). The interesting thing is that the bindings do work on the same code with the eclipse ASTView plugin. What am I doing wrong?

Here are some relevant code snippets which will hopefully help someone figure out what is going on!

ASTParser parser = ASTParser.newParser(AST.JLS3); 
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source);
parser.setResolveBindings(true);
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

GenericVisitor visitor = new GenericVisitor(outDir + "//" + file.getName() + ".xml");
visitor.process(unit);


public class GenericVisitor extends ASTVisitor 
{
    public void endVisit(FieldDeclaration node) 
    {
        String bindingInfo = "";    
        ITypeBinding binding = node.getType().resolveBinding();

        if(binding == null)
        {                    
         System.out.println("field declaration binding = null");
        }
        else
        {
         bindingInfo = binding.getQualifiedName();
        }

        endVisitNode(node, bindingInfo); 
    }

    public void endVisit(MethodInvocation node) 
    {
         String bindingInfo = "";   
         IMethodBinding binding = node.resolveMethodBinding();

     if(binding == null)
     {                    
        System.out.println("method binding = null");
     }
     else
     {
         bindingInfo = binding.toString();
     }

    endVisitNode(node, bindingInfo); 
    }
} 

解决方案

When you use: parser.setSource(source); What is the type of param "source"?

Binding information is obtained from the Java model. This means that the compilation unit must be located relative to the Java model. This happens automatically when the source code comes from either setSource(ICompilationUnit) or setSource(IClassFile). When source is supplied by setSource(char[]), the location must be extablished explicitly by calling setProject(IJavaProject) and setUnitName(String).

This is from http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/ASTParser.html I think maybe you just use setSource(char[]) without calling setProject(IJavaProject) and setUnitName(String)

这篇关于eclipse中的AST处理不能解析绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆