VariableDeclarationFragment node resolveBindind() returns null in eclipse/jdt/ast

前端 未结 3 657
故里飘歌
故里飘歌 2020-12-07 04:12

I\'m trying to try out eclipse jdt/ast following this article.

This is the java code as an input:

class Hello
{
    int hello()
    {
        int a =         


        
3条回答
  •  無奈伤痛
    2020-12-07 04:44

    I tried solving this using the following piece of code

    protected static CompilationUnit parseStatements(String source) {
        ASTParser parser = ASTParser.newParser(AST.JLS8);
           parser.setSource(source.toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setResolveBindings(true);
    
        parser.setEnvironment( // apply classpath
                new String[] { "//home//user//Projects//SmartCopy//ASTParser_Test//bin" }, //
                null, null, true);
        parser.setUnitName("any_name");
        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
        return cu;
    
    }
    
    
    static void newcheckVariableDeclaration(){
        String source ="package javaproject;" // package for all classes
                + "class Dummy {"
                + "int j;" //
                + "   public void add(){"
                + "int x=0,y=0;"
                + "j=x+y;\n" //
                + "   }" //
                + "}"; 
    
        final CompilationUnit root = parseStatements(source);
        root.accept(new ASTVisitor() {
            public boolean visit(SimpleName node) {
                System.out.println(node.toString());
                if(node.resolveBinding() == null){
                    System.out.println(node.toString()+" is not declared");
                }
                else{
                    System.out.println(node.toString() + " is declared");
                }
                System.out.println();
                return true;
            }
        });
    

提交回复
热议问题