001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.search.matching;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jdt.core.IJavaElement;
014: import org.eclipse.jdt.core.search.SearchPattern;
015: import org.eclipse.jdt.internal.compiler.ast.ASTNode;
016: import org.eclipse.jdt.internal.compiler.ast.Annotation;
017: import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
018: import org.eclipse.jdt.internal.compiler.ast.Expression;
019: import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
020: import org.eclipse.jdt.internal.compiler.ast.ImportReference;
021: import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
022: import org.eclipse.jdt.internal.compiler.ast.MemberValuePair;
023: import org.eclipse.jdt.internal.compiler.ast.MessageSend;
024: import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
025: import org.eclipse.jdt.internal.compiler.ast.Reference;
026: import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
027: import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
028: import org.eclipse.jdt.internal.compiler.ast.TypeReference;
029: import org.eclipse.jdt.internal.compiler.lookup.Binding;
030:
031: public class AndLocator extends PatternLocator {
032:
033: final PatternLocator[] patternLocators;
034: final int[] levels;
035:
036: public AndLocator(AndPattern pattern) {
037: super (pattern);
038:
039: SearchPattern[] patterns = pattern.patterns;
040: PatternLocator[] locators = new PatternLocator[patterns.length];
041: this .levels = new int[patterns.length];
042: for (int i = 0, l = patterns.length; i < l; i++) {
043: locators[i] = PatternLocator.patternLocator(patterns[i]);
044: this .levels[i] = IMPOSSIBLE_MATCH;
045: }
046: this .patternLocators = locators;
047: }
048:
049: public void initializePolymorphicSearch(MatchLocator locator) {
050: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
051: this .patternLocators[i]
052: .initializePolymorphicSearch(locator);
053: }
054: }
055:
056: public int match(Annotation node, MatchingNodeSet nodeSet) {
057: int level = IMPOSSIBLE_MATCH;
058: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
059: int newLevel = this .patternLocators[i].match(node, nodeSet);
060: if (newLevel > level) {
061: if (newLevel == ACCURATE_MATCH)
062: return ACCURATE_MATCH;
063: level = newLevel;
064: }
065: }
066: return level;
067: }
068:
069: public int match(ASTNode node, MatchingNodeSet nodeSet) {
070: int level = IMPOSSIBLE_MATCH;
071: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
072: int newLevel = this .patternLocators[i].match(node, nodeSet);
073: if (newLevel > level) {
074: if (newLevel == ACCURATE_MATCH)
075: return ACCURATE_MATCH;
076: level = newLevel;
077: }
078: }
079: return level;
080: }
081:
082: public int match(ConstructorDeclaration node,
083: MatchingNodeSet nodeSet) {
084: int level = IMPOSSIBLE_MATCH;
085: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
086: int newLevel = this .patternLocators[i].match(node, nodeSet);
087: if (newLevel > level) {
088: if (newLevel == ACCURATE_MATCH)
089: return ACCURATE_MATCH;
090: level = newLevel;
091: }
092: }
093: return level;
094: }
095:
096: public int match(Expression node, MatchingNodeSet nodeSet) {
097: int level = IMPOSSIBLE_MATCH;
098: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
099: int newLevel = this .patternLocators[i].match(node, nodeSet);
100: if (newLevel > level) {
101: if (newLevel == ACCURATE_MATCH)
102: return ACCURATE_MATCH;
103: level = newLevel;
104: }
105: }
106: return level;
107: }
108:
109: public int match(FieldDeclaration node, MatchingNodeSet nodeSet) {
110: int level = IMPOSSIBLE_MATCH;
111: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
112: int newLevel = this .patternLocators[i].match(node, nodeSet);
113: if (newLevel > level) {
114: if (newLevel == ACCURATE_MATCH)
115: return ACCURATE_MATCH;
116: level = newLevel;
117: }
118: }
119: return level;
120: }
121:
122: public int match(LocalDeclaration node, MatchingNodeSet nodeSet) {
123: int level = IMPOSSIBLE_MATCH;
124: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
125: int newLevel = this .patternLocators[i].match(node, nodeSet);
126: if (newLevel > level) {
127: if (newLevel == ACCURATE_MATCH)
128: return ACCURATE_MATCH;
129: level = newLevel;
130: }
131: }
132: return level;
133: }
134:
135: public int match(MethodDeclaration node, MatchingNodeSet nodeSet) {
136: int level = IMPOSSIBLE_MATCH;
137: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
138: int newLevel = this .patternLocators[i].match(node, nodeSet);
139: if (newLevel > level) {
140: if (newLevel == ACCURATE_MATCH)
141: return ACCURATE_MATCH;
142: level = newLevel;
143: }
144: }
145: return level;
146: }
147:
148: public int match(MemberValuePair node, MatchingNodeSet nodeSet) {
149: int level = IMPOSSIBLE_MATCH;
150: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
151: int newLevel = this .patternLocators[i].match(node, nodeSet);
152: if (newLevel > level) {
153: if (newLevel == ACCURATE_MATCH)
154: return ACCURATE_MATCH;
155: level = newLevel;
156: }
157: }
158: return level;
159: }
160:
161: public int match(MessageSend node, MatchingNodeSet nodeSet) {
162: int level = IMPOSSIBLE_MATCH;
163: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
164: int newLevel = this .patternLocators[i].match(node, nodeSet);
165: if (newLevel > level) {
166: if (newLevel == ACCURATE_MATCH)
167: return ACCURATE_MATCH;
168: level = newLevel;
169: }
170: }
171: return level;
172: }
173:
174: public int match(Reference node, MatchingNodeSet nodeSet) {
175: int level = IMPOSSIBLE_MATCH;
176: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
177: int newLevel = this .patternLocators[i].match(node, nodeSet);
178: if (newLevel > level) {
179: if (newLevel == ACCURATE_MATCH)
180: return ACCURATE_MATCH;
181: level = newLevel;
182: }
183: }
184: return level;
185: }
186:
187: public int match(TypeDeclaration node, MatchingNodeSet nodeSet) {
188: int level = IMPOSSIBLE_MATCH;
189: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
190: int newLevel = this .patternLocators[i].match(node, nodeSet);
191: if (newLevel > level) {
192: if (newLevel == ACCURATE_MATCH)
193: return ACCURATE_MATCH;
194: level = newLevel;
195: }
196: }
197: return level;
198: }
199:
200: public int match(TypeParameter node, MatchingNodeSet nodeSet) {
201: int level = IMPOSSIBLE_MATCH;
202: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
203: int newLevel = this .patternLocators[i].match(node, nodeSet);
204: if (newLevel > level) {
205: if (newLevel == ACCURATE_MATCH)
206: return ACCURATE_MATCH;
207: level = newLevel;
208: }
209: }
210: return level;
211: }
212:
213: public int match(TypeReference node, MatchingNodeSet nodeSet) {
214: int level = IMPOSSIBLE_MATCH;
215: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
216: int newLevel = this .patternLocators[i].match(node, nodeSet);
217: if (newLevel > level) {
218: if (newLevel == ACCURATE_MATCH)
219: return ACCURATE_MATCH;
220: level = newLevel;
221: }
222: }
223: return level;
224: }
225:
226: protected int matchContainer() {
227: int result = ALL_CONTAINER;
228: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
229: result &= this .patternLocators[i].matchContainer();
230: }
231: return result;
232: }
233:
234: protected void matchReportImportRef(ImportReference importRef,
235: Binding binding, IJavaElement element, int accuracy,
236: MatchLocator locator) throws CoreException {
237: PatternLocator weakestPattern = null;
238: int level = IMPOSSIBLE_MATCH;
239: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
240: int newLevel = this .patternLocators[i]
241: .matchLevel(importRef);
242: if (newLevel == IMPOSSIBLE_MATCH)
243: return;
244: if (weakestPattern == null || newLevel < level) {
245: weakestPattern = this .patternLocators[i];
246: level = newLevel;
247: }
248: }
249: weakestPattern.matchReportImportRef(importRef, binding,
250: element, accuracy, locator);
251: }
252:
253: protected void matchReportReference(ASTNode reference,
254: IJavaElement element, Binding elementBinding, int accuracy,
255: MatchLocator locator) throws CoreException {
256: PatternLocator weakestPattern = null;
257: int level = IMPOSSIBLE_MATCH;
258: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
259: if (this .patternLocators[i].referenceType() == 0)
260: return; // impossible match
261: int newLevel = this .patternLocators[i]
262: .resolveLevel(reference);
263: if (newLevel == IMPOSSIBLE_MATCH)
264: return;
265: if (weakestPattern == null || newLevel < level) {
266: weakestPattern = this .patternLocators[i];
267: level = newLevel;
268: }
269: }
270: weakestPattern.matchReportReference(reference, element,
271: elementBinding, accuracy, locator);
272: }
273:
274: public int resolveLevel(ASTNode node) {
275: int level = ACCURATE_MATCH;
276: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
277: int newLevel = this .patternLocators[i].resolveLevel(node);
278: if (newLevel == IMPOSSIBLE_MATCH)
279: return IMPOSSIBLE_MATCH;
280: this .levels[i] = newLevel;
281: if (newLevel < level) {
282: level = newLevel; // want to answer the weaker match
283: }
284: }
285: return level;
286: }
287:
288: public int resolveLevel(Binding binding) {
289: int level = ACCURATE_MATCH;
290: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
291: int newLevel = this .patternLocators[i]
292: .resolveLevel(binding);
293: if (newLevel == IMPOSSIBLE_MATCH)
294: return IMPOSSIBLE_MATCH;
295: this .levels[i] = newLevel;
296: if (newLevel < level) {
297: level = newLevel; // want to answer the weaker match
298: }
299: }
300: return level;
301: }
302: }
|