KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
17 import org.eclipse.jdt.internal.core.search.indexing.IIndexConstants;
18
19 public class SuperTypeReferenceLocator extends PatternLocator {
20
21 protected SuperTypeReferencePattern pattern;
22
23 public SuperTypeReferenceLocator(SuperTypeReferencePattern pattern) {
24     super(pattern);
25
26     this.pattern = pattern;
27 }
28 //public int match(ASTNode node, MatchingNodeSet nodeSet) - SKIP IT
29
//public int match(ConstructorDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
30
//public int match(Expression node, MatchingNodeSet nodeSet) - SKIP IT
31
//public int match(FieldDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
32
//public int match(MethodDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
33
//public int match(MessageSend node, MatchingNodeSet nodeSet) - SKIP IT
34
//public int match(Reference node, MatchingNodeSet nodeSet) - SKIP IT
35
//public int match(TypeDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
36
public int match(TypeReference node, MatchingNodeSet nodeSet) {
37     if (this.pattern.superSimpleName == null)
38         return nodeSet.addMatch(node, ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
39
40     char[] typeRefSimpleName = null;
41     if (node instanceof SingleTypeReference) {
42         typeRefSimpleName = ((SingleTypeReference) node).token;
43     } else { // QualifiedTypeReference
44
char[][] tokens = ((QualifiedTypeReference) node).tokens;
45         typeRefSimpleName = tokens[tokens.length-1];
46     }
47     if (matchesName(this.pattern.superSimpleName, typeRefSimpleName))
48         return nodeSet.addMatch(node, ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
49
50     return IMPOSSIBLE_MATCH;
51 }
52
53 protected int matchContainer() {
54     return CLASS_CONTAINER;
55 }
56 /* (non-Javadoc)
57  * @see org.eclipse.jdt.internal.core.search.matching.PatternLocator#matchReportReference(org.eclipse.jdt.internal.compiler.ast.ASTNode, org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt.internal.compiler.lookup.Binding, int, org.eclipse.jdt.internal.core.search.matching.MatchLocator)
58  */

59 protected void matchReportReference(ASTNode reference, IJavaElement element, Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException {
60     if (elementBinding instanceof ReferenceBinding) {
61         ReferenceBinding referenceBinding = (ReferenceBinding) elementBinding;
62         if (referenceBinding.isClass() && this.pattern.typeSuffix == IIndexConstants.INTERFACE_SUFFIX) {
63             // do not report class if expected types are only interfaces
64
return;
65         }
66         if (referenceBinding.isInterface() && this.pattern.typeSuffix == IIndexConstants.CLASS_SUFFIX) {
67             // do not report interface if expected types are only classes
68
return;
69         }
70     }
71     super.matchReportReference(reference, element, elementBinding, accuracy, locator);
72 }
73 protected int referenceType() {
74     return IJavaElement.TYPE;
75 }
76 public int resolveLevel(ASTNode node) {
77     if (!(node instanceof TypeReference)) return IMPOSSIBLE_MATCH;
78
79     TypeReference typeRef = (TypeReference) node;
80     TypeBinding binding = typeRef.resolvedType;
81     if (binding == null) return INACCURATE_MATCH;
82     return resolveLevelForType(this.pattern.superSimpleName, this.pattern.superQualification, binding);
83 }
84 public int resolveLevel(Binding binding) {
85     if (binding == null) return INACCURATE_MATCH;
86     if (!(binding instanceof ReferenceBinding)) return IMPOSSIBLE_MATCH;
87
88     ReferenceBinding type = (ReferenceBinding) binding;
89     int level = IMPOSSIBLE_MATCH;
90     if (this.pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_INTERFACES) {
91         level = resolveLevelForType(this.pattern.superSimpleName, this.pattern.superQualification, type.superclass());
92         if (level == ACCURATE_MATCH) return ACCURATE_MATCH;
93     }
94
95     if (this.pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_CLASSES) {
96         ReferenceBinding[] superInterfaces = type.superInterfaces();
97         for (int i = 0, max = superInterfaces.length; i < max; i++) {
98             int newLevel = resolveLevelForType(this.pattern.superSimpleName, this.pattern.superQualification, superInterfaces[i]);
99             if (newLevel > level) {
100                 if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
101                 level = newLevel;
102             }
103         }
104     }
105     return level;
106 }
107 public String JavaDoc toString() {
108     return "Locator for " + this.pattern.toString(); //$NON-NLS-1$
109
}
110 }
111
Popular Tags