Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/ScopeIndex.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.Hashtable;
33 import java.util.Vector;
34
35
36
37
38 /**
39 * <code>ScopeIndex</code> provides methods for finding <code>Scope</code>s
40 * related to a known <code>Occurrence</code>
41 */
42 public class ScopeIndex {
43
44 //This is a Hashtable full of Vectors. The keys to this hashtable are filenames.
45 //Each vector contains all of the scope objects from the specific file.
46 private Hashtable indexOfFiles = new Hashtable();
47
48 public Hashtable getIndex() {
49 return indexOfFiles;
50 }
51
52 /**
53 * returns the most specific <code>Scope</code> to which the specified
54 * <code>Occurence</code> belongs.
55 *
56 * @param occ the <code>Occurrence</code> whose <code>Scope</code> we're interested in.
57 * @return Scope
58 */
59 public Scope lookup(Occurrence occ) {
60 String key = occ.getFile().getAbsolutePath();
61 Vector scopeList = getFileVector(key);
62
63 Scope result = findScope(scopeList, occ);
64
65 return result;
66 }
67
68 /**
69 * returns the most specific <code>Scope</code> to which the specified
70 * <code>Occurence</code> belongs from the specified <code>Vector</code>
71 * of <code>Scope</code>s.
72 *
73 * @param occ the <code>Occurrence</code> whose <code>Scope</code> we're interested in.
74 * @param scopeList the <code>Vector</code> of <code>Scope</code>s to chose from.
75 * @return Scope
76 */
77 public Scope findScope(Vector scopeList, Occurrence occ) {
78 int i = 0;
79
80 Scope bestSoFar = (Scope) scopeList.elementAt(i);
81
82 while (!bestSoFar
83 .getTreeNode()
84 .getSpan()
85 .contains(occ.getLine(), occ.getColumn())) {
86 i++;
87 bestSoFar = (Scope) scopeList.elementAt(i);
88 }
89
90 for (; i < scopeList.size(); i++) {
91 Scope currentScope = (Scope) scopeList.elementAt(i);
92
93 if (currentScope
94 .getTreeNode()
95 .getSpan()
96 .contains(occ.getLine(), occ.getColumn())) {
97 if (bestSoFar
98 .getTreeNode()
99 .getSpan()
100 .contains(currentScope.getTreeNode().getSpan())) {
101 bestSoFar = currentScope;
102 }
103 }
104 }
105
106 return bestSoFar;
107 }
108
109 /**
110 * adds a <code>Scope</code> to the <code>ScopeIndex</code> for searching.
111 *
112 * @param scope the <code>Scope</code> to add.
113 */
114 public void addScope(Scope scope) {
115
116 final SymTabAST SymTabAST = scope.getTreeNode();
117 final File file = SymTabAST.getFile();
118 Vector fileVector =
119 getFileVector(scope.getTreeNode().getFile().getAbsolutePath());
120
121 fileVector.addElement(scope);
122 }
123
124 /**
125 * returns the <code>Vector</code> containing the <code>Scope</code>s related
126 * to the specified filename.
127 *
128 * @param fileName the fileName to find scopes for.
129 * @return Vector
130 */
131 private Vector getFileVector(String fileName) {
132 Vector result = (Vector) indexOfFiles.get(fileName);
133
134 if (result == null) {
135 result = new Vector();
136 indexOfFiles.put(fileName, result);
137 }
138
139 return result;
140 }
141 }