Bug 15036 - ASTVisitor.preVisit and ASTVisitor.postVisit not called correctly
Summary: ASTVisitor.preVisit and ASTVisitor.postVisit not called correctly
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC All
: P3 minor (vote)
Target Milestone: 2.0 M6   Edit
Assignee: Jim des Rivieres CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-05-01 15:34 EDT by mark_dixon CLA
Modified: 2002-05-01 19:29 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description mark_dixon CLA 2002-05-01 15:34:09 EDT
I may be misunderstanding the model here, but preVisit and postVisit seem to be 
called incorrectly.

1. Create a one line java source file:

package thepackage;

2. Run this code over it:
    // icu is an ICompilationUnit for the above file
    CompilationUnit cu = AST.parseCompilationUnit(icu, true);

    System.out.println(icu.getResource().getName());
    cu.accept (new ASTVisitor() {
	public void preVisit(ASTNode node) {
	    System.out.println("Pre:  " + node.getClass());
        }
        public void postVisit(ASTNode node) {
	    System.out.println("Post: " + node.getClass());
        }
    });

Expected output is something like:
Pre:  class org.eclipse.jdt.core.dom.CompilationUnit
Pre:  class org.eclipse.jdt.core.dom.PackageDeclaration
Pre:  class org.eclipse.jdt.core.dom.SimpleName ??
Post: class org.eclipse.jdt.core.dom.SimpleName ??
Post: class org.eclipse.jdt.core.dom.PackageDeclaration
Post: class org.eclipse.jdt.core.dom.CompilationUnit

Actual output is:
Pre:  class org.eclipse.jdt.core.dom.CompilationUnit
Pre:  class org.eclipse.jdt.core.dom.CompilationUnit
Pre:  class org.eclipse.jdt.core.dom.PackageDeclaration
Post: class org.eclipse.jdt.core.dom.PackageDeclaration
Post: class org.eclipse.jdt.core.dom.CompilationUnit
Post: class org.eclipse.jdt.core.dom.CompilationUnit

I think the problems are in ASTNode.acceptChild and ASTNode.acceptChildren. E.g 
in ASTNode.getChild shouldn't

		// begin with the generic pre-visit
		visitor.preVisit(this);
		// dynamic dispatch to internal method for type-specific 
visit/endVisit
		child.accept0(visitor);
		// end with the generic post-visit
		visitor.postVisit(this);

be

		// begin with the generic pre-visit ** for the child **
		visitor.preVisit(child);
		// dynamic dispatch to internal method for type-specific 
visit/endVisit
		child.accept0(visitor);
		// end with the generic post-visit ** for the child **
		visitor.postVisit(child);

or, even simpler,

    // visit the child
    child.accept(visitor);

and similarly in acceptChildren?

Thanks
Mark
Comment 1 Olivier Thomann CLA 2002-05-01 18:41:07 EDT
I agree that:
Pre:  class org.eclipse.jdt.core.dom.CompilationUnit
Pre:  class 
org.eclipse.jdt.core.dom.PackageDeclaration
Pre:  class 
org.eclipse.jdt.core.dom.SimpleName ??
Post: class 
org.eclipse.jdt.core.dom.SimpleName ??
Post: class 
org.eclipse.jdt.core.dom.PackageDeclaration
Post: class 
org.eclipse.jdt.core.dom.CompilationUnit

looks much more natural.
Comment 2 Jim des Rivieres CLA 2002-05-01 19:27:46 EDT
It is indeed supposed to work that way you expected. The implementation was 
totally bogus, as you point out. Thanks muchly for reporting this.

Comment 3 Jim des Rivieres CLA 2002-05-01 19:29:24 EDT
Made fix and added a test to check this.