Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » AST Resolve Bindings in Standalone Application
AST Resolve Bindings in Standalone Application [message #1403695] Fri, 25 July 2014 12:11 Go to next message
Felix Thiele is currently offline Felix ThieleFriend
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 #1404088 is a reply to message #1403695] Tue, 29 July 2014 14:39 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Have you tried to properly place all your classes in separate files and setting proper source/classpaths via ASTParser.setEnvironment()?
Re: AST Resolve Bindings in Standalone Application [message #1404107 is a reply to message #1404088] Tue, 29 July 2014 16:15 Go to previous messageGo to next message
Felix Thiele is currently offline Felix ThieleFriend
Messages: 5
Registered: July 2014
Junior Member
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 Smile

Best regards,

Felix

Re: AST Resolve Bindings in Standalone Application [message #1404115 is a reply to message #1404107] Tue, 29 July 2014 17:11 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
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 Go to previous messageGo to next message
Felix Thiele is currently offline Felix ThieleFriend
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 Wink

Best regards,

Felix





Re: AST Resolve Bindings in Standalone Application [message #1404130 is a reply to message #1404124] Tue, 29 July 2014 19:59 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
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 Go to previous messageGo to next message
Felix Thiele is currently offline Felix ThieleFriend
Messages: 5
Registered: July 2014
Junior Member
Stephan Herrmann wrote on Tue, 29 July 2014 15:59
putting the content of all source files into a single string doesn't make any sense to me.


Same here! Wink 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! Smile

Best regards,

Felix



Re: AST Resolve Bindings in Standalone Application [message #1404134 is a reply to message #1404133] Tue, 29 July 2014 20:37 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
have you tried org.eclipse.jdt.core.dom.ASTParser.createASTs(String[], String[], String[], FileASTRequestor, IProgressMonitor) ?
Re: AST Resolve Bindings in Standalone Application [message #1404347 is a reply to message #1404134] Thu, 31 July 2014 12:27 Go to previous messageGo to next message
Felix Thiele is currently offline Felix ThieleFriend
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 Sad
Re: AST Resolve Bindings in Standalone Application [message #1411130 is a reply to message #1404347] Fri, 22 August 2014 13:17 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
In case you're still searching for a solution, it seems you have to find out what's the difference between the cases that work vs. crash.
What kind of crash, BTW? Inside JDT code or in your own code because you try to work with missing bindings?

S.
Previous Topic:Safe to turn off forbidden references compiler errors in this case?
Next Topic:Code Generation Error using Axis2 wizard
Goto Forum:
  


Current Time: Thu Apr 18 16:18:33 GMT 2024

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

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

Back to the top