1 11 package org.eclipse.jdt.internal.core.search.matching; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.jdt.core.search.SearchMatch; 16 import org.eclipse.jdt.core.search.SearchPattern; 17 import org.eclipse.jdt.internal.compiler.ast.*; 18 import org.eclipse.jdt.internal.compiler.util.HashtableOfLong; 19 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; 20 import org.eclipse.jdt.internal.compiler.util.SimpleSet; 21 import org.eclipse.jdt.internal.core.util.Util; 22 23 26 public class MatchingNodeSet { 27 28 32 SimpleLookupTable matchingNodes = new SimpleLookupTable(3); private HashtableOfLong matchingNodesKeys = new HashtableOfLong(3); static Integer EXACT_MATCH = new Integer (SearchMatch.A_ACCURATE); 35 static Integer POTENTIAL_MATCH = new Integer (SearchMatch.A_INACCURATE); 36 static Integer ERASURE_MATCH = new Integer (SearchPattern.R_ERASURE_MATCH); 37 38 41 public boolean mustResolve; 42 43 47 SimpleSet possibleMatchingNodesSet = new SimpleSet(7); 48 private HashtableOfLong possibleMatchingNodesKeys = new HashtableOfLong(7); 49 50 51 public MatchingNodeSet(boolean mustResolvePattern) { 52 super(); 53 mustResolve = mustResolvePattern; 54 } 55 56 public int addMatch(ASTNode node, int matchLevel) { 57 int maskedLevel = matchLevel & PatternLocator.MATCH_LEVEL_MASK; 58 switch (maskedLevel) { 59 case PatternLocator.INACCURATE_MATCH: 60 if (matchLevel != maskedLevel) { 61 addTrustedMatch(node, new Integer (SearchMatch.A_INACCURATE+(matchLevel & PatternLocator.FLAVORS_MASK))); 62 } else { 63 addTrustedMatch(node, POTENTIAL_MATCH); 64 } 65 break; 66 case PatternLocator.POSSIBLE_MATCH: 67 addPossibleMatch(node); 68 break; 69 case PatternLocator.ERASURE_MATCH: 70 if (matchLevel != maskedLevel) { 71 addTrustedMatch(node, new Integer (SearchPattern.R_ERASURE_MATCH+(matchLevel & PatternLocator.FLAVORS_MASK))); 72 } else { 73 addTrustedMatch(node, ERASURE_MATCH); 74 } 75 break; 76 case PatternLocator.ACCURATE_MATCH: 77 if (matchLevel != maskedLevel) { 78 addTrustedMatch(node, new Integer (SearchMatch.A_ACCURATE+(matchLevel & PatternLocator.FLAVORS_MASK))); 79 } else { 80 addTrustedMatch(node, EXACT_MATCH); 81 } 82 break; 83 } 84 return matchLevel; 85 } 86 public void addPossibleMatch(ASTNode node) { 87 long key = (((long) node.sourceStart) << 32) + node.sourceEnd; 91 ASTNode existing = (ASTNode) this.possibleMatchingNodesKeys.get(key); 92 if (existing != null && existing.getClass().equals(node.getClass())) 93 this.possibleMatchingNodesSet.remove(existing); 94 95 this.possibleMatchingNodesSet.add(node); 97 this.possibleMatchingNodesKeys.put(key, node); 98 } 99 public void addTrustedMatch(ASTNode node, boolean isExact) { 100 addTrustedMatch(node, isExact ? EXACT_MATCH : POTENTIAL_MATCH); 101 102 } 103 void addTrustedMatch(ASTNode node, Integer level) { 104 long key = (((long) node.sourceStart) << 32) + node.sourceEnd; 108 ASTNode existing = (ASTNode) this.matchingNodesKeys.get(key); 109 if (existing != null && existing.getClass().equals(node.getClass())) 110 this.matchingNodes.removeKey(existing); 111 112 this.matchingNodes.put(node, level); 114 this.matchingNodesKeys.put(key, node); 115 } 116 protected boolean hasPossibleNodes(int start, int end) { 117 Object [] nodes = this.possibleMatchingNodesSet.values; 118 for (int i = 0, l = nodes.length; i < l; i++) { 119 ASTNode node = (ASTNode) nodes[i]; 120 if (node != null && start <= node.sourceStart && node.sourceEnd <= end) 121 return true; 122 } 123 nodes = this.matchingNodes.keyTable; 124 for (int i = 0, l = nodes.length; i < l; i++) { 125 ASTNode node = (ASTNode) nodes[i]; 126 if (node != null && start <= node.sourceStart && node.sourceEnd <= end) 127 return true; 128 } 129 return false; 130 } 131 134 protected ASTNode[] matchingNodes(int start, int end) { 135 ArrayList nodes = null; 136 Object [] keyTable = this.matchingNodes.keyTable; 137 for (int i = 0, l = keyTable.length; i < l; i++) { 138 ASTNode node = (ASTNode) keyTable[i]; 139 if (node != null && start <= node.sourceStart && node.sourceEnd <= end) { 140 if (nodes == null) nodes = new ArrayList (); 141 nodes.add(node); 142 } 143 } 144 if (nodes == null) return null; 145 146 ASTNode[] result = new ASTNode[nodes.size()]; 147 nodes.toArray(result); 148 149 Util.Comparer comparer = new Util.Comparer() { 151 public int compare(Object o1, Object o2) { 152 return ((ASTNode) o1).sourceStart - ((ASTNode) o2).sourceStart; 153 } 154 }; 155 Util.sort(result, comparer); 156 return result; 157 } 158 public Object removePossibleMatch(ASTNode node) { 159 long key = (((long) node.sourceStart) << 32) + node.sourceEnd; 160 ASTNode existing = (ASTNode) this.possibleMatchingNodesKeys.get(key); 161 if (existing == null) return null; 162 163 this.possibleMatchingNodesKeys.put(key, null); 164 return this.possibleMatchingNodesSet.remove(node); 165 } 166 public Object removeTrustedMatch(ASTNode node) { 167 long key = (((long) node.sourceStart) << 32) + node.sourceEnd; 168 ASTNode existing = (ASTNode) this.matchingNodesKeys.get(key); 169 if (existing == null) return null; 170 171 this.matchingNodesKeys.put(key, null); 172 return this.matchingNodes.removeKey(node); 173 } 174 public String toString() { 175 StringBuffer result = new StringBuffer (); 177 result.append("Exact matches:"); Object [] keyTable = this.matchingNodes.keyTable; 179 Object [] valueTable = this.matchingNodes.valueTable; 180 for (int i = 0, l = keyTable.length; i < l; i++) { 181 ASTNode node = (ASTNode) keyTable[i]; 182 if (node == null) continue; 183 result.append("\n\t"); switch (((Integer )valueTable[i]).intValue()) { 185 case SearchMatch.A_ACCURATE: 186 result.append("ACCURATE_MATCH: "); break; 188 case SearchMatch.A_INACCURATE: 189 result.append("INACCURATE_MATCH: "); break; 191 case SearchPattern.R_ERASURE_MATCH: 192 result.append("ERASURE_MATCH: "); break; 194 } 195 node.print(0, result); 196 } 197 198 result.append("\nPossible matches:"); Object [] nodes = this.possibleMatchingNodesSet.values; 200 for (int i = 0, l = nodes.length; i < l; i++) { 201 ASTNode node = (ASTNode) nodes[i]; 202 if (node == null) continue; 203 result.append("\nPOSSIBLE_MATCH: "); node.print(0, result); 205 } 206 return result.toString(); 207 } 208 } 209 | Popular Tags |