diff options
author | Manoj Palat | 2017-11-28 07:26:09 +0000 |
---|---|---|
committer | Manoj Palat | 2017-11-28 07:26:09 +0000 |
commit | 7b578e6c554f0780808d7dc0a5748e53798be2a7 (patch) | |
tree | a2579cf0d146a9e1e264334614d67d6aaef943f0 /org.eclipse.jdt.core/search | |
parent | e45ddc1659ac0d14714b0b0978ad0b020a4ef2eb (diff) | |
download | eclipse.jdt.core-7b578e6c554f0780808d7dc0a5748e53798be2a7.tar.gz eclipse.jdt.core-7b578e6c554f0780808d7dc0a5748e53798be2a7.tar.xz eclipse.jdt.core-7b578e6c554f0780808d7dc0a5748e53798be2a7.zip |
module list part of bug 519151 [9][search] Need a way to use modules as
a search scope
Change-Id: I08e5471ef57f88fc1e68792f7747bcdac5e0568f
Diffstat (limited to 'org.eclipse.jdt.core/search')
5 files changed, 109 insertions, 22 deletions
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/core/search/SearchPattern.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/core/search/SearchPattern.java index 477b58dcca..c7ad4ec42a 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/core/search/SearchPattern.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/core/search/SearchPattern.java @@ -1440,6 +1440,15 @@ private static SearchPattern createPackagePattern(String patternString, int limi * <li>'?' is treated as a wildcard when it is inside <> (i.e. it must be put on first position of the type argument)</li> * </ul> * </div> + * Since 3.14 for Java 9, Type Declaration Patterns can have module names also embedded with the following syntax + * <p><b><code>[moduleName1[,moduleName2,..]]:[qualification '.']typeName ['<' typeArguments '>']</code></b></p> + * <p>Examples:</p> + * <ul> + * <li><code>java.base:java.lang.Object</code></li> + * <li><code>mod.one, mod.two:pack.X</code> find declaration in the list of given modules.</li> + * <li><code>:pack.X</code> find in the unnamed module.</li> + * </ul> + * <p> * </li> * <li>Method patterns have the following syntax: * <p><b><code>[declaringType '.'] ['<' typeArguments '>'] methodName ['(' parameterTypes ')'] [returnType]</code></b></p> @@ -2188,8 +2197,14 @@ private static SearchPattern createTypePattern(char[] simpleName, char[] package } return null; } - private static SearchPattern createTypePattern(String patternString, int limitTo, int matchRule, char indexSuffix) { + String[] arr = patternString.split(String.valueOf(IIndexConstants.MODULE_SEPARATOR)); + String moduleName = null; + if (arr.length == 2) { + moduleName = arr[0]; + patternString = arr[1]; + } + char[] patModName = moduleName != null ? moduleName.toCharArray() : null; // use 1.7 as the source level as there are more valid tokens in 1.7 mode // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376673 Scanner scanner = new Scanner(false /*comment*/, true /*whitespace*/, false /*nls*/, ClassFileConstants.JDK1_7/*sourceLevel*/, null /*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); @@ -2270,14 +2285,14 @@ private static SearchPattern createTypePattern(String patternString, int limitTo } switch (limitTo) { case IJavaSearchConstants.DECLARATIONS : // cannot search for explicit member types - return new QualifiedTypeDeclarationPattern(qualificationChars, typeChars, indexSuffix, matchRule); + return new QualifiedTypeDeclarationPattern(patModName, qualificationChars, typeChars, indexSuffix, matchRule); case IJavaSearchConstants.REFERENCES : return new TypeReferencePattern(qualificationChars, typeChars, typeSignature, indexSuffix, matchRule); case IJavaSearchConstants.IMPLEMENTORS : return new SuperTypeReferencePattern(qualificationChars, typeChars, SuperTypeReferencePattern.ONLY_SUPER_INTERFACES, indexSuffix, matchRule); case IJavaSearchConstants.ALL_OCCURRENCES : return new OrPattern( - new QualifiedTypeDeclarationPattern(qualificationChars, typeChars, indexSuffix, matchRule),// cannot search for explicit member types + new QualifiedTypeDeclarationPattern(patModName, qualificationChars, typeChars, indexSuffix, matchRule),// cannot search for explicit member types new TypeReferencePattern(qualificationChars, typeChars, typeSignature, indexSuffix, matchRule)); default: return new TypeReferencePattern(qualificationChars, typeChars, typeSignature, limitTo, indexSuffix, matchRule); diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IIndexConstants.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IIndexConstants.java index 44b2e9ccbf..43e2e83e8d 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IIndexConstants.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IIndexConstants.java @@ -45,6 +45,7 @@ public interface IIndexConstants { char SEPARATOR= '/'; char PARAMETER_SEPARATOR= ','; char SECONDARY_SUFFIX = 'S'; + char MODULE_SEPARATOR= ':'; char[] ONE_STAR = new char[] {'*'}; char[][] ONE_STAR_CHAR = new char[][] {ONE_STAR}; diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/QualifiedTypeDeclarationPattern.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/QualifiedTypeDeclarationPattern.java index 42f0e7d64e..ce8233e955 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/QualifiedTypeDeclarationPattern.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/QualifiedTypeDeclarationPattern.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2017 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 @@ -19,15 +19,18 @@ public char[] qualification; PackageDeclarationPattern packagePattern; public int packageIndex = -1; -public QualifiedTypeDeclarationPattern(char[] qualification, char[] simpleName, char typeSuffix, int matchRule) { +public QualifiedTypeDeclarationPattern(char[] moduleNames, char[] qualification, char[] simpleName, char typeSuffix, int matchRule) { this(matchRule); - + addModuleNames(moduleNames); this.qualification = this.isCaseSensitive ? qualification : CharOperation.toLowerCase(qualification); this.simpleName = (this.isCaseSensitive || this.isCamelCase) ? simpleName : CharOperation.toLowerCase(simpleName); this.typeSuffix = typeSuffix; this.mustResolve = this.qualification != null || typeSuffix != TYPE_SUFFIX; } +public QualifiedTypeDeclarationPattern(char[] qualification, char[] simpleName, char typeSuffix, int matchRule) { + this(null, qualification, simpleName, typeSuffix, matchRule); +} public QualifiedTypeDeclarationPattern(char[] qualification, int qualificationMatchRule, char[] simpleName, char typeSuffix, int matchRule) { this(qualification, simpleName, typeSuffix, matchRule); this.packagePattern = new PackageDeclarationPattern(qualification, qualificationMatchRule); diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationLocator.java index bdbd12a479..9fc2530685 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationLocator.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationLocator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. + * Copyright (c) 2000, 2017 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 @@ -10,6 +10,9 @@ *******************************************************************************/ package org.eclipse.jdt.internal.core.search.matching; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; @@ -75,6 +78,9 @@ public int resolveLevel(Binding binding) { case TYPE_SUFFIX : // nothing } + if (matchModule(this.pattern, type) == IMPOSSIBLE_MATCH) { + return IMPOSSIBLE_MATCH; + } // fully qualified name if (this.pattern instanceof QualifiedTypeDeclarationPattern) { QualifiedTypeDeclarationPattern qualifiedPattern = (QualifiedTypeDeclarationPattern) this.pattern; @@ -103,6 +109,28 @@ protected int resolveLevelForType(char[] simpleNamePattern, char[] qualification return resolveLevelForType(simpleNamePattern, fullQualificationPattern, type); return IMPOSSIBLE_MATCH; } +private int matchModule(TypeDeclarationPattern typePattern, TypeBinding type) { + if (!(type instanceof ReferenceBinding)) + return INACCURATE_MATCH; // a safety net, should not come here for error free code. + ReferenceBinding reference = (ReferenceBinding) type; + ModuleBinding module = reference.module(); + if (module == null || module.moduleName == null || typePattern.moduleNames == null) + return POSSIBLE_MATCH; //can't determine, say possible to all. + String bindModName = new String(module.moduleName); + + if (typePattern.modulePatterns == null) {// use 'normal' matching + for (char[] m : typePattern.moduleNames) { // match any in the list + int ret = matchNameValue(m, module.moduleName); + if (ret != IMPOSSIBLE_MATCH) return ret; + } + } else {// use pattern matching + for (Pattern p : typePattern.modulePatterns) { + Matcher matcher = p.matcher(bindModName); + if (matcher.matches()) return ACCURATE_MATCH; + } + } + return IMPOSSIBLE_MATCH; +} public String toString() { return "Locator for " + this.pattern.toString(); //$NON-NLS-1$ } diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationPattern.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationPattern.java index ea813ef363..4b048e5da6 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationPattern.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeDeclarationPattern.java @@ -11,17 +11,22 @@ package org.eclipse.jdt.internal.core.search.matching; import java.io.IOException; +import java.util.regex.Pattern; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.core.search.SearchPattern; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.core.index.*; +import org.eclipse.jdt.internal.core.search.indexing.IIndexConstants; public class TypeDeclarationPattern extends JavaSearchPattern { public char[] simpleName; public char[] pkg; public char[][] enclosingTypeNames; +public char[][] moduleNames = null; +private boolean allowModuleRegex = false; // enable to try experimental Module Regex Match +/* package */ Pattern[] modulePatterns = null; // set to CLASS_SUFFIX for only matching classes // set to INTERFACE_SUFFIX for only matching interfaces @@ -132,31 +137,66 @@ public static char[] createIndexKey(int modifiers, char[] typeName, char[] packa } public TypeDeclarationPattern( + char[] moduleNames, + char[] pkg, + char[][] enclosingTypeNames, + char[] simpleName, + char typeSuffix, + int matchRule) { + + this(matchRule); + addModuleNames(moduleNames); + this.pkg = this.isCaseSensitive ? pkg : CharOperation.toLowerCase(pkg); + if (this.isCaseSensitive || enclosingTypeNames == null) { + this.enclosingTypeNames = enclosingTypeNames; + } else { + int length = enclosingTypeNames.length; + this.enclosingTypeNames = new char[length][]; + for (int i = 0; i < length; i++) + this.enclosingTypeNames[i] = CharOperation.toLowerCase(enclosingTypeNames[i]); + } + this.simpleName = (this.isCaseSensitive || this.isCamelCase) ? simpleName : CharOperation.toLowerCase(simpleName); + this.typeSuffix = typeSuffix; + + this.mustResolve = (this.pkg != null && this.enclosingTypeNames != null) || typeSuffix != TYPE_SUFFIX; + } + +public TypeDeclarationPattern( char[] pkg, char[][] enclosingTypeNames, char[] simpleName, char typeSuffix, int matchRule) { - this(matchRule); - - this.pkg = this.isCaseSensitive ? pkg : CharOperation.toLowerCase(pkg); - if (this.isCaseSensitive || enclosingTypeNames == null) { - this.enclosingTypeNames = enclosingTypeNames; - } else { - int length = enclosingTypeNames.length; - this.enclosingTypeNames = new char[length][]; - for (int i = 0; i < length; i++) - this.enclosingTypeNames[i] = CharOperation.toLowerCase(enclosingTypeNames[i]); - } - this.simpleName = (this.isCaseSensitive || this.isCamelCase) ? simpleName : CharOperation.toLowerCase(simpleName); - this.typeSuffix = typeSuffix; - - this.mustResolve = (this.pkg != null && this.enclosingTypeNames != null) || typeSuffix != TYPE_SUFFIX; + this(null, pkg, enclosingTypeNames, simpleName, typeSuffix, matchRule); } TypeDeclarationPattern(int matchRule) { super(TYPE_DECL_PATTERN, matchRule); } +protected void addModuleNames(char[] modNames) { + if (modNames == null) { + return; + } + String[] names = new String(modNames).split(String.valueOf(CharOperation.COMMA_SEPARATOR)); + int len = names.length; + if (this.allowModuleRegex && len > 0 && names[0] != null && names[0].length() > 0 + && names[0].charAt(0) == IIndexConstants.ZERO_CHAR) { //pattern + names[0] = names[0].substring(1); + this.modulePatterns = new Pattern[len]; + for (int i = 0; i < len; ++i) { + this.modulePatterns[i] = Pattern.compile(names[i]); + } + } else { // 'normal' matching - flag if don't care conditions are passed + for (int i = 0; i < len; ++i) { + String name = names[i] = names[i].trim(); + } + } + this.moduleNames = new char[len][]; + for (int i = 0; i < len; ++i) { + String s = names[i]; + this.moduleNames[i] = s != null ? s.toCharArray() : CharOperation.NO_CHAR; + } +} /* * Type entries are encoded as: * simpleTypeName / packageName / enclosingTypeName / modifiers |