KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.compiler.ast.*;
14
15 public class VariableLocator extends PatternLocator {
16
17 protected VariablePattern pattern;
18
19 public VariableLocator(VariablePattern pattern) {
20     super(pattern);
21
22     this.pattern = pattern;
23 }
24 public int match(Expression node, MatchingNodeSet nodeSet) { // interested in Assignment
25
if (this.pattern.writeAccess) {
26         if (this.pattern.readAccess) return IMPOSSIBLE_MATCH; // already checked the lhs in match(Reference...) before we reached here
27

28         if (node instanceof Assignment) {
29             Expression lhs = ((Assignment) node).lhs;
30             if (lhs instanceof Reference)
31                 return matchReference((Reference) lhs, nodeSet, true);
32         }
33     } else if (this.pattern.readAccess) {
34         if (node instanceof Assignment && !(node instanceof CompoundAssignment)) {
35             // the lhs of a simple assignment may be added in match(Reference...) before we reach here
36
// for example, the fieldRef to 'this.x' in the statement this.x = x; is not considered a readAccess
37
Expression lhs = ((Assignment) node).lhs;
38             nodeSet.removePossibleMatch(lhs);
39             nodeSet.removeTrustedMatch(lhs);
40         }
41     }
42     return IMPOSSIBLE_MATCH;
43 }
44 public int match(Reference node, MatchingNodeSet nodeSet) { // interested in NameReference & its subtypes
45
return this.pattern.readAccess
46         ? matchReference(node, nodeSet, false)
47         : IMPOSSIBLE_MATCH;
48 }
49 protected int matchReference(Reference node, MatchingNodeSet nodeSet, boolean writeOnlyAccess) {
50     if (node instanceof NameReference) {
51         if (this.pattern.name == null) {
52             return nodeSet.addMatch(node, ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
53         } else if (node instanceof SingleNameReference) {
54             if (matchesName(this.pattern.name, ((SingleNameReference) node).token))
55                 return nodeSet.addMatch(node, POSSIBLE_MATCH);
56         } else {
57             QualifiedNameReference qNameRef = (QualifiedNameReference) node;
58             char[][] tokens = qNameRef.tokens;
59             if (writeOnlyAccess) {
60                 // in the case of the assigment of a qualified name reference, the match must be on the last token
61
if (matchesName(this.pattern.name, tokens[tokens.length-1]))
62                     return nodeSet.addMatch(node, POSSIBLE_MATCH);
63             } else {
64                 for (int i = 0, max = tokens.length; i < max; i++)
65                     if (matchesName(this.pattern.name, tokens[i]))
66                         return nodeSet.addMatch(node, POSSIBLE_MATCH);
67             }
68         }
69     }
70     return IMPOSSIBLE_MATCH;
71 }
72 public String JavaDoc toString() {
73     return "Locator for " + this.pattern.toString(); //$NON-NLS-1$
74
}
75 }
76
Popular Tags