01: // This file is part of KeY - Integrated Deductive Software Design
02: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
03: // Universitaet Koblenz-Landau, Germany
04: // Chalmers University of Technology, Sweden
05: //
06: // The KeY system is protected by the GNU General Public License.
07: // See LICENSE.TXT for details.
08: //
09: //
10:
11: package de.uka.ilkd.key.java.visitor;
12:
13: import de.uka.ilkd.key.java.ListOfProgramElement;
14: import de.uka.ilkd.key.java.ProgramElement;
15: import de.uka.ilkd.key.java.SLListOfProgramElement;
16:
17: /** Walks through a java AST in depth-left-fist-order.
18: * You can set the type of nodes you want to collect and then start the
19: * walker. The found nodes of the given type are returned as a
20: * ListOfJavaProgramElement
21: */
22: public class JavaASTCollector extends JavaASTWalker {
23:
24: /** the type of nodes to be collected */
25: private Class type;
26: /** the list of found elements */
27: private ListOfProgramElement resultList = SLListOfProgramElement.EMPTY_LIST;
28:
29: /** create the JavaASTWalker
30: * @param root the ProgramElement where to begin
31: * @param type the Class representing the type of nodes that have
32: * to be collected
33: */
34: public JavaASTCollector(ProgramElement root, Class type) {
35: super (root);
36: this .type = type;
37: }
38:
39: /** the action that is performed just before leaving the node the
40: * last time
41: */
42: protected void doAction(ProgramElement node) {
43: if (type.isInstance(node)) {
44: resultList = resultList.prepend(node);
45: }
46: }
47:
48: /** returns the found nodes of the specified type
49: * @return the found nodes of the specified type as list
50: */
51: public ListOfProgramElement getNodes() {
52: return resultList;
53: }
54:
55: }
|