Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3e5bda0086706035e28365e92be82e0b2317be3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*******************************************************************************
 * Copyright (c) 2004, 2014 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core.search.matching;

import org.eclipse.jdt.core.BindingKey;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.internal.core.search.indexing.IIndexConstants;
import org.eclipse.jdt.internal.core.util.Util;


public class JavaSearchPattern extends SearchPattern implements IIndexConstants {
	
	/*
	 * Whether this pattern is case sensitive.
	 */
	boolean isCaseSensitive;
	/*
	 * Whether this pattern is camel case.
	 */
	boolean isCamelCase;

	/**
	 * One of following pattern value:
	 * <ul>
	 * 	<li>{@link #R_EXACT_MATCH}</li>
	 *		<li>{@link #R_PREFIX_MATCH}</li>
	 *		<li>{@link #R_PATTERN_MATCH}</li>
	 *		<li>{@link #R_REGEXP_MATCH}</li>
	 *		<li>{@link #R_CAMELCASE_MATCH}</li>
	 *		<li>{@link #R_CAMELCASE_SAME_PART_COUNT_MATCH}</li>
	 * </ul>
	 */
	int matchMode;

	/**
	 * One of {@link #R_ERASURE_MATCH}, {@link #R_EQUIVALENT_MATCH}, {@link #R_FULL_MATCH}.
	 */
	int matchCompatibility;

	/**
	 * Fine grain limitation
	 */
	public int fineGrain = 0;
	
	/**
	 * Mask used on match rule for match mode.
	 */
	public static final int MATCH_MODE_MASK = R_EXACT_MATCH
		| R_PREFIX_MATCH 
		| R_PATTERN_MATCH 
		| R_REGEXP_MATCH 
		| R_CAMELCASE_MATCH
		| R_CAMELCASE_SAME_PART_COUNT_MATCH;

	/**
	 * Mask used on match rule for generic relevance.
	 */
	public static final int MATCH_COMPATIBILITY_MASK = R_ERASURE_MATCH | R_EQUIVALENT_MATCH | R_FULL_MATCH;

	// Signatures and arguments for parameterized types search
	char[][] typeSignatures;
	private char[][][] typeArguments;
	private int flags = 0;
	static final int HAS_TYPE_ARGUMENTS = 1;

	protected JavaSearchPattern(int patternKind, int matchRule) {
		super(matchRule);
		this.kind = patternKind;
		// Use getMatchRule() instead of matchRule as super constructor may modify its value
		// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=81377
		int rule = getMatchRule();
		this.isCaseSensitive = (rule & R_CASE_SENSITIVE) != 0;
		this.isCamelCase = (rule & (R_CAMELCASE_MATCH | R_CAMELCASE_SAME_PART_COUNT_MATCH)) != 0;
		this.matchCompatibility = rule & MATCH_COMPATIBILITY_MASK;
		this.matchMode = rule & MATCH_MODE_MASK;
	}

	/**
	 * @param fineGrain
	 */
	public static String getFineGrainFlagString(final int fineGrain) {
		if (fineGrain == 0) {
			return "none"; //$NON-NLS-1$
		}
		StringBuffer buffer = new StringBuffer();
		for (int i=1; i<=32; i++) {
			int bit = fineGrain & (1<<(i-1));
			if (bit != 0 && buffer.length()>0) buffer.append(" | "); //$NON-NLS-1$
			switch (bit) {
				case IJavaSearchConstants.FIELD_DECLARATION_TYPE_REFERENCE:
					buffer.append("FIELD_DECLARATION_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.LOCAL_VARIABLE_DECLARATION_TYPE_REFERENCE:
					buffer.append("LOCAL_VARIABLE_DECLARATION_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.PARAMETER_DECLARATION_TYPE_REFERENCE:
					buffer.append("PARAMETER_DECLARATION_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.SUPERTYPE_TYPE_REFERENCE:
					buffer.append("SUPERTYPE_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.THROWS_CLAUSE_TYPE_REFERENCE:
					buffer.append("THROWS_CLAUSE_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.CAST_TYPE_REFERENCE:
					buffer.append("CAST_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.CATCH_TYPE_REFERENCE:
					buffer.append("CATCH_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.CLASS_INSTANCE_CREATION_TYPE_REFERENCE:
					buffer.append("CLASS_INSTANCE_CREATION_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.RETURN_TYPE_REFERENCE:
					buffer.append("RETURN_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.IMPORT_DECLARATION_TYPE_REFERENCE:
					buffer.append("IMPORT_DECLARATION_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE:
					buffer.append("ANNOTATION_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.TYPE_ARGUMENT_TYPE_REFERENCE:
					buffer.append("TYPE_ARGUMENT_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.TYPE_VARIABLE_BOUND_TYPE_REFERENCE:
					buffer.append("TYPE_VARIABLE_BOUND_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.WILDCARD_BOUND_TYPE_REFERENCE:
					buffer.append("WILDCARD_BOUND_TYPE_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.SUPER_REFERENCE:
					buffer.append("SUPER_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.QUALIFIED_REFERENCE:
					buffer.append("QUALIFIED_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.THIS_REFERENCE:
					buffer.append("THIS_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.IMPLICIT_THIS_REFERENCE:
					buffer.append("IMPLICIT_THIS_REFERENCE"); //$NON-NLS-1$
					break;
				case IJavaSearchConstants.METHOD_REFERENCE_EXPRESSION:
					buffer.append("METHOD_REFERENCE_EXPRESSION"); //$NON-NLS-1$
					break;
			}
		}
		return buffer.toString();
	}

	@Override
	public SearchPattern getBlankPattern() {
		return null;
	}

	final int getMatchMode() {
		return this.matchMode;
	}

	final boolean isCamelCase() {
		return this.isCamelCase;
	}

	final boolean isCaseSensitive () {
		return this.isCaseSensitive;
	}

	final boolean isErasureMatch() {
		return (this.matchCompatibility & R_ERASURE_MATCH) != 0;
	}

	final boolean isEquivalentMatch() {
		return (this.matchCompatibility & R_EQUIVALENT_MATCH) != 0;
	}

	/*
	 * Extract method arguments using unique key for parameterized methods
	 * and type parameters for non-generic ones.
	 */
	char[][] extractMethodArguments(IMethod method) {

		// Use bind key if the element is resolved
		if (method.isResolved()) {
			BindingKey bindingKey = new BindingKey(method.getKey());
			if (bindingKey.isParameterizedMethod()) {
				String[] argumentsSignatures = bindingKey.getTypeArguments();
				int length = argumentsSignatures.length;
				if (length > 0) {
					char[][] methodArguments = new char[length][];
					for (int i=0; i<length; i++) {
						methodArguments[i] = argumentsSignatures[i].toCharArray();
						CharOperation.replace(methodArguments[i], new char[] { '$', '/' }, '.');
					}
					return methodArguments;
				}
			}
			return null;
		}
		
		// Try to get the argument using the JavaModel info
		try {
			ITypeParameter[] parameters = method.getTypeParameters();
			if (parameters != null) {
				int length = parameters.length;
				if (length > 0) {
					char[][] arguments = new char[length][];
					for (int i=0; i<length; i++) {
						arguments[i] = Signature.createTypeSignature(parameters[i].getElementName(), false).toCharArray();
					}
					return arguments;
				}
			}
		}
		catch (JavaModelException jme) {
			// do nothing
		}
		return null;
	}

	/**
	 * @return Returns the typeArguments.
	 */
	final char[][][] getTypeArguments() {
		return this.typeArguments;
	}

	/**
	 * Returns whether the pattern has signatures or not.
	 * If pattern {@link #typeArguments} field, this field shows that it was built
	 * on a generic source type.
	 * @return true if {@link #typeSignatures} field is not null and has a length greater than 0.
	 */
	public final boolean hasSignatures() {
		return this.typeSignatures != null && this.typeSignatures.length > 0;
	}

	/**
	 * Returns whether the pattern includes type arguments information or not.
	 * @return default is false
	 */
	public final boolean hasTypeArguments() {
		return (this.flags & HAS_TYPE_ARGUMENTS) != 0;
	}

	/**
	 * Returns whether the pattern includes type parameters information or not.
	 * @return true if {@link #typeArguments} contains type parameters instead
	 * 	type arguments signatures.
	 */
	public final boolean hasTypeParameters() {
		return !hasSignatures() && hasTypeArguments();
	}
	
	/**
	 * Return whether two suffixes are compatible.
	 * 
	 * Note that obvious compatibility values as equals and {@link IIndexConstants#TYPE_SUFFIX}
	 * has to be tested by caller to avoid unnecessary method call...
	 * 
	 * @param typeSuffix
	 * @param patternSuffix
	 * @return true if suffixes are compatible, false otherwise
	 */
	boolean matchDifferentTypeSuffixes(int typeSuffix, int patternSuffix) {
		switch(typeSuffix) {
			case CLASS_SUFFIX :
				switch (patternSuffix) {
					case CLASS_AND_INTERFACE_SUFFIX :
					case CLASS_AND_ENUM_SUFFIX :
						return true;
				}
				return false;

			case INTERFACE_SUFFIX :
				switch (patternSuffix) {
					case CLASS_AND_INTERFACE_SUFFIX :
					case INTERFACE_AND_ANNOTATION_SUFFIX:
						return true;
				}
				return false;

			case ENUM_SUFFIX :
				return patternSuffix == CLASS_AND_ENUM_SUFFIX;

			case ANNOTATION_TYPE_SUFFIX :
				return patternSuffix == INTERFACE_AND_ANNOTATION_SUFFIX;

			case CLASS_AND_INTERFACE_SUFFIX :
				switch (patternSuffix) {
					case CLASS_SUFFIX :
					case INTERFACE_SUFFIX :
						return true;
				}
				return false;

			case CLASS_AND_ENUM_SUFFIX :
				switch (patternSuffix) {
					case CLASS_SUFFIX :
					case ENUM_SUFFIX :
						return true;
				}
				return false;

			case INTERFACE_AND_ANNOTATION_SUFFIX :
				switch (patternSuffix) {
					case INTERFACE_SUFFIX :
					case ANNOTATION_TYPE_SUFFIX :
						return true;
				}
				return false;
		}
		
		// Default behavior is to match suffixes
		return true;
	}

	protected StringBuffer print(StringBuffer output) {
		output.append(", "); //$NON-NLS-1$
		if (hasTypeArguments() && hasSignatures()) {
			output.append("signature:\""); //$NON-NLS-1$
			output.append(this.typeSignatures[0]);
			output.append("\", "); //$NON-NLS-1$
		}
		switch(getMatchMode()) {
			case R_EXACT_MATCH : 
				output.append("exact match, "); //$NON-NLS-1$
				break;
			case R_PREFIX_MATCH :
				output.append("prefix match, "); //$NON-NLS-1$
				break;
			case R_PATTERN_MATCH :
				output.append("pattern match, "); //$NON-NLS-1$
				break;
			case R_REGEXP_MATCH :
				output.append("regexp match, "); //$NON-NLS-1$
				break;
			case R_CAMELCASE_MATCH :
				output.append("camel case match, "); //$NON-NLS-1$
				break;
			case R_CAMELCASE_SAME_PART_COUNT_MATCH:
				output.append("camel case same part count match, "); //$NON-NLS-1$
				break;
		}
		if (isCaseSensitive())
			output.append("case sensitive, "); //$NON-NLS-1$
		else
			output.append("case insensitive, "); //$NON-NLS-1$
		if ((this.matchCompatibility & R_FULL_MATCH) != 0) {
			output.append("generic full match, "); //$NON-NLS-1$
		}
		if ((this.matchCompatibility & R_ERASURE_MATCH) != 0) {
			output.append("generic erasure match, "); //$NON-NLS-1$
		}
		if ((this.matchCompatibility & R_EQUIVALENT_MATCH) != 0) {
			output.append("generic equivalent match, "); //$NON-NLS-1$
		}
		output.append("fine grain: "); //$NON-NLS-1$
		output.append(getFineGrainFlagString(this.fineGrain));
		return output;
	}

	/**
	 * @param typeArguments The typeArguments to set.
	 */
	final void setTypeArguments(char[][][] typeArguments) {
		this.typeArguments = typeArguments;
		// update flags
		if (this.typeArguments != null) {
			int length = this.typeArguments.length;
			for (int i=0; i<length; i++) {
				if (this.typeArguments[i] != null && this.typeArguments[i].length > 0) {
					this.flags |= HAS_TYPE_ARGUMENTS;
					break;
				}
			}
		}
	}

	/*
	 * Extract and store type signatures and arguments using unique key for parameterized types
	 * and type parameters for non-generic ones
	 */
	void storeTypeSignaturesAndArguments(IType type) {
		if (type.isResolved()) {
			BindingKey bindingKey = new BindingKey(type.getKey());
			if (bindingKey.isParameterizedType() || bindingKey.isRawType()) {
				String signature = bindingKey.toSignature();
				this.typeSignatures = Util.splitTypeLevelsSignature(signature);
				setTypeArguments(Util.getAllTypeArguments(this.typeSignatures));
			}
			return;
		}

		// Scan hierarchy to store type arguments at each level
		char[][][] typeParameters = new char[10][][];
		int ptr = -1;
		boolean hasParameters = false;
		try {
			IJavaElement parent = type;
			ITypeParameter[] parameters = null;
			while (parent != null && parent.getElementType() == IJavaElement.TYPE) {
				if (++ptr > typeParameters.length) {
					System.arraycopy(typeParameters, 0, typeParameters = new char[typeParameters.length+10][][], 0, ptr);
				}
				IType parentType = (IType) parent;
				parameters = parentType.getTypeParameters();
				if (parameters !=null) {
					int length = parameters.length;
					if (length > 0) {
						hasParameters = true;
						typeParameters[ptr] = new char[length][];
						for (int i=0; i<length; i++)
							typeParameters[ptr][i] = Signature.createTypeSignature(parameters[i].getElementName(), false).toCharArray();
					}
				}
				parent = parent.getParent();
			}
		}
		catch (JavaModelException jme) {
			return;
		}

		// Store type arguments if any
		if (hasParameters) {
			if (++ptr < typeParameters.length)
				System.arraycopy(typeParameters, 0, typeParameters = new char[ptr][][], 0, ptr);
			setTypeArguments(typeParameters);
		}
	}
	@Override
	public final String toString() {
		return print(new StringBuffer(30)).toString();
	}
}

Back to the top