Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Noob Eclipse AST Conundrum
Noob Eclipse AST Conundrum [message #1016261] Tue, 05 March 2013 17:31 Go to next message
Scott Dudley is currently offline Scott DudleyFriend
Messages: 21
Registered: October 2012
Junior Member
If this isn't the correct forum and audience for AST-related questions, please pardon the intrusion.

I need to write a one-off ditty to parse a large set of java source files searching for assignment or comparison operation on arrays (not individual array elements).

I've read several howto's and well-written articles that provided sufficient fodder that I got close to my target - but no dice. Could use some help.

I'm thinking that it could all be done with two visitors - one for Assignment and the other, IfStatement. My conundrum lies in my ability (or lack thereof) to intuit the API and the DOM structure.

For instance, in the following snippet, how do I ascertain whether or not Expression left is an array (from within this Assignment visitor)?

            public boolean visit(Assignment node) {
                
                Expression left = node.getLeftHandSide();
                if (left instanceof QualifiedName)
                    System.err.println(((QualifiedName) left).getFullyQualifiedName());
                else if (left instanceof SimpleName) {
                    System.err.println(((SimpleName) left).getIdentifier());
                }


Same of IfStatement. How do I relate the InfixExpression.getLeftOperand() to it's data type and identify whether or not it's an array.

Very much appreciate any insight.
Re: Noob Eclipse AST Conundrum [message #1016263 is a reply to message #1016261] Tue, 05 March 2013 17:49 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
You should create your AST with binding resolution enabled and check the binding of the expression.
public boolean visit(Assignment node) {

Expression left = node.getLeftHandSide();
ITypeBinding binding = left.resolveTypeBinding();
if (binding.isArray()) {
....
}
...
return true;
}

Make sure you provide the right environment to properly resolve the bindings.

HTH,

Olivier
Re: Noob Eclipse AST Conundrum [message #1016267 is a reply to message #1016263] Tue, 05 March 2013 18:07 Go to previous messageGo to next message
Scott Dudley is currently offline Scott DudleyFriend
Messages: 21
Registered: October 2012
Junior Member
Quote:
Make sure you provide the right environment to properly resolve the bindings.


That may be my problem Olivier. In the attached, the ITypeBinding is null.

What am I missing?
Re: Noob Eclipse AST Conundrum [message #1016271 is a reply to message #1016267] Tue, 05 March 2013 18:53 Go to previous message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
You might want to check the methods
org.eclipse.jdt.core.dom.ASTParser.setEnvironment(String[], String[], String[], boolean) and org.eclipse.jdt.core.dom.ASTParser.setUnitName(String)
You need to provide the required classpath entries so that the bindings can be resolved. This is explained in the javadoc of the org.eclipse.jdt.core.dom.ASTParser.setResolveBindings(boolean) method.

Let me know if you have more questions.

Olivier
Previous Topic:Running APT in headless Eclipse
Next Topic:Environment variables for Java launch configuration
Goto Forum:
  


Current Time: Thu Apr 25 04:27:11 GMT 2024

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

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

Back to the top