AST Resolve Bindings in Standalone Application [message #1403695] |
Fri, 25 July 2014 12:11  |
Eclipse User |
|
|
|
Hello everyone,
I'm currently working on a tool for calculating metrics and decided to use eclipse-AST tools for that purpose.
I've startet using resolveBindings(), which seems to work in certain projects my metrics tool runs on, and returns null in others. Therefore I think my configuration of the AST Parser might be the fault.
Currently I'm working with this:
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(sourceCode.toCharArray());
parser.setEnvironment(null, null, null, true);
parser.setUnitName(sourceName);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
for (ASTVisitor visitor : visitors.values()) {
cu.accept(visitor);
}
sourceCode is the sourceCode of several Java files put together in one String.
sourceName is the basedirectory of the project.
I've tried to parse one AST per source-file, but I need the method isFromSource(), which returns false for all classes in the project, that are not in the parsed AST.
Does anyone know, what's the correct way of building an AST with resolved bindings over several java files which are located in different packages ?
Thanks in advance,
Felix
|
|
|
|
Re: AST Resolve Bindings in Standalone Application [message #1404107 is a reply to message #1404088] |
Tue, 29 July 2014 16:15   |
Eclipse User |
|
|
|
I'm currently recursing over a project and whenever my program finds an .java file it appends it to a sourcestring. I tried appending the parent folder of each file to an String array which I passed to the setEnvironment() as sourcePaths. This does not change the behaviour of the resolveBinding() though.
Again, I do not know, what I should pass to the setUnitName() Method. Currently I'm passing the name of the project folder, but that can't be it...
Any further help would be greatly appreciated 
Best regards,
Felix
|
|
|
Re: AST Resolve Bindings in Standalone Application [message #1404115 is a reply to message #1404107] |
Tue, 29 July 2014 17:11   |
Eclipse User |
|
|
|
Quote:whenever my program finds an .java file it appends it to a sourcestring
what exactly do you mean by that??
Mh, the javadoc of setEnvironment() may not be perfectly clear in this, but I do read it as expecting root directories suitable as -cp or -sourcepath arguments for the batch compiler, i.e., root directories containing a directory structure corresponding to your package structure, *not* individual files.
HTH
Stephan
[Updated on: Tue, 29 July 2014 17:11] by Moderator Report message to a moderator
|
|
|
Re: AST Resolve Bindings in Standalone Application [message #1404124 is a reply to message #1404115] |
Tue, 29 July 2014 18:23   |
Eclipse User |
|
|
|
I'm retriving all .java Files from a project and store them in a List. Then I call this method:
public void analyze(List<File> files, File baseDirectory) {
StringBuilder source = new StringBuilder();
List<String> sourcePath = new ArrayList<String>();
for (File file : files) {
source.append(readFileToString(file));
sourcePath.add(file.getParent());
}
parse(source.toString(), sourcePath.toArray(new String[0]), baseDirectory.getName());
}
The Method "readFileToString(File)" just returns the content of the given file as a String. I've also attached the current state of my parse method, that creates the AST.
private void parse(String sourceCode, String[] sourcePath, String sourceName) {
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(sourceCode.toCharArray());
parser.setEnvironment(sourcePath, sourcePath, null, true);
parser.setUnitName(sourceName);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
for (ASTVisitor visitor : visitors.values()) {
cu.accept(visitor);
}
}
I hope this clarifies it!
I tried to change the sourcepath to the root directories containing the packages, but no result either: for clarification, I'm running my code on maven projects so the project structure is:
src
--main
----java
------package...
--test
----java
------package...
so I'm currently passing the absolute paths ending with \java to the setEnvironment.
Again, my program is able to resolve bindings on some sources but fails to do so on others... and I would be certainly very happy if that would be caused by a misconception on my behalf and would be fixable 
Best regards,
Felix
|
|
|
Re: AST Resolve Bindings in Standalone Application [message #1404130 is a reply to message #1404124] |
Tue, 29 July 2014 19:59   |
Eclipse User |
|
|
|
putting the content of all source files into a single string doesn't make any sense to me. Each compilation unit starts with one package declaration and a list of imports. Any imports found later in the file will cause syntax errors, etc ...
and yes, using the standard maven layout, you should specify these two source paths: "src/main/java" and "src/test/java".
what about 3rd party dependencies?
|
|
|
Re: AST Resolve Bindings in Standalone Application [message #1404133 is a reply to message #1404130] |
Tue, 29 July 2014 20:33   |
Eclipse User |
|
|
|
Stephan Herrmann wrote on Tue, 29 July 2014 15:59putting the content of all source files into a single string doesn't make any sense to me.
Same here! But I could not find any other solution as I'm working outside of an eclipse plugin project but need to parse several files at once. If there is one I missed, I would greatly appreciate your input! 
Best regards,
Felix
|
|
|
|
Re: AST Resolve Bindings in Standalone Application [message #1404347 is a reply to message #1404134] |
Thu, 31 July 2014 12:27   |
Eclipse User |
|
|
|
Thanks, I did try that. Sourcecode is attached:
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setEnvironment(null, envPath, null, true);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
List<String> bindingKeys = new ArrayList<String>();
for (String typeName : typeNames) {
bindingKeys.add(BindingKey.createTypeBindingKey(typeName));
}
FileASTRequestor requestor = new FileASTRequestor() {
@Override
public void acceptAST(String sourceFilePath, CompilationUnit ast) {
super.acceptAST(sourceFilePath, ast);
for (AbstractVisitor visitor : visitors.values()) {
ast.accept(visitor);
}
}
};
parser.createASTs(sourcePaths, null, bindingKeys.toArray(new String[0]), requestor, null);
typeNames is a String array containing the names of the types up from the \java, so for example: com\package\artifact\test.java
envPath is the environment up to /java... for example: C:\...\src\java
Same result; works on certain projects but crashes on others 
|
|
|
|
Powered by
FUDForum. Page generated in 0.04746 seconds