Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Using ASTParser outside Eclipse(Problem accessing Bindings when using the ASTParser outside Eclipse)
Using ASTParser outside Eclipse [message #633585] Mon, 18 October 2010 13:14 Go to next message
Sebastian Proksch is currently offline Sebastian ProkschFriend
Messages: 2
Registered: October 2010
Junior Member
Hi, I'm trying to use the JDT-Parser outside the Eclipse context. With the help of the Eclipse forums, I'm able to parse source files and to access them as ASTNodes:

class MinimalParser
...
ASTParser pars = ASTParser.newParser(AST.JLS3);

CLASS_PATH.add(".");
SOURCE_PATH.add("<path to some source directory>");
SOURCE_FILES.add("<path to a java source file>);

pars.setEnvironment(CLASS_PATH.toArray(new String[0]), SOURCE_PATH.toArray(new String[0]), null, true);
pars.setResolveBindings(true);
pars.setKind(ASTParser.K_COMPILATION_UNIT);

FileASTRequestor req = new FileASTRequestor() {
	@Override
	public void acceptAST(String sourceFilePath, CompilationUnit ast) {
		ast.getRoot().accept(new MinimalVisitor());
	}
};

pars.createASTs(SOURCE_FILES.toArray(new String[0]), null, new String[0], req, null);
...

class MinimalVisitor extends ASTVisitor {
...
@Override
public boolean visit(MethodDeclaration node) {
	IMethodBinding binding = node.resolveBinding();
	System.out.println(binding.getJavaElement());
	return false;
}
...

The ".visit()" method is the point where I run into problems. As far as I can see, the ".getJavaElement()" Method should return an IJavaElement, but I actually get "null". With the Debugger I can point the "problem"(?) to

package org.eclipse.jdt.core.dom;

class MethodBinding
...
private JavaElement getUnresolvedJavaElement() {
	...
	if (!defaultBindingResolver.fromJavaProject) return null;
	...
}
...

Obviously the resolver needs a java project when used this way, but as far as I unterstand this should not be necessary according to the description of "ASTParser.setEnvironment(...)". I'm not sure, if this is the right way to get IJavaElements or some type of ITypes or if there is another way, that actually works. Any hints, what I'm doing wrong or how to access ITypes out of Eclipse?

PS: I'm adding the following jars to my classpath to get the code running:

/* included jar files */
org.eclipse.core.contenttype_3.4.100.v20100505-1235.jar
org.eclipse.core.runtime_3.6.0.v20100505.jar
org.eclipse.equinox.common_3.6.0.v20100503.jar
org.eclipse.equinox.preferences_3.3.0.v20100503.jar
org.eclipse.core.jobs_3.5.1.R36x_v20100824.jar
org.eclipse.core.resources_3.6.0.R36x_v20100825-0600.jar
org.eclipse.jdt.core_3.6.1.v_A68_R36x.jar
org.eclipse.osgi_3.6.1.R36x_v20100806.jar
Re: Using ASTParser outside Eclipse [message #634677 is a reply to message #633585] Fri, 22 October 2010 17:25 Go to previous message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
IJavaElement are not available when using ASTParser outside of a headless Eclipse since there is no Java model initialized.
So what you are seeing is working as expected. If you want java elements, then you need to work inside a headless Eclipse where the JDT/Core bundle is initialized.

If you believe the documentation needs to be updated to better reflect this, then open a bug report against JDT/Core.

Thanks,
--
Olivier

[Updated on: Fri, 22 October 2010 17:28]

Report message to a moderator

Previous Topic:ASTParser having problems with line comments
Next Topic:The final local variable output may already have been assigned in a try catch block
Goto Forum:
  


Current Time: Fri Mar 29 04:33:29 GMT 2024

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

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

Back to the top