How to get ASTNode declaration in another CompilationUnit? [message #1108743] |
Sat, 14 September 2013 05:22  |
Eclipse User |
|
|
|
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06599 seconds