KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.core.IJavaElement;
14 import org.eclipse.jdt.internal.compiler.ast.*;
15 import org.eclipse.jdt.internal.compiler.lookup.Binding;
16 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
17 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
18 import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
19
20 /**
21  * Search engine locator for type parameters matches.
22  */

23 public class TypeParameterLocator extends PatternLocator {
24
25     protected TypeParameterPattern pattern;
26
27     public TypeParameterLocator(TypeParameterPattern pattern) {
28         super(pattern);
29         this.pattern = pattern;
30     }
31
32     /*
33      * Verify whether a type reference matches name pattern.
34      * Type parameter references (ie. type arguments) are compiler type reference nodes
35      */

36     public int match(TypeReference node, MatchingNodeSet nodeSet) {
37         if (pattern.findReferences) {
38             if (node instanceof SingleTypeReference) { // Type parameter cannot be qualified
39
if (matchesName(this.pattern.name, ((SingleTypeReference) node).token)) {
40                     int level = ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
41                     return nodeSet.addMatch(node, level);
42                 }
43             }
44         }
45         return IMPOSSIBLE_MATCH;
46     }
47
48
49     /*
50      * Verify whether a type parameter matches name pattern.
51      */

52     public int match(TypeParameter node, MatchingNodeSet nodeSet) {
53         if (pattern.findDeclarations) {
54             if (matchesName(this.pattern.name, node.name)) {
55                 int level = ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
56                 return nodeSet.addMatch(node, level);
57             }
58         }
59         return IMPOSSIBLE_MATCH;
60     }
61
62     /*
63      * While searching for references, need to match all containers as we can have references in javadoc comments.
64      * Otherwise, only class or method container can declare type parameters.
65      */

66     protected int matchContainer() {
67         if (pattern.findReferences) {
68             return ALL_CONTAINER;
69         }
70         return CLASS_CONTAINER | METHOD_CONTAINER;
71     }
72
73     /*
74      * Verify that a type variable binding match pattern infos.
75      * For types, only look at declaring member name.
76      * For methods, also look at declaring class and parameters type names
77      */

78     protected int matchTypeParameter(TypeVariableBinding variable, boolean matchName) {
79         if (variable == null || variable.declaringElement == null) return INACCURATE_MATCH;
80         if (variable.declaringElement instanceof ReferenceBinding) {
81             ReferenceBinding refBinding = (ReferenceBinding) variable.declaringElement;
82             if (matchesName(refBinding.sourceName, pattern.declaringMemberName)) {
83                 return ACCURATE_MATCH;
84             }
85         } else if (variable.declaringElement instanceof MethodBinding) {
86             MethodBinding methBinding = (MethodBinding) variable.declaringElement;
87             if (matchesName(methBinding.declaringClass.sourceName, pattern.methodDeclaringClassName) &&
88                 (methBinding.isConstructor() || matchesName(methBinding.selector, pattern.declaringMemberName))) {
89                 int length = pattern.methodArgumentTypes==null ? 0 : pattern.methodArgumentTypes.length;
90                 if (methBinding.parameters == null) {
91                     if (length == 0) return ACCURATE_MATCH;
92                 } else if (methBinding.parameters.length == length){
93                     for (int i=0; i<length; i++) {
94                         if (!matchesName(methBinding.parameters[i].shortReadableName(), pattern.methodArgumentTypes[i])) {
95                             return IMPOSSIBLE_MATCH;
96                         }
97                     }
98                     return ACCURATE_MATCH;
99                 }
100             }
101         }
102         return IMPOSSIBLE_MATCH;
103     }
104
105     protected int referenceType() {
106         return IJavaElement.TYPE_PARAMETER;
107     }
108
109     /*
110      * Resolve level for a possible matching node.
111      * Only type references while searching references and type parameters
112      * while searching declarations are valid.
113      */

114     public int resolveLevel(ASTNode possibleMatchingNode) {
115         if (this.pattern.findReferences) {
116             if (possibleMatchingNode instanceof SingleTypeReference) {
117                 return resolveLevel(((SingleTypeReference) possibleMatchingNode).resolvedType);
118             }
119         }
120         if (this.pattern.findDeclarations) {
121             if (possibleMatchingNode instanceof TypeParameter) {
122                 return matchTypeParameter(((TypeParameter) possibleMatchingNode).binding, true);
123             }
124         }
125         return IMPOSSIBLE_MATCH;
126     }
127
128     /*
129      * Resolve level for a binding.
130      * Only type variable bindings are valid.
131      */

132     public int resolveLevel(Binding binding) {
133         if (binding == null) return INACCURATE_MATCH;
134         if (!(binding instanceof TypeVariableBinding)) return IMPOSSIBLE_MATCH;
135     
136         return matchTypeParameter((TypeVariableBinding) binding, true);
137     }
138
139     public String JavaDoc toString() {
140         return "Locator for " + this.pattern.toString(); //$NON-NLS-1$
141
}
142 }
143
Popular Tags