KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.compiler.ast.*;
16 import org.eclipse.jdt.internal.compiler.lookup.Binding;
17 import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
18 import org.eclipse.jdt.internal.core.LocalVariable;
19
20 public class LocalVariableLocator extends VariableLocator {
21
22 public LocalVariableLocator(LocalVariablePattern pattern) {
23     super(pattern);
24 }
25 public int match(LocalDeclaration node, MatchingNodeSet nodeSet) {
26     int referencesLevel = IMPOSSIBLE_MATCH;
27     if (this.pattern.findReferences)
28         // must be a write only access with an initializer
29
if (this.pattern.writeAccess && !this.pattern.readAccess && node.initialization != null)
30             if (matchesName(this.pattern.name, node.name))
31                 referencesLevel = ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
32
33     int declarationsLevel = IMPOSSIBLE_MATCH;
34     if (this.pattern.findDeclarations)
35         if (matchesName(this.pattern.name, node.name))
36             if (node.declarationSourceStart == getLocalVariable().declarationSourceStart)
37                 declarationsLevel = ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
38
39     return nodeSet.addMatch(node, referencesLevel >= declarationsLevel ? referencesLevel : declarationsLevel); // use the stronger match
40
}
41 private LocalVariable getLocalVariable() {
42     return ((LocalVariablePattern) this.pattern).localVariable;
43 }
44 protected void matchReportReference(ASTNode reference, IJavaElement element, Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException {
45     int offset = -1;
46     int length = -1;
47     if (reference instanceof SingleNameReference) {
48         offset = reference.sourceStart;
49         length = reference.sourceEnd-offset+1;
50     } else if (reference instanceof QualifiedNameReference) {
51         QualifiedNameReference qNameRef = (QualifiedNameReference) reference;
52         long sourcePosition = qNameRef.sourcePositions[0];
53         offset = (int) (sourcePosition >>> 32);
54         length = ((int) sourcePosition) - offset +1;
55     } else if (reference instanceof LocalDeclaration) {
56         LocalVariable localVariable = getLocalVariable();
57         offset = localVariable.nameStart;
58         length = localVariable.nameEnd-offset+1;
59         element = localVariable;
60     }
61     if (offset >= 0) {
62         match = locator.newLocalVariableReferenceMatch(element, accuracy, offset, length, reference);
63         locator.report(match);
64     }
65 }
66 protected int matchContainer() {
67     return METHOD_CONTAINER;
68 }
69 protected int matchLocalVariable(LocalVariableBinding variable, boolean matchName) {
70     if (variable == null) return INACCURATE_MATCH;
71
72     if (matchName && !matchesName(this.pattern.name, variable.readableName())) return IMPOSSIBLE_MATCH;
73
74     return variable.declaration.declarationSourceStart == getLocalVariable().declarationSourceStart
75         ? ACCURATE_MATCH
76         : IMPOSSIBLE_MATCH;
77 }
78 protected int referenceType() {
79     return IJavaElement.LOCAL_VARIABLE;
80 }
81 public int resolveLevel(ASTNode possiblelMatchingNode) {
82     if (this.pattern.findReferences)
83         if (possiblelMatchingNode instanceof NameReference)
84             return resolveLevel((NameReference) possiblelMatchingNode);
85     if (possiblelMatchingNode instanceof LocalDeclaration)
86         return matchLocalVariable(((LocalDeclaration) possiblelMatchingNode).binding, true);
87     return IMPOSSIBLE_MATCH;
88 }
89 public int resolveLevel(Binding binding) {
90     if (binding == null) return INACCURATE_MATCH;
91     if (!(binding instanceof LocalVariableBinding)) return IMPOSSIBLE_MATCH;
92
93     return matchLocalVariable((LocalVariableBinding) binding, true);
94 }
95 protected int resolveLevel(NameReference nameRef) {
96     return resolveLevel(nameRef.binding);
97 }
98 }
99
Popular Tags