001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.search.matching;
011:
012: import org.eclipse.core.runtime.IPath;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.OperationCanceledException;
015: import org.eclipse.jdt.core.IJavaElement;
016: import org.eclipse.jdt.core.IMember;
017: import org.eclipse.jdt.core.IMethod;
018: import org.eclipse.jdt.core.IPackageFragmentRoot;
019: import org.eclipse.jdt.core.IType;
020: import org.eclipse.jdt.core.ITypeParameter;
021: import org.eclipse.jdt.core.Signature;
022: import org.eclipse.jdt.core.search.IJavaSearchScope;
023: import org.eclipse.jdt.core.search.SearchParticipant;
024: import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
025: import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
026: import org.eclipse.jdt.internal.core.index.Index;
027: import org.eclipse.jdt.internal.core.search.IndexQueryRequestor;
028: import org.eclipse.jdt.internal.core.search.JavaSearchScope;
029: import org.eclipse.jdt.internal.core.util.Util;
030:
031: /**
032: * Pattern to search type parameters.
033: *
034: * @since 3.1
035: */
036: public class TypeParameterPattern extends JavaSearchPattern {
037:
038: protected boolean findDeclarations;
039: protected boolean findReferences;
040: protected char[] name;
041: protected ITypeParameter typeParameter;
042: protected char[] declaringMemberName;
043: protected char[] methodDeclaringClassName;
044: protected char[][] methodArgumentTypes;
045:
046: /**
047: * @param findDeclarations
048: * @param findReferences
049: * @param typeParameter
050: * @param matchRule
051: */
052: public TypeParameterPattern(boolean findDeclarations,
053: boolean findReferences, ITypeParameter typeParameter,
054: int matchRule) {
055: super (TYPE_PARAM_PATTERN, matchRule);
056:
057: this .findDeclarations = findDeclarations; // set to find declarations & all occurences
058: this .findReferences = findReferences; // set to find references & all occurences
059: this .typeParameter = typeParameter;
060: this .name = typeParameter.getElementName().toCharArray(); // store type parameter name
061: IMember member = typeParameter.getDeclaringMember();
062: this .declaringMemberName = member.getElementName()
063: .toCharArray(); // store type parameter declaring member name
064:
065: // For method type parameter, store also declaring class name and parameters type names
066: if (member instanceof IMethod) {
067: IMethod method = (IMethod) member;
068: this .methodDeclaringClassName = method.getParent()
069: .getElementName().toCharArray();
070: String[] parameters = method.getParameterTypes();
071: int length = parameters.length;
072: this .methodArgumentTypes = new char[length][];
073: for (int i = 0; i < length; i++) {
074: this .methodArgumentTypes[i] = Signature
075: .toCharArray(parameters[i].toCharArray());
076: }
077: }
078: }
079:
080: /*
081: * Same than LocalVariablePattern.
082: */
083: public void findIndexMatches(Index index,
084: IndexQueryRequestor requestor,
085: SearchParticipant participant, IJavaSearchScope scope,
086: IProgressMonitor progressMonitor) {
087: IPackageFragmentRoot root = (IPackageFragmentRoot) this .typeParameter
088: .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
089: String documentPath;
090: String relativePath;
091: if (root.isArchive()) {
092: IType type = (IType) this .typeParameter
093: .getAncestor(IJavaElement.TYPE);
094: relativePath = (type.getFullyQualifiedName('/')).replace(
095: '.', '/')
096: + SuffixConstants.SUFFIX_STRING_class;
097: documentPath = root.getPath()
098: + IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR
099: + relativePath;
100: } else {
101: IPath path = this .typeParameter.getPath();
102: documentPath = path.toString();
103: relativePath = Util
104: .relativePath(path, 1/*remove project segment*/);
105: }
106:
107: if (scope instanceof JavaSearchScope) {
108: JavaSearchScope javaSearchScope = (JavaSearchScope) scope;
109: // Get document path access restriction from java search scope
110: // Note that requestor has to verify if needed whether the document violates the access restriction or not
111: AccessRuleSet access = javaSearchScope.getAccessRuleSet(
112: relativePath, index.containerPath);
113: if (access != JavaSearchScope.NOT_ENCLOSED) { // scope encloses the path
114: if (!requestor.acceptIndexMatch(documentPath, this ,
115: participant, access))
116: throw new OperationCanceledException();
117: }
118: } else if (scope.encloses(documentPath)) {
119: if (!requestor.acceptIndexMatch(documentPath, this ,
120: participant, null))
121: throw new OperationCanceledException();
122: }
123: }
124:
125: protected StringBuffer print(StringBuffer output) {
126: if (this .findDeclarations) {
127: output
128: .append(this .findReferences ? "TypeParamCombinedPattern: " //$NON-NLS-1$
129: : "TypeParamDeclarationPattern: "); //$NON-NLS-1$
130: } else {
131: output.append("TypeParamReferencePattern: "); //$NON-NLS-1$
132: }
133: output.append(typeParameter.toString());
134: return super.print(output);
135: }
136:
137: }
|