KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > query > NodeScanner


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source.query;
21
22 import org.netbeans.modules.java.source.engine.EngineEnvironment;
23 import com.sun.source.tree.*;
24 import com.sun.source.util.TreeScanner;
25 import javax.lang.model.element.Element;
26 import org.netbeans.modules.java.source.engine.ASTModel;
27 import org.netbeans.modules.java.source.engine.EngineEnvironment;
28
29 /**
30  * Like TreeScanner, but stores the current modeled node
31  * being scanned for results reporting.
32  */

33 public class NodeScanner<R,P> extends TreeScanner<R,P> {
34     protected Element currentSym;
35     protected QueryEnvironment env;
36     ASTModel model;
37     
38     /**
39      * Initialize a new Query instance. This method is called prior to
40      * processing any source files.
41      */

42     public void init() {
43     }
44
45     /**
46      * Attach this NodeScanner instance to the specified QueryEnvironment
47      * prior to processing one or more source files.
48      */

49     public void attach(QueryEnvironment env) {
50         this.env = env;
51         this.model = ((EngineEnvironment)env).getModel();
52     }
53     
54     /**
55      * Release any instance data created by attach() or the processing of
56      * a set of source files. All references to the attached QueryEnvironment
57      * and its source files must be released.
58      */

59     public void release() {
60         env = null;
61         model = null;
62     }
63
64     /**
65      * Destroy all instance data not released. This is necessary because the Java
66      * reflection support may cache created instances, preventing the session
67      * data from being garbage-collected. After destroy() is invoked, the instance
68      * should be in the same state as when it was first instantiated.
69      */

70     public void destroy() {
71     }
72
73     /** Visitor method: Scan a single node.
74      */

75     public R scan(Tree tree, P p) {
76         assert model != null : "NodeScanner not attached to CommandEnvironment";
77     R r = null;
78     if(tree != null) {
79             Element sym = model.getElement(tree);
80             if (sym != null) {
81                 Element oldSym = currentSym;
82                 currentSym = sym;
83                 r = tree.accept(this, p);
84                 currentSym = oldSym;
85             }
86             else
87                 r = tree.accept(this, p);
88         }
89         return r;
90     }
91
92     public Element getCurrentElement() {
93     return currentSym;
94     }
95     public void setCurrentElement(Element sym) {
96     currentSym = sym;
97     }
98 }
99
100
Popular Tags