01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.core.search.matching;
11:
12: import org.eclipse.jdt.internal.compiler.ast.*;
13:
14: public class VariableLocator extends PatternLocator {
15:
16: protected VariablePattern pattern;
17:
18: public VariableLocator(VariablePattern pattern) {
19: super (pattern);
20:
21: this .pattern = pattern;
22: }
23:
24: public int match(Expression node, MatchingNodeSet nodeSet) { // interested in Assignment
25: if (this .pattern.writeAccess) {
26: if (this .pattern.readAccess)
27: return IMPOSSIBLE_MATCH; // already checked the lhs in match(Reference...) before we reached here
28:
29: if (node instanceof Assignment) {
30: Expression lhs = ((Assignment) node).lhs;
31: if (lhs instanceof Reference)
32: return matchReference((Reference) lhs, nodeSet,
33: true);
34: }
35: } else if (this .pattern.readAccess) {
36: if (node instanceof Assignment
37: && !(node instanceof CompoundAssignment)) {
38: // the lhs of a simple assignment may be added in match(Reference...) before we reach here
39: // for example, the fieldRef to 'this.x' in the statement this.x = x; is not considered a readAccess
40: Expression lhs = ((Assignment) node).lhs;
41: nodeSet.removePossibleMatch(lhs);
42: nodeSet.removeTrustedMatch(lhs);
43: }
44: }
45: return IMPOSSIBLE_MATCH;
46: }
47:
48: public int match(Reference node, MatchingNodeSet nodeSet) { // interested in NameReference & its subtypes
49: return this .pattern.readAccess ? matchReference(node, nodeSet,
50: false) : IMPOSSIBLE_MATCH;
51: }
52:
53: protected int matchReference(Reference node,
54: MatchingNodeSet nodeSet, boolean writeOnlyAccess) {
55: if (node instanceof NameReference) {
56: if (this .pattern.name == null) {
57: return nodeSet
58: .addMatch(
59: node,
60: ((InternalSearchPattern) this .pattern).mustResolve ? POSSIBLE_MATCH
61: : ACCURATE_MATCH);
62: } else if (node instanceof SingleNameReference) {
63: if (matchesName(this .pattern.name,
64: ((SingleNameReference) node).token))
65: return nodeSet.addMatch(node, POSSIBLE_MATCH);
66: } else {
67: QualifiedNameReference qNameRef = (QualifiedNameReference) node;
68: char[][] tokens = qNameRef.tokens;
69: if (writeOnlyAccess) {
70: // in the case of the assigment of a qualified name reference, the match must be on the last token
71: if (matchesName(this .pattern.name,
72: tokens[tokens.length - 1]))
73: return nodeSet.addMatch(node, POSSIBLE_MATCH);
74: } else {
75: for (int i = 0, max = tokens.length; i < max; i++)
76: if (matchesName(this .pattern.name, tokens[i]))
77: return nodeSet.addMatch(node,
78: POSSIBLE_MATCH);
79: }
80: }
81: }
82: return IMPOSSIBLE_MATCH;
83: }
84:
85: public String toString() {
86: return "Locator for " + this .pattern.toString(); //$NON-NLS-1$
87: }
88: }
|