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.internal.compiler.ast.*;
015: import org.eclipse.jdt.internal.compiler.lookup.Binding;
016: import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
017: import org.eclipse.jdt.internal.core.LocalVariable;
018:
019: public class LocalVariableLocator extends VariableLocator {
020:
021: public LocalVariableLocator(LocalVariablePattern pattern) {
022: super (pattern);
023: }
024:
025: public int match(LocalDeclaration node, MatchingNodeSet nodeSet) {
026: int referencesLevel = IMPOSSIBLE_MATCH;
027: if (this .pattern.findReferences)
028: // must be a write only access with an initializer
029: if (this .pattern.writeAccess && !this .pattern.readAccess
030: && node.initialization != null)
031: if (matchesName(this .pattern.name, node.name))
032: referencesLevel = ((InternalSearchPattern) this .pattern).mustResolve ? POSSIBLE_MATCH
033: : ACCURATE_MATCH;
034:
035: int declarationsLevel = IMPOSSIBLE_MATCH;
036: if (this .pattern.findDeclarations)
037: if (matchesName(this .pattern.name, node.name))
038: if (node.declarationSourceStart == getLocalVariable().declarationSourceStart)
039: declarationsLevel = ((InternalSearchPattern) this .pattern).mustResolve ? POSSIBLE_MATCH
040: : ACCURATE_MATCH;
041:
042: return nodeSet.addMatch(node,
043: referencesLevel >= declarationsLevel ? referencesLevel
044: : declarationsLevel); // use the stronger match
045: }
046:
047: private LocalVariable getLocalVariable() {
048: return ((LocalVariablePattern) this .pattern).localVariable;
049: }
050:
051: protected void matchReportReference(ASTNode reference,
052: IJavaElement element, Binding elementBinding, int accuracy,
053: MatchLocator locator) throws CoreException {
054: int offset = -1;
055: int length = -1;
056: if (reference instanceof SingleNameReference) {
057: offset = reference.sourceStart;
058: length = reference.sourceEnd - offset + 1;
059: } else if (reference instanceof QualifiedNameReference) {
060: QualifiedNameReference qNameRef = (QualifiedNameReference) reference;
061: long sourcePosition = qNameRef.sourcePositions[0];
062: offset = (int) (sourcePosition >>> 32);
063: length = ((int) sourcePosition) - offset + 1;
064: } else if (reference instanceof LocalDeclaration) {
065: LocalVariable localVariable = getLocalVariable();
066: offset = localVariable.nameStart;
067: length = localVariable.nameEnd - offset + 1;
068: element = localVariable;
069: }
070: if (offset >= 0) {
071: match = locator.newLocalVariableReferenceMatch(element,
072: accuracy, offset, length, reference);
073: locator.report(match);
074: }
075: }
076:
077: protected int matchContainer() {
078: return METHOD_CONTAINER;
079: }
080:
081: protected int matchLocalVariable(LocalVariableBinding variable,
082: boolean matchName) {
083: if (variable == null)
084: return INACCURATE_MATCH;
085:
086: if (matchName
087: && !matchesName(this .pattern.name, variable
088: .readableName()))
089: return IMPOSSIBLE_MATCH;
090:
091: return variable.declaration.declarationSourceStart == getLocalVariable().declarationSourceStart ? ACCURATE_MATCH
092: : IMPOSSIBLE_MATCH;
093: }
094:
095: protected int referenceType() {
096: return IJavaElement.LOCAL_VARIABLE;
097: }
098:
099: public int resolveLevel(ASTNode possiblelMatchingNode) {
100: if (this .pattern.findReferences)
101: if (possiblelMatchingNode instanceof NameReference)
102: return resolveLevel((NameReference) possiblelMatchingNode);
103: if (possiblelMatchingNode instanceof LocalDeclaration)
104: return matchLocalVariable(
105: ((LocalDeclaration) possiblelMatchingNode).binding,
106: true);
107: return IMPOSSIBLE_MATCH;
108: }
109:
110: public int resolveLevel(Binding binding) {
111: if (binding == null)
112: return INACCURATE_MATCH;
113: if (!(binding instanceof LocalVariableBinding))
114: return IMPOSSIBLE_MATCH;
115:
116: return matchLocalVariable((LocalVariableBinding) binding, true);
117: }
118:
119: protected int resolveLevel(NameReference nameRef) {
120: return resolveLevel(nameRef.binding);
121: }
122: }
|