Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to get ASTNode declaration in another CompilationUnit?
How to get ASTNode declaration in another CompilationUnit? [message #1108743] Sat, 14 September 2013 09:22 Go to next message
Ray Wu is currently offline Ray WuFriend
Messages: 4
Registered: August 2013
Junior Member
I want to use JDT without using Eclipse. This is how I get a parser
private ASTParser parserInit() {
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);       
    parser.setEnvironment(null, null, null, true);
    parser.setUnitName("test");
    parser.setResolveBindings(true);
    parser.setBindingsRecovery(true);
    return parser;
}


This is my example classes

Class A {
    public String sayHello() {
        return "hello";
    }
}

Class B {
    public B() {
        A a = new A();
        System.out.println(a.sayHello());    //  (*)
    }
}


If I use parser.setSource() parser.createAST(null) to create CompilationUnit for each file, at (*) line sayHello SimpleName.resolveBinding() will return null.

If I use createASTs(fileNames, null, new String[0], requester, null), in FileASTRequestor.acceptAST(String sourceFilePath, CompilationUnit ast) I could get a non-null IBinding object from sayHello, but when I try use use unit.findDeclaringNode(binding), it will return null. This makes sense since the method sayHello is declared in another file. However, since in acceptAST I could only get one AST at a time, I have no idea how to find out where did it get declared.

I tried to use parser.createAST(null) to generate CompilationUnit for each file at the beginning, store it in a Collection, store it as an member of my AST visitor. Whenever I have to use findDeclaringNode to figure out where is a node declared, I try all CompilationUnit to findDeclaringNode. However, it will not find the node.

Here are some code snippets:

To collect all CompilationUnit. The reason I use two parsers is because if I use the same parser to generate CompilationUnit, in requestor, SimpleName.resolveBinding() will always give me null.

ASTParser parser = parserInit();
ASTParser p2 = parserInit();

Collection <CompilationUnit> units = new ArrayList<CompilationUnit>();
for (File f: filePaths) {
    try {
        String fileContent = FileUtils.readFileToString(f);
        p2.setSource(fileContent.toCharArray());
        CompilationUnit unit = (CompilationUnit) p2.createAST(null);
        units.add(unit);
    } catch (IOException e) {
        System.err.println("read file error");
    }
}
FileASTRequestor requestor = new AnnotationASTRequestor(units);
parser.createASTs(sourceFilePaths, null, new String[0], requestor, null);


How I find out the declaring node in visitor. Document mention that when I try to findout declaration in another file, I have to feed in binding.getKey() instead of binding. But the result is still the same.

private ASTNode getDeclaringNode(IBinding binding) {
    if (binding == null) {
        return null;
    }

    ASTNode node = null;
    if (currentUnit != null) {
        node = currentUnit.findDeclaringNode(binding);
    }

    if (node == null) {
        if (units != null) {
            for (CompilationUnit unit: units) {
                node = unit.findDeclaringNode(binding.getKey());
                if (node != null) {
                    break;
                }
            }
        } else {
            System.err.println("units is empty");
        }
    } 

    return node;
}


I think the key may be on the third argument of createASTs(sourceFilePaths, encodings, String[] bindingKeys, requestor, monitor). However, I don't understand why I would have all bindingKeys I want to figure out before I run createASTs? Since all bindingKeys I will get is generated in visitor, where will get executed in requestor.acceptAST
Re: How to get ASTNode declaration in another CompilationUnit? [message #1240304 is a reply to message #1108743] Thu, 06 February 2014 06:59 Go to previous messageGo to next message
Hans Werner is currently offline Hans WernerFriend
Messages: 1
Registered: February 2014
Junior Member
Actually i have the same problem, just that I use findDeclaringNode inside eclipse, but it shows the same behavior. Did you find a solution for that?
Re: How to get ASTNode declaration in another CompilationUnit? [message #1240621 is a reply to message #1240304] Thu, 06 February 2014 21:31 Go to previous message
Ray Wu is currently offline Ray WuFriend
Messages: 4
Registered: August 2013
Junior Member
I store all CompilationUnits and bindingKeys (in string) in the first time running createASTs.
Then iterate through all elements and figure the binding.
Previous Topic:plugin to export all open classes
Next Topic:How to show right side based on left side tree selection
Goto Forum:
  


Current Time: Fri Mar 29 11:50:16 GMT 2024

Powered by FUDForum. Page generated in 0.02704 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top