AST Resolve Bindings in Standalone Application [message #1403695] |
Fri, 25 July 2014 12:11 |
Felix Thiele Messages: 5 Registered: July 2014 |
Junior Member |
|
|
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 #1404115 is a reply to message #1404107] |
Tue, 29 July 2014 17:11 |
Stephan Herrmann Messages: 1853 Registered: July 2009 |
Senior Member |
|
|
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] 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 |
Felix Thiele Messages: 5 Registered: July 2014 |
Junior Member |
|
|
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 #1404347 is a reply to message #1404134] |
Thu, 31 July 2014 12:27 |
Felix Thiele Messages: 5 Registered: July 2014 |
Junior Member |
|
|
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.05036 seconds