在Assignment和VariableDeclarationFragment ASTParser中,Resolve类型绑定始终为null

问题描述 投票:0回答:1

我正在使用ast parser eclipse api开发独立的Java应用程序。我试图提取字段和方法声明的类型绑定。这样做的逻辑在我的解析器和Visitor类中(见下文)。

    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(parseFile);
    parser.setUnitName(fileName);
    parser.setResolveBindings(true);
    parser.setEnvironment(null,null, null, true);
    CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null); // parse
    return compilationUnit; 

和访客班

  for (final TypeDeclaration typeDeclaration : allTypes) {
        typeDeclaration.accept(new ASTVisitor() {           
            @Override
            public boolean visit(final Assignment node) {
                addLogs("Assignment Node type :- "+node);
                ITypeBinding iTypeBinding = node.resolveTypeBinding();      
                .............
             }
         });

           @Override
           public  boolean visit(VariableDeclarationFragment node) {
              if(node == null || node.getInitializer() == null) return super.visit(node);
            final IVariableBinding iVariableBinding = node.resolveBinding();
            if(iVariableBinding == null) return super.visit(node);
            ITypeBinding iTypeBinding = node.getName().resolveTypeBinding();
            ..............
            }
          });

但是,resolve绑定始终为null,因此我无法获取变量名称并完全限定变量的类类型。

提前致谢。

java eclipse parsing abstract-syntax-tree
1个回答
2
投票

在ASTParser setEnvironment(classPath,sourcePath,contentType,true)中设置环境。这里classPath是源类文件位置,sourcePath是源java文件位置,内容类型是UTF-8。

© www.soinside.com 2019 - 2024. All rights reserved.