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.SearchMatch;
015: import org.eclipse.jdt.core.search.SearchPattern;
016: import org.eclipse.jdt.internal.compiler.ast.ASTNode;
017: import org.eclipse.jdt.internal.compiler.ast.Annotation;
018: import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
019: import org.eclipse.jdt.internal.compiler.ast.Expression;
020: import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
021: import org.eclipse.jdt.internal.compiler.ast.ImportReference;
022: import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
023: import org.eclipse.jdt.internal.compiler.ast.MemberValuePair;
024: import org.eclipse.jdt.internal.compiler.ast.MessageSend;
025: import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
026: import org.eclipse.jdt.internal.compiler.ast.Reference;
027: import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
028: import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
029: import org.eclipse.jdt.internal.compiler.ast.TypeReference;
030: import org.eclipse.jdt.internal.compiler.lookup.Binding;
031: import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
032: import org.eclipse.jdt.internal.compiler.lookup.MemberTypeBinding;
033: import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
034:
035: public class OrLocator extends PatternLocator {
036:
037: protected PatternLocator[] patternLocators;
038:
039: public OrLocator(OrPattern pattern) {
040: super (pattern);
041:
042: SearchPattern[] patterns = pattern.patterns;
043: int length = patterns.length;
044: this .patternLocators = new PatternLocator[length];
045: for (int i = 0; i < length; i++)
046: this .patternLocators[i] = PatternLocator
047: .patternLocator(patterns[i]);
048: }
049:
050: public void initializePolymorphicSearch(MatchLocator locator) {
051: for (int i = 0, length = this .patternLocators.length; i < length; i++)
052: this .patternLocators[i]
053: .initializePolymorphicSearch(locator);
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 = 0;
228: for (int i = 0, length = this .patternLocators.length; i < length; i++)
229: result |= this .patternLocators[i].matchContainer();
230: return result;
231: }
232:
233: protected void matchLevelAndReportImportRef(
234: ImportReference importRef, Binding binding,
235: MatchLocator locator) throws CoreException {
236:
237: // for static import, binding can be a field binding or a member type binding
238: // verify that in this case binding is static and use declaring class for fields
239: Binding refBinding = binding;
240: if (importRef.isStatic()) {
241: if (binding instanceof FieldBinding) {
242: FieldBinding fieldBinding = (FieldBinding) binding;
243: if (!fieldBinding.isStatic())
244: return;
245: refBinding = fieldBinding.declaringClass;
246: } else if (binding instanceof MethodBinding) {
247: MethodBinding methodBinding = (MethodBinding) binding;
248: if (!methodBinding.isStatic())
249: return;
250: refBinding = methodBinding.declaringClass;
251: } else if (binding instanceof MemberTypeBinding) {
252: MemberTypeBinding memberBinding = (MemberTypeBinding) binding;
253: if (!memberBinding.isStatic())
254: return;
255: }
256: }
257:
258: // Look for closest pattern
259: PatternLocator closestPattern = null;
260: int level = IMPOSSIBLE_MATCH;
261: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
262: PatternLocator patternLocator = this .patternLocators[i];
263: int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH
264: : patternLocator.resolveLevel(refBinding);
265: if (newLevel > level) {
266: closestPattern = patternLocator;
267: if (newLevel == ACCURATE_MATCH)
268: break;
269: level = newLevel;
270: }
271: }
272: if (closestPattern != null) {
273: closestPattern.matchLevelAndReportImportRef(importRef,
274: binding, locator);
275: }
276: }
277:
278: protected void matchReportImportRef(ImportReference importRef,
279: Binding binding, IJavaElement element, int accuracy,
280: MatchLocator locator) throws CoreException {
281: PatternLocator closestPattern = null;
282: int level = IMPOSSIBLE_MATCH;
283: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
284: int newLevel = this .patternLocators[i]
285: .matchLevel(importRef);
286: if (newLevel > level) {
287: closestPattern = this .patternLocators[i];
288: if (newLevel == ACCURATE_MATCH)
289: break;
290: level = newLevel;
291: }
292: }
293: if (closestPattern != null)
294: closestPattern.matchReportImportRef(importRef, binding,
295: element, accuracy, locator);
296: }
297:
298: protected void matchReportReference(ASTNode reference,
299: IJavaElement element, Binding elementBinding, int accuracy,
300: MatchLocator locator) throws CoreException {
301: PatternLocator closestPattern = null;
302: int level = IMPOSSIBLE_MATCH;
303: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
304: PatternLocator patternLocator = this .patternLocators[i];
305: int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH
306: : patternLocator.resolveLevel(reference);
307: if (newLevel > level) {
308: closestPattern = patternLocator;
309: if (newLevel == ACCURATE_MATCH)
310: break;
311: level = newLevel;
312: }
313: }
314: if (closestPattern != null)
315: closestPattern.matchReportReference(reference, element,
316: elementBinding, accuracy, locator);
317: }
318:
319: public SearchMatch newDeclarationMatch(ASTNode reference,
320: IJavaElement element, Binding elementBinding, int accuracy,
321: int length, MatchLocator locator) {
322: PatternLocator closestPattern = null;
323: int level = IMPOSSIBLE_MATCH;
324: for (int i = 0, pl = this .patternLocators.length; i < pl; i++) {
325: PatternLocator patternLocator = this .patternLocators[i];
326: int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH
327: : patternLocator.resolveLevel(reference);
328: if (newLevel > level) {
329: closestPattern = patternLocator;
330: if (newLevel == ACCURATE_MATCH)
331: break;
332: level = newLevel;
333: }
334: }
335: if (closestPattern != null) {
336: return closestPattern.newDeclarationMatch(reference,
337: element, elementBinding, accuracy, length, locator);
338: }
339: // super implementation...
340: return locator.newDeclarationMatch(element, elementBinding,
341: accuracy, reference.sourceStart, length);
342: }
343:
344: public int resolveLevel(ASTNode node) {
345: int level = IMPOSSIBLE_MATCH;
346: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
347: int newLevel = this .patternLocators[i].resolveLevel(node);
348: if (newLevel > level) {
349: if (newLevel == ACCURATE_MATCH)
350: return ACCURATE_MATCH;
351: level = newLevel; // want to answer the stronger match
352: }
353: }
354: return level;
355: }
356:
357: public int resolveLevel(Binding binding) {
358: int level = IMPOSSIBLE_MATCH;
359: for (int i = 0, length = this .patternLocators.length; i < length; i++) {
360: int newLevel = this .patternLocators[i]
361: .resolveLevel(binding);
362: if (newLevel > level) {
363: if (newLevel == ACCURATE_MATCH)
364: return ACCURATE_MATCH;
365: level = newLevel; // want to answer the stronger match
366: }
367: }
368: return level;
369: }
370: }
|