KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
15 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17
18 public class TypeDeclarationLocator extends PatternLocator {
19
20 protected TypeDeclarationPattern pattern; // can be a QualifiedTypeDeclarationPattern
21

22 public TypeDeclarationLocator(TypeDeclarationPattern pattern) {
23     super(pattern);
24
25     this.pattern = pattern;
26 }
27 //public int match(ASTNode node, MatchingNodeSet nodeSet) - SKIP IT
28
//public int match(ConstructorDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
29
//public int match(Expression node, MatchingNodeSet nodeSet) - SKIP IT
30
//public int match(FieldDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
31
//public int match(MethodDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
32
//public int match(MessageSend node, MatchingNodeSet nodeSet) - SKIP IT
33
//public int match(Reference node, MatchingNodeSet nodeSet) - SKIP IT
34
public int match(TypeDeclaration node, MatchingNodeSet nodeSet) {
35     if (this.pattern.simpleName == null || matchesName(this.pattern.simpleName, node.name))
36         return nodeSet.addMatch(node, ((InternalSearchPattern)this.pattern).mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
37
38     return IMPOSSIBLE_MATCH;
39 }
40 //public int match(TypeReference node, MatchingNodeSet nodeSet) - SKIP IT
41

42 public int resolveLevel(ASTNode node) {
43     if (!(node instanceof TypeDeclaration)) return IMPOSSIBLE_MATCH;
44
45     return resolveLevel(((TypeDeclaration) node).binding);
46 }
47 public int resolveLevel(Binding binding) {
48     if (binding == null) return INACCURATE_MATCH;
49     if (!(binding instanceof TypeBinding)) return IMPOSSIBLE_MATCH;
50
51     TypeBinding type = (TypeBinding) binding;
52
53     switch (this.pattern.typeSuffix) {
54         case CLASS_SUFFIX:
55             if (!type.isClass()) return IMPOSSIBLE_MATCH;
56             break;
57         case CLASS_AND_INTERFACE_SUFFIX:
58             if (!(type.isClass() || (type.isInterface() && !type.isAnnotationType()))) return IMPOSSIBLE_MATCH;
59             break;
60         case CLASS_AND_ENUM_SUFFIX:
61             if (!(type.isClass() || type.isEnum())) return IMPOSSIBLE_MATCH;
62             break;
63         case INTERFACE_SUFFIX:
64             if (!type.isInterface() || type.isAnnotationType()) return IMPOSSIBLE_MATCH;
65             break;
66         case INTERFACE_AND_ANNOTATION_SUFFIX:
67             if (!(type.isInterface() || type.isAnnotationType())) return IMPOSSIBLE_MATCH;
68             break;
69         case ENUM_SUFFIX:
70             if (!type.isEnum()) return IMPOSSIBLE_MATCH;
71             break;
72         case ANNOTATION_TYPE_SUFFIX:
73             if (!type.isAnnotationType()) return IMPOSSIBLE_MATCH;
74             break;
75         case TYPE_SUFFIX : // nothing
76
}
77
78     // fully qualified name
79
if (this.pattern instanceof QualifiedTypeDeclarationPattern) {
80         QualifiedTypeDeclarationPattern qualifiedPattern = (QualifiedTypeDeclarationPattern) this.pattern;
81         return resolveLevelForType(qualifiedPattern.simpleName, qualifiedPattern.qualification, type);
82     } else {
83         char[] enclosingTypeName = this.pattern.enclosingTypeNames == null ? null : CharOperation.concatWith(this.pattern.enclosingTypeNames, '.');
84         return resolveLevelForType(this.pattern.simpleName, this.pattern.pkg, enclosingTypeName, type);
85     }
86 }
87 /**
88  * Returns whether the given type binding matches the given simple name pattern
89  * qualification pattern and enclosing type name pattern.
90  */

91 protected int resolveLevelForType(char[] simpleNamePattern, char[] qualificationPattern, char[] enclosingNamePattern, TypeBinding type) {
92     if (enclosingNamePattern == null)
93         return resolveLevelForType(simpleNamePattern, qualificationPattern, type);
94     if (qualificationPattern == null)
95         return resolveLevelForType(simpleNamePattern, enclosingNamePattern, type);
96
97     // case of an import reference while searching for ALL_OCCURENCES of a type (see bug 37166)
98
if (type instanceof ProblemReferenceBinding) return IMPOSSIBLE_MATCH;
99
100     // pattern was created from a Java element: qualification is the package name.
101
char[] fullQualificationPattern = CharOperation.concat(qualificationPattern, enclosingNamePattern, '.');
102     if (CharOperation.equals(this.pattern.pkg, CharOperation.concatWith(type.getPackage().compoundName, '.')))
103         return resolveLevelForType(simpleNamePattern, fullQualificationPattern, type);
104     return IMPOSSIBLE_MATCH;
105 }
106 public String JavaDoc toString() {
107     return "Locator for " + this.pattern.toString(); //$NON-NLS-1$
108
}
109 }
110
Popular Tags