Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/QueryEngine.java


1   
2   // Transmogrify License
3   // 
4   // Copyright (c) 2001, ThoughtWorks, Inc.
5   // All rights reserved.
6   // Redistribution and use in source and binary forms, with or without
7   // modification, are permitted provided that the following conditions
8   // are met:
9   // - Redistributions of source code must retain the above copyright notice,
10  //   this list of conditions and the following disclaimer.
11  // - Redistributions in binary form must reproduce the above copyright
12  // notice, this list of conditions and the following disclaimer in the
13  // documentation and/or other materials provided with the distribution.
14  // Neither the name of the ThoughtWorks, Inc. nor the names of its
15  // contributors may be used to endorse or promote products derived from this
16  // software without specific prior written permission.
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  
29  package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
30  
31  import java.io.File;
32  import java.util.Iterator;
33  
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  
36  
37  
38  /**
39   * a wrapper around a <code>SymbolTable</code> that makes Definition and
40   * reference lookup easier
41   */
42  public class QueryEngine {
43    private SymbolTable symbolTable;
44    private ScopeIndex index;
45  
46    public QueryEngine(SymbolTable symbolTable) {
47      this.symbolTable = symbolTable;
48      setIndex();
49    }
50  
51    /**
52     * sets the scope index that this <code>QueryEngine</code> uses
53     */
54    private void setIndex() {
55      index = symbolTable.getScopeIndex();
56    }
57  
58    /**
59     * gets a symbol from the associated symbol table
60     *
61     * @param name the name of the symbol to get
62     * @param location the location of that symbol
63     *
64     * @return Object the (possibly null) result of the lookup
65     */
66    public Reference getSymbol(String name, Occurrence location) {
67      Scope enclosingScope = index.lookup(location);
68      Reference result = enclosingScope.getSymbol(name, location);
69  
70      // REDTAG -- for cases like a label on the same line as the
71      //           block it names, e.g. 'bar: for(int i = 0; ...'
72      if (result == null) {
73        result = enclosingScope.getParentScope().getSymbol(name, location);
74      }
75  
76      return result;
77    }
78  
79    /**
80     * gets the definition of the given symbol
81     *
82     * @param name the name of the symbol to consider
83     * @param location the <code>Occurrence</code> that represents the
84     *                 location of the symbol
85     *
86     * @return Definition the (possibly null) result of the lookup
87     */
88    public IDefinition getDefinition(String name, Occurrence location) {
89      Reference symbol = getSymbol(name, location);
90  
91      //if (symbol != null) {
92      //  System.out.println("  found " + name);
93      //}
94      //else {
95      //  System.out.println("  !could not find " + name);
96      //}
97  
98      return resolveDefinition(symbol);
99    }
100 
101   public IDefinition getDefinition(Occurrence location) {
102     IDefinition result = null;
103 
104     SymTabAST node = getWordNodeAtOccurrence(location);
105     if ( node != null ) {
106       result = node.getDefinition();
107     }
108 
109     return result;
110   }
111 
112   private IDefinition resolveDefinition(Reference reference) {
113     IDefinition result = null;
114 
115     if ( reference != null ) {
116       result = reference.getDefinition();
117     }
118 
119     return result;
120   }
121 
122   /**
123    * gets a collection of references determined by a symbol and location
124    *
125    * @param name the name of the symbol to consider
126    * @param location the <code>Occurrence</code> that represents its location
127    *
128    * @return
129    */
130   public Iterator getReferences(String name, Occurrence location) {
131     Reference symbol = getSymbol(name, location);
132     return resolveReferences(symbol);
133   }
134 
135   public Iterator getReferences(Occurrence location) {
136     Iterator result = null;
137 
138     SymTabAST node = getWordNodeAtOccurrence(location);
139     if ( node != null && node.getDefinition() != null ) {
140       result = node.getDefinition().getReferences();
141     }
142 
143     return result;
144   }
145 
146   private Iterator resolveReferences(Reference reference) {
147     return reference.getDefinition().getReferences();
148   }
149 
150   public SymTabAST getFileNode(File file) {
151     return ASTUtil.getFileNode(symbolTable.getTree(), file);
152   }
153 
154   private SymTabAST getWordNodeAtOccurrence(Occurrence location) {
155     SymTabAST result = null;
156 
157     SymTabAST fileNode = getFileNode(location.getFile());
158     if ( fileNode != null ) {
159       SymTabAST node = fileNode.getEnclosingNode(location.getLine(),
160                                                  location.getColumn());
161 
162       if ( (node != null) && (node.getType() == TokenTypes.IDENT) ) {
163         result = node;
164       }
165     }
166 
167     return result;
168   }
169 
170   public String getWordAtOccurrence(Occurrence location ) {
171     String result = null;
172 
173     SymTabAST node = getWordNodeAtOccurrence(location);
174     if ( node != null ) {
175       result = node.getText();
176     }
177 
178     return result;
179   }
180 
181 }
182 
183 
184 
185