List of usage examples for org.eclipse.jdt.core.compiler CharOperation concat
public static final char[] concat(char[] first, char[] second)
From source file:com.codenvy.ide.ext.java.server.core.search.SearchPattern.java
License:Open Source License
private static SearchPattern createFieldPattern(String patternString, int limitTo, int matchRule) { // 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*/); scanner.setSource(patternString.toCharArray()); final int InsideDeclaringPart = 1; final int InsideType = 2; int lastToken = -1; String declaringType = null, fieldName = null; String type = null;//from w w w. j a v a 2 s . c o m int mode = InsideDeclaringPart; int token; try { token = scanner.getNextToken(); } catch (InvalidInputException e) { return null; } while (token != TerminalTokens.TokenNameEOF) { switch (mode) { // read declaring type and fieldName case InsideDeclaringPart: switch (token) { case TerminalTokens.TokenNameDOT: if (declaringType == null) { if (fieldName == null) return null; declaringType = fieldName; } else { String tokenSource = scanner.getCurrentTokenString(); declaringType += tokenSource + fieldName; } fieldName = null; break; case TerminalTokens.TokenNameWHITESPACE: if (!(TerminalTokens.TokenNameWHITESPACE == lastToken || TerminalTokens.TokenNameDOT == lastToken)) mode = InsideType; break; default: // all other tokens are considered identifiers (see bug 21763 Problem in Java search [search]) if (fieldName == null) fieldName = scanner.getCurrentTokenString(); else fieldName += scanner.getCurrentTokenString(); } break; // read type case InsideType: switch (token) { case TerminalTokens.TokenNameWHITESPACE: break; default: // all other tokens are considered identifiers (see bug 21763 Problem in Java search [search]) if (type == null) type = scanner.getCurrentTokenString(); else type += scanner.getCurrentTokenString(); } break; } lastToken = token; try { token = scanner.getNextToken(); } catch (InvalidInputException e) { return null; } } if (fieldName == null) return null; char[] fieldNameChars = fieldName.toCharArray(); if (fieldNameChars.length == 1 && fieldNameChars[0] == '*') fieldNameChars = null; char[] declaringTypeQualification = null, declaringTypeSimpleName = null; char[] typeQualification = null, typeSimpleName = null; // extract declaring type infos if (declaringType != null) { char[] declaringTypePart = declaringType.toCharArray(); int lastDotPosition = CharOperation.lastIndexOf('.', declaringTypePart); if (lastDotPosition >= 0) { declaringTypeQualification = CharOperation.subarray(declaringTypePart, 0, lastDotPosition); if (declaringTypeQualification.length == 1 && declaringTypeQualification[0] == '*') declaringTypeQualification = null; declaringTypeSimpleName = CharOperation.subarray(declaringTypePart, lastDotPosition + 1, declaringTypePart.length); } else { declaringTypeSimpleName = declaringTypePart; } if (declaringTypeSimpleName.length == 1 && declaringTypeSimpleName[0] == '*') declaringTypeSimpleName = null; } // extract type infos if (type != null) { char[] typePart = type.toCharArray(); int lastDotPosition = CharOperation.lastIndexOf('.', typePart); if (lastDotPosition >= 0) { typeQualification = CharOperation.subarray(typePart, 0, lastDotPosition); if (typeQualification.length == 1 && typeQualification[0] == '*') { typeQualification = null; } else { // prefix with a '*' as the full qualification could be bigger (because of an import) typeQualification = CharOperation.concat(IIndexConstants.ONE_STAR, typeQualification); } typeSimpleName = CharOperation.subarray(typePart, lastDotPosition + 1, typePart.length); } else { typeSimpleName = typePart; } if (typeSimpleName.length == 1 && typeSimpleName[0] == '*') typeSimpleName = null; } // Create field pattern return new FieldPattern(fieldNameChars, declaringTypeQualification, declaringTypeSimpleName, typeQualification, typeSimpleName, limitTo, matchRule); }
From source file:com.codenvy.ide.ext.java.server.core.search.SearchPattern.java
License:Open Source License
private static SearchPattern createMethodOrConstructorPattern(String patternString, int limitTo, int matchRule, boolean isConstructor) { // 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*/); scanner.setSource(patternString.toCharArray()); final int InsideSelector = 1; final int InsideTypeArguments = 2; final int InsideParameter = 3; final int InsideReturnType = 4; int lastToken = -1; String declaringType = null, selector = null, parameterType = null; String[] parameterTypes = null; char[][] typeArguments = null; String typeArgumentsString = null; int parameterCount = -1; String returnType = null;/*from w w w .jav a2 s . c o m*/ boolean foundClosingParenthesis = false; int mode = InsideSelector; int token, argCount = 0; try { token = scanner.getNextToken(); } catch (InvalidInputException e) { return null; } while (token != TerminalTokens.TokenNameEOF) { switch (mode) { // read declaring type and selector case InsideSelector: if (argCount == 0) { switch (token) { case TerminalTokens.TokenNameLESS: argCount++; if (selector == null || lastToken == TerminalTokens.TokenNameDOT) { typeArgumentsString = scanner.getCurrentTokenString(); mode = InsideTypeArguments; break; } if (declaringType == null) { declaringType = selector; } else { declaringType += '.' + selector; } declaringType += scanner.getCurrentTokenString(); selector = null; break; case TerminalTokens.TokenNameDOT: if (!isConstructor && typeArgumentsString != null) return null; // invalid syntax if (declaringType == null) { if (selector == null) return null; // invalid syntax declaringType = selector; } else if (selector != null) { declaringType += scanner.getCurrentTokenString() + selector; } selector = null; break; case TerminalTokens.TokenNameLPAREN: parameterTypes = new String[5]; parameterCount = 0; mode = InsideParameter; break; case TerminalTokens.TokenNameWHITESPACE: switch (lastToken) { case TerminalTokens.TokenNameWHITESPACE: case TerminalTokens.TokenNameDOT: case TerminalTokens.TokenNameGREATER: case TerminalTokens.TokenNameRIGHT_SHIFT: case TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT: break; default: mode = InsideReturnType; break; } break; default: // all other tokens are considered identifiers (see bug 21763 Problem in Java search [search]) if (selector == null) selector = scanner.getCurrentTokenString(); else selector += scanner.getCurrentTokenString(); break; } } else { if (declaringType == null) return null; // invalid syntax switch (token) { case TerminalTokens.TokenNameGREATER: case TerminalTokens.TokenNameRIGHT_SHIFT: case TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT: argCount--; break; case TerminalTokens.TokenNameLESS: argCount++; break; } declaringType += scanner.getCurrentTokenString(); } break; // read type arguments case InsideTypeArguments: if (typeArgumentsString == null) return null; // invalid syntax typeArgumentsString += scanner.getCurrentTokenString(); switch (token) { case TerminalTokens.TokenNameGREATER: case TerminalTokens.TokenNameRIGHT_SHIFT: case TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT: argCount--; if (argCount == 0) { String pseudoType = "Type" + typeArgumentsString; //$NON-NLS-1$ typeArguments = Signature .getTypeArguments(Signature.createTypeSignature(pseudoType, false).toCharArray()); mode = InsideSelector; } break; case TerminalTokens.TokenNameLESS: argCount++; break; } break; // read parameter types case InsideParameter: if (argCount == 0) { switch (token) { case TerminalTokens.TokenNameWHITESPACE: break; case TerminalTokens.TokenNameCOMMA: if (parameterType == null) return null; if (parameterTypes != null) { if (parameterTypes.length == parameterCount) System.arraycopy(parameterTypes, 0, parameterTypes = new String[parameterCount * 2], 0, parameterCount); parameterTypes[parameterCount++] = parameterType; } parameterType = null; break; case TerminalTokens.TokenNameRPAREN: foundClosingParenthesis = true; if (parameterType != null && parameterTypes != null) { if (parameterTypes.length == parameterCount) System.arraycopy(parameterTypes, 0, parameterTypes = new String[parameterCount * 2], 0, parameterCount); parameterTypes[parameterCount++] = parameterType; } mode = isConstructor ? InsideTypeArguments : InsideReturnType; break; case TerminalTokens.TokenNameLESS: argCount++; if (parameterType == null) return null; // invalid syntax // $FALL-THROUGH$ - fall through next case to add token default: // all other tokens are considered identifiers (see bug 21763 Problem in Java search [search]) if (parameterType == null) parameterType = scanner.getCurrentTokenString(); else parameterType += scanner.getCurrentTokenString(); } } else { if (parameterType == null) return null; // invalid syntax switch (token) { case TerminalTokens.TokenNameGREATER: case TerminalTokens.TokenNameRIGHT_SHIFT: case TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT: argCount--; break; case TerminalTokens.TokenNameLESS: argCount++; break; } parameterType += scanner.getCurrentTokenString(); } break; // read return type case InsideReturnType: if (argCount == 0) { switch (token) { case TerminalTokens.TokenNameWHITESPACE: break; case TerminalTokens.TokenNameLPAREN: parameterTypes = new String[5]; parameterCount = 0; mode = InsideParameter; break; case TerminalTokens.TokenNameLESS: argCount++; if (returnType == null) return null; // invalid syntax // $FALL-THROUGH$ - fall through next case to add token default: // all other tokens are considered identifiers (see bug 21763 Problem in Java search [search]) if (returnType == null) returnType = scanner.getCurrentTokenString(); else returnType += scanner.getCurrentTokenString(); } } else { if (returnType == null) return null; // invalid syntax switch (token) { case TerminalTokens.TokenNameGREATER: case TerminalTokens.TokenNameRIGHT_SHIFT: case TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT: argCount--; break; case TerminalTokens.TokenNameLESS: argCount++; break; } returnType += scanner.getCurrentTokenString(); } break; } lastToken = token; try { token = scanner.getNextToken(); } catch (InvalidInputException e) { return null; } } // parenthesis mismatch if (parameterCount > 0 && !foundClosingParenthesis) return null; // type arguments mismatch if (argCount > 0) return null; char[] selectorChars = null; if (isConstructor) { // retrieve type for constructor patterns if (declaringType == null) declaringType = selector; else if (selector != null) declaringType += '.' + selector; } else { // get selector chars if (selector == null) return null; selectorChars = selector.toCharArray(); if (selectorChars.length == 1 && selectorChars[0] == '*') selectorChars = null; } char[] declaringTypeQualification = null, declaringTypeSimpleName = null; char[] returnTypeQualification = null, returnTypeSimpleName = null; char[][] parameterTypeQualifications = null, parameterTypeSimpleNames = null; // Signatures String declaringTypeSignature = null; String returnTypeSignature = null; String[] parameterTypeSignatures = null; // extract declaring type infos if (declaringType != null) { // get declaring type part and signature char[] declaringTypePart = null; try { declaringTypeSignature = Signature.createTypeSignature(declaringType, false); if (declaringTypeSignature.indexOf(Signature.C_GENERIC_START) < 0) { declaringTypePart = declaringType.toCharArray(); } else { declaringTypePart = Signature .toCharArray(Signature.getTypeErasure(declaringTypeSignature.toCharArray())); } } catch (IllegalArgumentException iae) { // declaring type is invalid return null; } int lastDotPosition = CharOperation.lastIndexOf('.', declaringTypePart); if (lastDotPosition >= 0) { declaringTypeQualification = CharOperation.subarray(declaringTypePart, 0, lastDotPosition); if (declaringTypeQualification.length == 1 && declaringTypeQualification[0] == '*') declaringTypeQualification = null; declaringTypeSimpleName = CharOperation.subarray(declaringTypePart, lastDotPosition + 1, declaringTypePart.length); } else { declaringTypeSimpleName = declaringTypePart; } if (declaringTypeSimpleName.length == 1 && declaringTypeSimpleName[0] == '*') declaringTypeSimpleName = null; } // extract parameter types infos if (parameterCount >= 0) { parameterTypeQualifications = new char[parameterCount][]; parameterTypeSimpleNames = new char[parameterCount][]; parameterTypeSignatures = new String[parameterCount]; for (int i = 0; i < parameterCount; i++) { // get parameter type part and signature char[] parameterTypePart = null; try { if (parameterTypes != null) { parameterTypeSignatures[i] = Signature.createTypeSignature(parameterTypes[i], false); if (parameterTypeSignatures[i].indexOf(Signature.C_GENERIC_START) < 0) { parameterTypePart = parameterTypes[i].toCharArray(); } else { parameterTypePart = Signature.toCharArray( Signature.getTypeErasure(parameterTypeSignatures[i].toCharArray())); } } } catch (IllegalArgumentException iae) { // string is not a valid type syntax return null; } int lastDotPosition = parameterTypePart == null ? -1 : CharOperation.lastIndexOf('.', parameterTypePart); if (parameterTypePart != null && lastDotPosition >= 0) { parameterTypeQualifications[i] = CharOperation.subarray(parameterTypePart, 0, lastDotPosition); if (parameterTypeQualifications[i].length == 1 && parameterTypeQualifications[i][0] == '*') { parameterTypeQualifications[i] = null; } else { // prefix with a '*' as the full qualification could be bigger (because of an import) parameterTypeQualifications[i] = CharOperation.concat(IIndexConstants.ONE_STAR, parameterTypeQualifications[i]); } parameterTypeSimpleNames[i] = CharOperation.subarray(parameterTypePart, lastDotPosition + 1, parameterTypePart.length); } else { parameterTypeQualifications[i] = null; parameterTypeSimpleNames[i] = parameterTypePart; } if (parameterTypeSimpleNames[i].length == 1 && parameterTypeSimpleNames[i][0] == '*') parameterTypeSimpleNames[i] = null; } } // extract return type infos if (returnType != null) { // get return type part and signature char[] returnTypePart = null; try { returnTypeSignature = Signature.createTypeSignature(returnType, false); if (returnTypeSignature.indexOf(Signature.C_GENERIC_START) < 0) { returnTypePart = returnType.toCharArray(); } else { returnTypePart = Signature .toCharArray(Signature.getTypeErasure(returnTypeSignature.toCharArray())); } } catch (IllegalArgumentException iae) { // declaring type is invalid return null; } int lastDotPosition = CharOperation.lastIndexOf('.', returnTypePart); if (lastDotPosition >= 0) { returnTypeQualification = CharOperation.subarray(returnTypePart, 0, lastDotPosition); if (returnTypeQualification.length == 1 && returnTypeQualification[0] == '*') { returnTypeQualification = null; } else { // because of an import returnTypeQualification = CharOperation.concat(IIndexConstants.ONE_STAR, returnTypeQualification); } returnTypeSimpleName = CharOperation.subarray(returnTypePart, lastDotPosition + 1, returnTypePart.length); } else { returnTypeSimpleName = returnTypePart; } if (returnTypeSimpleName.length == 1 && returnTypeSimpleName[0] == '*') returnTypeSimpleName = null; } // Create method/constructor pattern if (isConstructor) { return new ConstructorPattern(declaringTypeSimpleName, declaringTypeQualification, declaringTypeSignature, parameterTypeQualifications, parameterTypeSimpleNames, parameterTypeSignatures, typeArguments, limitTo, matchRule); } else { return new MethodPattern(selectorChars, declaringTypeQualification, declaringTypeSimpleName, declaringTypeSignature, returnTypeQualification, returnTypeSimpleName, returnTypeSignature, parameterTypeQualifications, parameterTypeSimpleNames, parameterTypeSignatures, typeArguments, limitTo, matchRule); } }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.BasicSearchEngine.java
License:Open Source License
/** * Searches for all top-level types and member types in the given scope using a case sensitive exact match * with the given qualified names and type names. * * @see org.eclipse.jdt.core.search.SearchEngine#searchAllTypeNames(char[][], char[][], org.eclipse.jdt.core.search.IJavaSearchScope, * org.eclipse.jdt.core.search.TypeNameRequestor, int, org.eclipse.core.runtime.IProgressMonitor) * for detailed comment/* ww w.j a va 2 s .co m*/ */ public void searchAllTypeNames(final char[][] qualifications, final char[][] typeNames, final int matchRule, int searchFor, IJavaSearchScope scope, final IRestrictedAccessTypeRequestor nameRequestor, int waitingPolicy, IProgressMonitor progressMonitor) throws JavaModelException { // Debug if (VERBOSE) { Util.verbose( "BasicSearchEngine.searchAllTypeNames(char[][], char[][], int, int, IJavaSearchScope, IRestrictedAccessTypeRequestor, int, IProgressMonitor)"); //$NON-NLS-1$ Util.verbose(" - package name: " + (qualifications == null ? "null" : new String(CharOperation.concatWith(qualifications, ',')))); //$NON-NLS-1$ //$NON-NLS-2$ Util.verbose(" - type name: " + (typeNames == null ? "null" : new String(CharOperation.concatWith(typeNames, ',')))); //$NON-NLS-1$ //$NON-NLS-2$ Util.verbose(" - match rule: " + getMatchRuleString(matchRule)); //$NON-NLS-1$ Util.verbose(" - search for: " + searchFor); //$NON-NLS-1$ Util.verbose(" - scope: " + scope); //$NON-NLS-1$ } // Create pattern final char typeSuffix; switch (searchFor) { case IJavaSearchConstants.CLASS: typeSuffix = IIndexConstants.CLASS_SUFFIX; break; case IJavaSearchConstants.CLASS_AND_INTERFACE: typeSuffix = IIndexConstants.CLASS_AND_INTERFACE_SUFFIX; break; case IJavaSearchConstants.CLASS_AND_ENUM: typeSuffix = IIndexConstants.CLASS_AND_ENUM_SUFFIX; break; case IJavaSearchConstants.INTERFACE: typeSuffix = IIndexConstants.INTERFACE_SUFFIX; break; case IJavaSearchConstants.INTERFACE_AND_ANNOTATION: typeSuffix = IIndexConstants.INTERFACE_AND_ANNOTATION_SUFFIX; break; case IJavaSearchConstants.ENUM: typeSuffix = IIndexConstants.ENUM_SUFFIX; break; case IJavaSearchConstants.ANNOTATION_TYPE: typeSuffix = IIndexConstants.ANNOTATION_TYPE_SUFFIX; break; default: typeSuffix = IIndexConstants.TYPE_SUFFIX; break; } final MultiTypeDeclarationPattern pattern = new MultiTypeDeclarationPattern(qualifications, typeNames, typeSuffix, matchRule); // Get working copy path(s). Store in a single string in case of only one to optimize comparison in requestor final HashSet workingCopyPaths = new HashSet(); String workingCopyPath = null; ICompilationUnit[] copies = getWorkingCopies(); final int copiesLength = copies == null ? 0 : copies.length; if (copies != null) { if (copiesLength == 1) { workingCopyPath = copies[0].getPath().toString(); } else { for (int i = 0; i < copiesLength; i++) { ICompilationUnit workingCopy = copies[i]; workingCopyPaths.add(workingCopy.getPath().toString()); } } } final String singleWkcpPath = workingCopyPath; // Index requestor IndexQueryRequestor searchRequestor = new IndexQueryRequestor() { public boolean acceptIndexMatch(String documentPath, SearchPattern indexRecord, SearchParticipant participant, AccessRuleSet access) { // Filter unexpected types QualifiedTypeDeclarationPattern record = (QualifiedTypeDeclarationPattern) indexRecord; if (record.enclosingTypeNames == IIndexConstants.ONE_ZERO_CHAR) { return true; // filter out local and anonymous classes } switch (copiesLength) { case 0: break; case 1: if (singleWkcpPath.equals(documentPath)) { return true; // filter out *the* working copy } break; default: if (workingCopyPaths.contains(documentPath)) { return true; // filter out working copies } break; } // Accept document path AccessRestriction accessRestriction = null; if (access != null) { // Compute document relative path int qualificationLength = (record.qualification == null || record.qualification.length == 0) ? 0 : record.qualification.length + 1; int nameLength = record.simpleName == null ? 0 : record.simpleName.length; char[] path = new char[qualificationLength + nameLength]; int pos = 0; if (qualificationLength > 0) { System.arraycopy(record.qualification, 0, path, pos, qualificationLength - 1); CharOperation.replace(path, '.', '/'); path[qualificationLength - 1] = '/'; pos += qualificationLength; } if (nameLength > 0) { System.arraycopy(record.simpleName, 0, path, pos, nameLength); pos += nameLength; } // Update access restriction if path is not empty if (pos > 0) { accessRestriction = access.getViolatedRestriction(path); } } nameRequestor.acceptType(record.modifiers, record.pkg, record.simpleName, record.enclosingTypeNames, documentPath, accessRestriction); return true; } }; try { if (progressMonitor != null) { progressMonitor.beginTask(Messages.engine_searching, 100); } // add type names from indexes indexManager.performConcurrentJob( new PatternSearchJob(pattern, getDefaultSearchParticipant(indexManager), // Java search only scope, searchRequestor, indexManager), waitingPolicy, progressMonitor == null ? null : new SubProgressMonitor(progressMonitor, 100)); // add type names from working copies if (copies != null) { for (int i = 0, length = copies.length; i < length; i++) { ICompilationUnit workingCopy = copies[i]; final String path = workingCopy.getPath().toString(); if (workingCopy.isConsistent()) { IPackageDeclaration[] packageDeclarations = workingCopy.getPackageDeclarations(); char[] packageDeclaration = packageDeclarations.length == 0 ? CharOperation.NO_CHAR : packageDeclarations[0].getElementName().toCharArray(); IType[] allTypes = workingCopy.getAllTypes(); for (int j = 0, allTypesLength = allTypes.length; j < allTypesLength; j++) { IType type = allTypes[j]; IJavaElement parent = type.getParent(); char[][] enclosingTypeNames; char[] qualification = packageDeclaration; if (parent instanceof IType) { char[] parentQualifiedName = ((IType) parent).getTypeQualifiedName('.') .toCharArray(); enclosingTypeNames = CharOperation.splitOn('.', parentQualifiedName); qualification = CharOperation.concat(qualification, parentQualifiedName); } else { enclosingTypeNames = CharOperation.NO_CHAR_CHAR; } char[] simpleName = type.getElementName().toCharArray(); char suffix = IIndexConstants.TYPE_SUFFIX; if (type.isClass()) { suffix = IIndexConstants.CLASS_SUFFIX; } else if (type.isInterface()) { suffix = IIndexConstants.INTERFACE_SUFFIX; } else if (type.isEnum()) { suffix = IIndexConstants.ENUM_SUFFIX; } else if (type.isAnnotation()) { suffix = IIndexConstants.ANNOTATION_TYPE_SUFFIX; } if (pattern.matchesDecodedKey(new QualifiedTypeDeclarationPattern(qualification, simpleName, suffix, matchRule))) { nameRequestor.acceptType(type.getFlags(), packageDeclaration, simpleName, enclosingTypeNames, path, null); } } } else { Parser basicParser = getParser(); org.eclipse.jdt.internal.compiler.env.ICompilationUnit unit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) workingCopy; CompilationResult compilationUnitResult = new CompilationResult(unit, 0, 0, this.compilerOptions.maxProblemsPerUnit); CompilationUnitDeclaration parsedUnit = basicParser.dietParse(unit, compilationUnitResult); if (parsedUnit != null) { final char[] packageDeclaration = parsedUnit.currentPackage == null ? CharOperation.NO_CHAR : CharOperation.concatWith(parsedUnit.currentPackage.getImportName(), '.'); class AllTypeDeclarationsVisitor extends ASTVisitor { public boolean visit(TypeDeclaration typeDeclaration, BlockScope blockScope) { return false; // no local/anonymous type } public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope compilationUnitScope) { SearchPattern decodedPattern = new QualifiedTypeDeclarationPattern( packageDeclaration, typeDeclaration.name, convertTypeKind(TypeDeclaration.kind(typeDeclaration.modifiers)), matchRule); if (pattern.matchesDecodedKey(decodedPattern)) { nameRequestor.acceptType(typeDeclaration.modifiers, packageDeclaration, typeDeclaration.name, CharOperation.NO_CHAR_CHAR, path, null); } return true; } public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope classScope) { // compute enclosing type names char[] qualification = packageDeclaration; TypeDeclaration enclosing = memberTypeDeclaration.enclosingType; char[][] enclosingTypeNames = CharOperation.NO_CHAR_CHAR; while (enclosing != null) { qualification = CharOperation.concat(qualification, enclosing.name, '.'); enclosingTypeNames = CharOperation .arrayConcat(new char[][] { enclosing.name }, enclosingTypeNames); if ((enclosing.bits & ASTNode.IsMemberType) != 0) { enclosing = enclosing.enclosingType; } else { enclosing = null; } } SearchPattern decodedPattern = new QualifiedTypeDeclarationPattern( qualification, memberTypeDeclaration.name, convertTypeKind(TypeDeclaration.kind(memberTypeDeclaration.modifiers)), matchRule); if (pattern.matchesDecodedKey(decodedPattern)) { nameRequestor.acceptType(memberTypeDeclaration.modifiers, packageDeclaration, memberTypeDeclaration.name, enclosingTypeNames, path, null); } return true; } } parsedUnit.traverse(new AllTypeDeclarationsVisitor(), parsedUnit.scope); } } } } } finally { if (progressMonitor != null) { progressMonitor.done(); } } }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ClassFileMatchLocator.java
License:Open Source License
boolean matchTypeDeclaration(TypeDeclarationPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) { if (!(binaryInfo instanceof IBinaryType)) return false; IBinaryType type = (IBinaryType) binaryInfo; char[] fullyQualifiedTypeName = convertClassFileFormat(type.getName()); boolean qualifiedPattern = pattern instanceof QualifiedTypeDeclarationPattern; if (pattern.enclosingTypeNames == null || qualifiedPattern) { char[] simpleName = (pattern.getMatchMode() == SearchPattern.R_PREFIX_MATCH) ? CharOperation.concat(pattern.simpleName, IIndexConstants.ONE_STAR) : pattern.simpleName;//from w ww .j a v a2 s.co m char[] pkg = qualifiedPattern ? ((QualifiedTypeDeclarationPattern) pattern).qualification : pattern.pkg; if (!checkTypeName(simpleName, pkg, fullyQualifiedTypeName, pattern.isCaseSensitive(), pattern.isCamelCase())) return false; } else { char[] enclosingTypeName = CharOperation.concatWith(pattern.enclosingTypeNames, '.'); char[] patternString = pattern.pkg == null ? enclosingTypeName : CharOperation.concat(pattern.pkg, enclosingTypeName, '.'); if (!checkTypeName(pattern.simpleName, patternString, fullyQualifiedTypeName, pattern.isCaseSensitive(), pattern.isCamelCase())) return false; } int kind = TypeDeclaration.kind(type.getModifiers()); switch (pattern.typeSuffix) { case CLASS_SUFFIX: return kind == TypeDeclaration.CLASS_DECL; case INTERFACE_SUFFIX: return kind == TypeDeclaration.INTERFACE_DECL; case ENUM_SUFFIX: return kind == TypeDeclaration.ENUM_DECL; case ANNOTATION_TYPE_SUFFIX: return kind == TypeDeclaration.ANNOTATION_TYPE_DECL; case CLASS_AND_INTERFACE_SUFFIX: return kind == TypeDeclaration.CLASS_DECL || kind == TypeDeclaration.INTERFACE_DECL; case CLASS_AND_ENUM_SUFFIX: return kind == TypeDeclaration.CLASS_DECL || kind == TypeDeclaration.ENUM_DECL; case INTERFACE_AND_ANNOTATION_SUFFIX: return kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL; case TYPE_SUFFIX: // nothing } return true; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ConstructorPattern.java
License:Open Source License
/** * Constructor entries are encoded as TypeName '/' Arity: * e.g. 'X/0'/*from w w w. j a v a2 s. c o m*/ */ public static char[] createIndexKey(char[] typeName, int argCount) { char[] countChars = argCount < 10 ? COUNTS[argCount] : ("/" + String.valueOf(argCount)).toCharArray(); //$NON-NLS-1$ return CharOperation.concat(typeName, countChars); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ConstructorPattern.java
License:Open Source License
public EntryResult[] queryIn(Index index) throws IOException { char[] key = this.declaringSimpleName; // can be null int matchRule = getMatchRule(); switch (getMatchMode()) { case R_EXACT_MATCH: if (this.declaringSimpleName != null && this.parameterCount >= 0 && !this.varargs) { key = createIndexKey(this.declaringSimpleName, this.parameterCount); }//ww w .j ava2 s. c o m matchRule &= ~R_EXACT_MATCH; matchRule |= R_PREFIX_MATCH; break; case R_PREFIX_MATCH: // do a prefix query with the declaringSimpleName break; case R_PATTERN_MATCH: if (this.parameterCount >= 0 && !this.varargs) { key = CharOperation.concat( createIndexKey(this.declaringSimpleName == null ? ONE_STAR : this.declaringSimpleName, this.parameterCount), ONE_STAR); } else if (this.declaringSimpleName != null && this.declaringSimpleName[this.declaringSimpleName.length - 1] != '*') { key = CharOperation.concat(this.declaringSimpleName, ONE_STAR, SEPARATOR); } else if (key != null) { key = CharOperation.concat(key, ONE_STAR); } // else do a pattern query with just the declaringSimpleName break; case R_REGEXP_MATCH: // TODO (frederic) implement regular expression match break; case R_CAMELCASE_MATCH: case R_CAMELCASE_SAME_PART_COUNT_MATCH: // do a prefix query with the declaringSimpleName break; } return index.query(getIndexCategories(), key, matchRule); // match rule is irrelevant when the key is null }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
/** * Creates an IMethod from the given method declaration and type. */// w ww . j a v a 2 s. c o m protected IJavaElement createHandle(AbstractMethodDeclaration method, IJavaElement parent) { if (!(parent instanceof IType)) return parent; IType type = (IType) parent; Argument[] arguments = method.arguments; int argCount = arguments == null ? 0 : arguments.length; if (type.isBinary()) { // don't cache the methods of the binary type // fall thru if its a constructor with a synthetic argument... find it the slower way ClassFileReader reader = classFileReader(type); if (reader != null) { // build arguments names boolean firstIsSynthetic = false; if (reader.isMember() && method.isConstructor() && !Flags.isStatic(reader.getModifiers())) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=48261 firstIsSynthetic = true; argCount++; } char[][] argumentTypeNames = new char[argCount][]; for (int i = 0; i < argCount; i++) { char[] typeName = null; if (i == 0 && firstIsSynthetic) { typeName = type.getDeclaringType().getFullyQualifiedName().toCharArray(); } else if (arguments != null) { TypeReference typeRef = arguments[firstIsSynthetic ? i - 1 : i].type; typeName = CharOperation.concatWith(typeRef.getTypeName(), '.'); for (int k = 0, dim = typeRef.dimensions(); k < dim; k++) typeName = CharOperation.concat(typeName, new char[] { '[', ']' }); } if (typeName == null) { // invalid type name return null; } argumentTypeNames[i] = typeName; } // return binary method IMethod binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames); if (binaryMethod == null) { // when first attempt fails, try with similar matches if any... PossibleMatch similarMatch = this.currentPossibleMatch.getSimilarMatch(); while (similarMatch != null) { type = ((ClassFile) similarMatch.openable).getType(); binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames); if (binaryMethod != null) { return binaryMethod; } similarMatch = similarMatch.getSimilarMatch(); } } return binaryMethod; } if (BasicSearchEngine.VERBOSE) { System.out.println("Not able to createHandle for the method " + //$NON-NLS-1$ CharOperation.charToString(method.selector) + " May miss some results"); //$NON-NLS-1$ } return null; } String[] parameterTypeSignatures = new String[argCount]; if (arguments != null) { for (int i = 0; i < argCount; i++) { TypeReference typeRef = arguments[i].type; char[] typeName = CharOperation.concatWith(typeRef.getParameterizedTypeName(), '.'); parameterTypeSignatures[i] = Signature.createTypeSignature(typeName, false); } } return createMethodHandle(type, new String(method.selector), parameterTypeSignatures); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
/** * Creates an IImportDeclaration from the given import statement *//*from w w w .j av a 2 s . co m*/ protected IJavaElement createImportHandle(ImportReference importRef) { char[] importName = CharOperation.concatWith(importRef.getImportName(), '.'); if ((importRef.bits & ASTNode.OnDemand) != 0) importName = CharOperation.concat(importName, ".*".toCharArray()); //$NON-NLS-1$ Openable openable = this.currentPossibleMatch.openable; if (openable instanceof CompilationUnit) return ((CompilationUnit) openable).getImport(new String(importName)); // binary types do not contain import statements so just answer the top-level type as the element IType binaryType = ((ClassFile) openable).getType(); String typeName = binaryType.getElementName(); int lastDollar = typeName.lastIndexOf('$'); if (lastDollar == -1) return binaryType; return createTypeHandle(typeName.substring(0, lastDollar)); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MethodLocator.java
License:Open Source License
protected void reportDeclaration(MethodBinding methodBinding, MatchLocator locator, SimpleSet knownMethods) throws CoreException { ReferenceBinding declaringClass = methodBinding.declaringClass; IType type = locator.lookupType(declaringClass); if (type == null) return; // case of a secondary type // Report match for binary if (type.isBinary()) { IMethod method = null;/* ww w. j a v a2 s. com*/ TypeBinding[] parameters = methodBinding.original().parameters; int parameterLength = parameters.length; char[][] parameterTypes = new char[parameterLength][]; for (int i = 0; i < parameterLength; i++) { char[] typeName = parameters[i].qualifiedSourceName(); for (int j = 0, dim = parameters[i].dimensions(); j < dim; j++) { typeName = CharOperation.concat(typeName, new char[] { '[', ']' }); } parameterTypes[i] = typeName; } method = locator.createBinaryMethodHandle(type, methodBinding.selector, parameterTypes); if (method == null || knownMethods.addIfNotIncluded(method) == null) return; IResource resource = type.getResource(); if (resource == null) resource = type.getJavaProject().getProject(); IBinaryType info = locator.getBinaryInfo((org.eclipse.jdt.internal.core.ClassFile) type.getClassFile(), resource); locator.reportBinaryMemberDeclaration(resource, method, methodBinding, info, SearchMatch.A_ACCURATE); return; } // When source is available, report match if method is found in the declaring type IResource resource = type.getResource(); if (declaringClass instanceof ParameterizedTypeBinding) declaringClass = ((ParameterizedTypeBinding) declaringClass).genericType(); ClassScope scope = ((SourceTypeBinding) declaringClass).scope; if (scope != null) { TypeDeclaration typeDecl = scope.referenceContext; AbstractMethodDeclaration methodDecl = typeDecl.declarationOf(methodBinding.original()); if (methodDecl != null) { // Create method handle from method declaration String methodName = new String(methodBinding.selector); Argument[] arguments = methodDecl.arguments; int length = arguments == null ? 0 : arguments.length; String[] parameterTypes = new String[length]; for (int i = 0; i < length; i++) { char[][] typeName = arguments[i].type.getParameterizedTypeName(); parameterTypes[i] = Signature.createTypeSignature(CharOperation.concatWith(typeName, '.'), false); } IMethod method = type.getMethod(methodName, parameterTypes); if (method == null || knownMethods.addIfNotIncluded(method) == null) return; // Create and report corresponding match int offset = methodDecl.sourceStart; this.match = new MethodDeclarationMatch(method, SearchMatch.A_ACCURATE, offset, methodDecl.sourceEnd - offset + 1, locator.getParticipant(), resource); locator.report(this.match); } } }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MethodPattern.java
License:Open Source License
/** * Method entries are encoded as selector '/' Arity: * e.g. 'foo/0'/*from w w w .j a v a 2 s . com*/ */ public static char[] createIndexKey(char[] selector, int argCount) { char[] countChars = argCount < 10 ? COUNTS[argCount] : ("/" + String.valueOf(argCount)).toCharArray(); //$NON-NLS-1$ return CharOperation.concat(selector, countChars); }