在 Assignment 和 VariableDeclarationFragment ASTParser 中解析类型绑定总是 null

Resolve type binding is always null in Assignment and VariableDeclarationFragment ASTParser

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

    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; 

和访客class

  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();
            ..............
            }
          });

但解析绑定始终为空,因此我无法获取变量名并完全限定 class 类型的变量。

提前致谢。

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