KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.IPath;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.OperationCanceledException;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IMember;
18 import org.eclipse.jdt.core.IMethod;
19 import org.eclipse.jdt.core.IPackageFragmentRoot;
20 import org.eclipse.jdt.core.IType;
21 import org.eclipse.jdt.core.ITypeParameter;
22 import org.eclipse.jdt.core.Signature;
23 import org.eclipse.jdt.core.search.IJavaSearchScope;
24 import org.eclipse.jdt.core.search.SearchParticipant;
25 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
26 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
27 import org.eclipse.jdt.internal.core.index.Index;
28 import org.eclipse.jdt.internal.core.search.IndexQueryRequestor;
29 import org.eclipse.jdt.internal.core.search.JavaSearchScope;
30 import org.eclipse.jdt.internal.core.util.Util;
31
32 /**
33  * Pattern to search type parameters.
34  *
35  * @since 3.1
36  */

37 public class TypeParameterPattern extends JavaSearchPattern {
38
39     protected boolean findDeclarations;
40     protected boolean findReferences;
41     protected char[] name;
42     protected ITypeParameter typeParameter;
43     protected char[] declaringMemberName;
44     protected char[] methodDeclaringClassName;
45     protected char[][] methodArgumentTypes;
46
47     /**
48      * @param findDeclarations
49      * @param findReferences
50      * @param typeParameter
51      * @param matchRule
52      */

53     public TypeParameterPattern(boolean findDeclarations, boolean findReferences, ITypeParameter typeParameter, int matchRule) {
54         super(TYPE_PARAM_PATTERN, matchRule);
55
56         this.findDeclarations = findDeclarations; // set to find declarations & all occurences
57
this.findReferences = findReferences; // set to find references & all occurences
58
this.typeParameter = typeParameter;
59         this.name = typeParameter.getElementName().toCharArray(); // store type parameter name
60
IMember member = typeParameter.getDeclaringMember();
61         this.declaringMemberName = member.getElementName().toCharArray(); // store type parameter declaring member name
62

63         // For method type parameter, store also declaring class name and parameters type names
64
if (member instanceof IMethod) {
65             IMethod method = (IMethod) member;
66             this.methodDeclaringClassName = method.getParent().getElementName().toCharArray();
67             String JavaDoc[] parameters = method.getParameterTypes();
68             int length = parameters.length;
69             this.methodArgumentTypes = new char[length][];
70             for (int i=0; i<length; i++) {
71                 this.methodArgumentTypes[i] = Signature.toCharArray(parameters[i].toCharArray());
72             }
73         }
74     }
75
76     /*
77      * Same than LocalVariablePattern.
78      */

79     public void findIndexMatches(Index index, IndexQueryRequestor requestor, SearchParticipant participant, IJavaSearchScope scope, IProgressMonitor progressMonitor) {
80         IPackageFragmentRoot root = (IPackageFragmentRoot) this.typeParameter.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
81         String JavaDoc documentPath;
82         String JavaDoc relativePath;
83         if (root.isArchive()) {
84             IType type = (IType) this.typeParameter.getAncestor(IJavaElement.TYPE);
85             relativePath = (type.getFullyQualifiedName('/')).replace('.', '/') + SuffixConstants.SUFFIX_STRING_class;
86             documentPath = root.getPath() + IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR + relativePath;
87         } else {
88             IPath path = this.typeParameter.getPath();
89             documentPath = path.toString();
90             relativePath = Util.relativePath(path, 1/*remove project segment*/);
91         }
92     
93         if (scope instanceof JavaSearchScope) {
94             JavaSearchScope javaSearchScope = (JavaSearchScope) scope;
95             // Get document path access restriction from java search scope
96
// Note that requestor has to verify if needed whether the document violates the access restriction or not
97
AccessRuleSet access = javaSearchScope.getAccessRuleSet(relativePath, index.containerPath);
98             if (access != JavaSearchScope.NOT_ENCLOSED) { // scope encloses the path
99
if (!requestor.acceptIndexMatch(documentPath, this, participant, access))
100                     throw new OperationCanceledException();
101             }
102         } else if (scope.encloses(documentPath)) {
103             if (!requestor.acceptIndexMatch(documentPath, this, participant, null))
104                 throw new OperationCanceledException();
105         }
106     }
107
108     protected StringBuffer JavaDoc print(StringBuffer JavaDoc output) {
109         if (this.findDeclarations) {
110             output.append(this.findReferences
111                 ? "TypeParamCombinedPattern: " //$NON-NLS-1$
112
: "TypeParamDeclarationPattern: "); //$NON-NLS-1$
113
} else {
114             output.append("TypeParamReferencePattern: "); //$NON-NLS-1$
115
}
116         output.append(typeParameter.toString());
117         return super.print(output);
118     }
119
120 }
121
Popular Tags