KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > OrLocator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search.matching;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.search.SearchMatch;
16 import org.eclipse.jdt.core.search.SearchPattern;
17 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
18 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
19 import org.eclipse.jdt.internal.compiler.ast.Expression;
20 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.ImportReference;
22 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
23 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
24 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
25 import org.eclipse.jdt.internal.compiler.ast.Reference;
26 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
27 import org.eclipse.jdt.internal.compiler.ast.TypeReference;
28 import org.eclipse.jdt.internal.compiler.lookup.Binding;
29 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
30 import org.eclipse.jdt.internal.compiler.lookup.MemberTypeBinding;
31 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
32
33 public class OrLocator extends PatternLocator {
34
35 protected PatternLocator[] patternLocators;
36
37 public OrLocator(OrPattern pattern) {
38     super(pattern);
39
40     SearchPattern[] patterns = pattern.patterns;
41     int length = patterns.length;
42     this.patternLocators = new PatternLocator[length];
43     for (int i = 0; i < length; i++)
44         this.patternLocators[i] = PatternLocator.patternLocator(patterns[i]);
45 }
46 public void initializePolymorphicSearch(MatchLocator locator) {
47     for (int i = 0, length = this.patternLocators.length; i < length; i++)
48         this.patternLocators[i].initializePolymorphicSearch(locator);
49 }
50 public int match(ASTNode node, MatchingNodeSet nodeSet) {
51     int level = IMPOSSIBLE_MATCH;
52     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
53         int newLevel = this.patternLocators[i].match(node, nodeSet);
54         if (newLevel > level) {
55             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
56             level = newLevel;
57         }
58     }
59     return level;
60 }
61 public int match(ConstructorDeclaration node, MatchingNodeSet nodeSet) {
62     int level = IMPOSSIBLE_MATCH;
63     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
64         int newLevel = this.patternLocators[i].match(node, nodeSet);
65         if (newLevel > level) {
66             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
67             level = newLevel;
68         }
69     }
70     return level;
71 }
72 public int match(Expression node, MatchingNodeSet nodeSet) {
73     int level = IMPOSSIBLE_MATCH;
74     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
75         int newLevel = this.patternLocators[i].match(node, nodeSet);
76         if (newLevel > level) {
77             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
78             level = newLevel;
79         }
80     }
81     return level;
82 }
83 public int match(FieldDeclaration node, MatchingNodeSet nodeSet) {
84     int level = IMPOSSIBLE_MATCH;
85     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
86         int newLevel = this.patternLocators[i].match(node, nodeSet);
87         if (newLevel > level) {
88             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
89             level = newLevel;
90         }
91     }
92     return level;
93 }
94 public int match(LocalDeclaration node, MatchingNodeSet nodeSet) {
95     int level = IMPOSSIBLE_MATCH;
96     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
97         int newLevel = this.patternLocators[i].match(node, nodeSet);
98         if (newLevel > level) {
99             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
100             level = newLevel;
101         }
102     }
103     return level;
104 }
105 public int match(MethodDeclaration node, MatchingNodeSet nodeSet) {
106     int level = IMPOSSIBLE_MATCH;
107     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
108         int newLevel = this.patternLocators[i].match(node, nodeSet);
109         if (newLevel > level) {
110             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
111             level = newLevel;
112         }
113     }
114     return level;
115 }
116 public int match(MessageSend node, MatchingNodeSet nodeSet) {
117     int level = IMPOSSIBLE_MATCH;
118     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
119         int newLevel = this.patternLocators[i].match(node, nodeSet);
120         if (newLevel > level) {
121             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
122             level = newLevel;
123         }
124     }
125     return level;
126 }
127 public int match(Reference node, MatchingNodeSet nodeSet) {
128     int level = IMPOSSIBLE_MATCH;
129     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
130         int newLevel = this.patternLocators[i].match(node, nodeSet);
131         if (newLevel > level) {
132             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
133             level = newLevel;
134         }
135     }
136     return level;
137 }
138 public int match(TypeDeclaration node, MatchingNodeSet nodeSet) {
139     int level = IMPOSSIBLE_MATCH;
140     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
141         int newLevel = this.patternLocators[i].match(node, nodeSet);
142         if (newLevel > level) {
143             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
144             level = newLevel;
145         }
146     }
147     return level;
148 }
149 public int match(TypeReference node, MatchingNodeSet nodeSet) {
150     int level = IMPOSSIBLE_MATCH;
151     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
152         int newLevel = this.patternLocators[i].match(node, nodeSet);
153         if (newLevel > level) {
154             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
155             level = newLevel;
156         }
157     }
158     return level;
159 }
160 protected int matchContainer() {
161     int result = 0;
162     for (int i = 0, length = this.patternLocators.length; i < length; i++)
163         result |= this.patternLocators[i].matchContainer();
164     return result;
165 }
166 protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator) throws CoreException {
167     
168     // for static import, binding can be a field binding or a member type binding
169
// verify that in this case binding is static and use declaring class for fields
170
Binding refBinding = binding;
171     if (importRef.isStatic()) {
172         if (binding instanceof FieldBinding) {
173             FieldBinding fieldBinding = (FieldBinding) binding;
174             if (!fieldBinding.isStatic()) return;
175             refBinding = fieldBinding.declaringClass;
176         } else if (binding instanceof MethodBinding) {
177             MethodBinding methodBinding = (MethodBinding) binding;
178             if (!methodBinding.isStatic()) return;
179             refBinding = methodBinding.declaringClass;
180         } else if (binding instanceof MemberTypeBinding) {
181             MemberTypeBinding memberBinding = (MemberTypeBinding) binding;
182             if (!memberBinding.isStatic()) return;
183         }
184     }
185     
186     // Look for closest pattern
187
PatternLocator closestPattern = null;
188     int level = IMPOSSIBLE_MATCH;
189     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
190         PatternLocator patternLocator = this.patternLocators[i];
191         int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH : patternLocator.resolveLevel(refBinding);
192         if (newLevel > level) {
193             closestPattern = patternLocator;
194             if (newLevel == ACCURATE_MATCH) break;
195             level = newLevel;
196         }
197     }
198     if (closestPattern != null) {
199         closestPattern.matchLevelAndReportImportRef(importRef, binding, locator);
200     }
201 }
202 protected void matchReportImportRef(ImportReference importRef, Binding binding, IJavaElement element, int accuracy, MatchLocator locator) throws CoreException {
203     PatternLocator closestPattern = null;
204     int level = IMPOSSIBLE_MATCH;
205     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
206         int newLevel = this.patternLocators[i].matchLevel(importRef);
207         if (newLevel > level) {
208             closestPattern = this.patternLocators[i];
209             if (newLevel == ACCURATE_MATCH) break;
210             level = newLevel;
211         }
212     }
213     if (closestPattern != null)
214         closestPattern.matchReportImportRef(importRef, binding, element, accuracy, locator);
215 }
216 protected void matchReportReference(ASTNode reference, IJavaElement element, Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException {
217     PatternLocator closestPattern = null;
218     int level = IMPOSSIBLE_MATCH;
219     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
220         PatternLocator patternLocator = this.patternLocators[i];
221         int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH : patternLocator.resolveLevel(reference);
222         if (newLevel > level) {
223             closestPattern = patternLocator;
224             if (newLevel == ACCURATE_MATCH) break;
225             level = newLevel;
226         }
227     }
228     if (closestPattern != null)
229         closestPattern.matchReportReference(reference, element, elementBinding, accuracy, locator);
230 }
231 public SearchMatch newDeclarationMatch(ASTNode reference, IJavaElement element, Binding elementBinding, int accuracy, int length, MatchLocator locator) {
232     PatternLocator closestPattern = null;
233     int level = IMPOSSIBLE_MATCH;
234     for (int i = 0, pl = this.patternLocators.length; i < pl; i++) {
235         PatternLocator patternLocator = this.patternLocators[i];
236         int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH : patternLocator.resolveLevel(reference);
237         if (newLevel > level) {
238             closestPattern = patternLocator;
239             if (newLevel == ACCURATE_MATCH) break;
240             level = newLevel;
241         }
242     }
243     if (closestPattern != null) {
244         return closestPattern.newDeclarationMatch(reference, element, elementBinding, accuracy, length, locator);
245     }
246     // super implementation...
247
return locator.newDeclarationMatch(element, elementBinding, accuracy, reference.sourceStart, length);
248 }
249 public int resolveLevel(ASTNode node) {
250     int level = IMPOSSIBLE_MATCH;
251     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
252         int newLevel = this.patternLocators[i].resolveLevel(node);
253         if (newLevel > level) {
254             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
255             level = newLevel; // want to answer the stronger match
256
}
257     }
258     return level;
259 }
260 public int resolveLevel(Binding binding) {
261     int level = IMPOSSIBLE_MATCH;
262     for (int i = 0, length = this.patternLocators.length; i < length; i++) {
263         int newLevel = this.patternLocators[i].resolveLevel(binding);
264         if (newLevel > level) {
265             if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
266             level = newLevel; // want to answer the stronger match
267
}
268     }
269     return level;
270 }
271 }
272
Popular Tags