Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Problem with ASTVisitor
Problem with ASTVisitor [message #509845] Mon, 25 January 2010 10:24 Go to next message
Eclipse UserFriend
I'm trying to find all ReturnStatement-s in a given MethodDeclaration:

MethodDeclaration decl = ... 
System.out.println("decl = " + decl);
final List<ReturnStatement> returnStmts = new ArrayList<ReturnStatement>();

ASTVisitor visitor = new ASTVisitor() {
	@Override
	public boolean visit(ReturnStatement node) {
		System.out.println("visiting " + node);
		returnStmts.add(node);
		return true;
	}
	@Override
	public void preVisit(ASTNode node) {
		System.out.println("visiting " + node);
	}
};
visitor.visit(decl);



I get empty list and only following output:

decl = static String getWhereClause(){
  return "where 1=2";
}


this shows that "decl" is OK, and it has a ReturnStatement.

Any idea what's wrong here?
Re: Problem with ASTVisitor [message #509912 is a reply to message #509845] Mon, 25 January 2010 13:30 Go to previous messageGo to next message
Eclipse UserFriend
Aivar Annamaa wrote:

> I'm trying to find all ReturnStatement-s in a given MethodDeclaration:
>
>
> MethodDeclaration decl = ...
> System.out.println("decl = " + decl);
> final List<ReturnStatement> returnStmts = new
> ArrayList<ReturnStatement>();
>
> ASTVisitor visitor = new ASTVisitor() {
> @Override
> public boolean visit(ReturnStatement node) {
> System.out.println("visiting " + node);
> returnStmts.add(node);
> return true;
> }
> @Override
> public void preVisit(ASTNode node) {
> System.out.println("visiting " + node);
> }
> };
> visitor.visit(decl);

This is, what you are invoking here (see ASTVisitor):

public boolean visit(MethodDeclaration node) {
return true;
}

:)

Try:
decl.accept(visitor);

HTH,
Stephan
Re: Problem with ASTVisitor [message #510046 is a reply to message #509912] Tue, 26 January 2010 05:55 Go to previous message
Eclipse UserFriend
Stephan Herrmann wrote on Mon, 25 January 2010 13:30


This is, what you are invoking here (see ASTVisitor):

public boolean visit(MethodDeclaration node) {
return true;
}

Smile

Try:
decl.accept(visitor);

HTH,
Stephan


Woohoo, much better now Very Happy
That's what you get when working while being tired Razz

thanks a lot!
Previous Topic:Have multiple views on a single document you're working on
Next Topic:Access AST generated by JDT at build time
Goto Forum:
  


Current Time: Thu Mar 20 15:24:37 EDT 2025

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

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

Back to the top