<s> package org . eclipse . jdt . core . search ; import java . io . IOException ; import org . eclipse . core . runtime . IProgressMonitor ; import org . eclipse . core . runtime . OperationCanceledException ; import org . eclipse . jdt . core . * ; import org . eclipse . jdt . core . compiler . * ; import org . eclipse . jdt . internal . compiler . classfmt . ClassFileConstants ; import org . eclipse . jdt . internal . compiler . env . AccessRuleSet ; import org . eclipse . jdt . internal . compiler . parser . Scanner ; import org . eclipse . jdt . internal . compiler . parser . ScannerHelper ; import org . eclipse . jdt . internal . compiler . parser . TerminalTokens ; import org . eclipse . jdt . internal . core . LocalVariable ; import org . eclipse . jdt . internal . core . index . EntryResult ; import org . eclipse . jdt . internal . core . index . Index ; import org . eclipse . jdt . internal . core . search . HierarchyScope ; import org . eclipse . jdt . internal . core . search . IndexQueryRequestor ; import org . eclipse . jdt . internal . core . search . JavaSearchScope ; import org . eclipse . jdt . internal . core . search . StringOperation ; import org . eclipse . jdt . internal . core . search . indexing . IIndexConstants ; import org . eclipse . jdt . internal . core . search . matching . * ; public abstract class SearchPattern { public static final int R_EXACT_MATCH = 0 ; public static final int R_PREFIX_MATCH = 0x0001 ; public static final int R_PATTERN_MATCH = 0x0002 ; public static final int R_REGEXP_MATCH = 0x0004 ; public static final int R_CASE_SENSITIVE = 0x0008 ; public static final int R_ERASURE_MATCH = 0x0010 ; public static final int R_EQUIVALENT_MATCH = 0x0020 ; public static final int R_FULL_MATCH = 0x0040 ; public static final int R_CAMELCASE_MATCH = 0x0080 ; public static final int R_CAMELCASE_SAME_PART_COUNT_MATCH = 0x0100 ; private static final int MODE_MASK = R_EXACT_MATCH | R_PREFIX_MATCH | R_PATTERN_MATCH | R_REGEXP_MATCH | R_CAMELCASE_MATCH | R_CAMELCASE_SAME_PART_COUNT_MATCH ; private int matchRule ; public IJavaElement focus ; public int kind ; public boolean mustResolve = true ; public SearchPattern ( int matchRule ) { this . matchRule = matchRule ; if ( ( matchRule & ( R_EQUIVALENT_MATCH | R_ERASURE_MATCH ) ) == 0 ) { this . matchRule |= R_FULL_MATCH ; } if ( ( matchRule & R_CAMELCASE_MATCH ) != 0 ) { this . matchRule &= ~ R_CAMELCASE_SAME_PART_COUNT_MATCH ; this . matchRule &= ~ R_PREFIX_MATCH ; } else if ( ( matchRule & R_CAMELCASE_SAME_PART_COUNT_MATCH ) != 0 ) { this . matchRule &= ~ R_PREFIX_MATCH ; } } public void acceptMatch ( String relativePath , String containerPath , char separator , SearchPattern pattern , IndexQueryRequestor requestor , SearchParticipant participant , IJavaSearchScope scope ) { acceptMatch ( relativePath , containerPath , separator , pattern , requestor , participant , scope , null ) ; } public void acceptMatch ( String relativePath , String containerPath , char separator , SearchPattern pattern , IndexQueryRequestor requestor , SearchParticipant participant , IJavaSearchScope scope , IProgressMonitor monitor ) { if ( scope instanceof JavaSearchScope ) { JavaSearchScope javaSearchScope = ( JavaSearchScope ) scope ; AccessRuleSet access = javaSearchScope . getAccessRuleSet ( relativePath , containerPath ) ; if ( access != JavaSearchScope . NOT_ENCLOSED ) { StringBuffer documentPath = new StringBuffer ( containerPath . length ( ) + 1 + relativePath . length ( ) ) ; documentPath . append ( containerPath ) ; documentPath . append ( separator ) ; documentPath . append ( relativePath ) ; if ( ! requestor . acceptIndexMatch ( documentPath . toString ( ) , pattern , participant , access ) ) throw new OperationCanceledException ( ) ; } } else { StringBuffer buffer = new StringBuffer ( containerPath . length ( ) + 1 + relativePath . length ( ) ) ; buffer . append ( containerPath ) ; buffer . append ( separator ) ; buffer . append ( relativePath ) ; String documentPath = buffer . toString ( ) ; boolean encloses = ( scope instanceof HierarchyScope ) ? ( ( HierarchyScope ) scope ) . encloses ( documentPath , monitor ) : scope . encloses ( documentPath ) ; if ( encloses ) if ( ! requestor . acceptIndexMatch ( documentPath , pattern , participant , null ) ) throw new OperationCanceledException ( ) ; } } public SearchPattern currentPattern ( ) { return this ; } public static final boolean camelCaseMatch ( String pattern , String name ) { if ( pattern == null ) return true ; if ( name == null ) return false ; return camelCaseMatch ( pattern , 0 , pattern . length ( ) , name , 0 , name . length ( ) , false ) ; } public static final boolean camelCaseMatch ( String pattern , String name , boolean samePartCount ) { if ( pattern == null ) return true ; if ( name == null ) return false ; return camelCaseMatch ( pattern , 0 , pattern . length ( ) , name , 0 , name . length ( ) , samePartCount ) ; } public static final boolean camelCaseMatch ( String pattern , int patternStart , int patternEnd , String name , int nameStart , int nameEnd ) { return camelCaseMatch ( pattern , patternStart , patternEnd , name , nameStart , nameEnd , false ) ; } public static final boolean camelCaseMatch ( String pattern , int patternStart , int patternEnd , String name , int nameStart , int nameEnd , boolean samePartCount ) { return StringOperation . getCamelCaseMatchingRegions ( pattern , patternStart , patternEnd , name , nameStart , nameEnd , samePartCount ) != null ; } public static final int [ ] getMatchingRegions ( String pattern , String name , int matchRule ) { if ( name == null ) return null ; final int nameLength = name . length ( ) ; if ( pattern == null ) { return new int [ ] { 0 , nameLength } ; } final int patternLength = pattern . length ( ) ; boolean countMatch = false ; switch ( matchRule ) { case SearchPattern . R_EXACT_MATCH : if ( patternLength == nameLength && pattern . equalsIgnoreCase ( name ) ) { return new int [ ] { 0 , patternLength } ; } break ; case SearchPattern . R_EXACT_MATCH | SearchPattern . R_CASE_SENSITIVE : if ( patternLength == nameLength && pattern . equals ( name ) ) { return new int [ ] { 0 , patternLength } ; } break ; case SearchPattern . R_PREFIX_MATCH : if ( patternLength <= nameLength && name . substring ( 0 , patternLength ) . equalsIgnoreCase ( pattern ) ) { return new int [ ] { 0 , patternLength } ; } break ; case SearchPattern . R_PREFIX_MATCH | SearchPattern . R_CASE_SENSITIVE : if ( name . startsWith ( pattern ) ) { return new int [ ] { 0 , patternLength } ; } break ; case SearchPattern . R_CAMELCASE_SAME_PART_COUNT_MATCH : countMatch = true ; case SearchPattern . R_CAMELCASE_MATCH : if ( patternLength <= nameLength ) { int [ ] regions = StringOperation . getCamelCaseMatchingRegions ( pattern , 0 , patternLength , name , 0 , nameLength , countMatch ) ; if ( regions != null ) return regions ; if ( name . substring ( 0 , patternLength ) . equalsIgnoreCase ( pattern ) ) { return new int [ ] { 0 , patternLength } ; } } break ; case SearchPattern . R_CAMELCASE_SAME_PART_COUNT_MATCH | SearchPattern . R_CASE_SENSITIVE : countMatch = true ; case SearchPattern . R_CAMELCASE_MATCH | SearchPattern . R_CASE_SENSITIVE : if ( patternLength <= nameLength ) { return StringOperation . getCamelCaseMatchingRegions ( pattern , 0 , patternLength , name , 0 , nameLength , countMatch ) ; } break ; case SearchPattern . R_PATTERN_MATCH : return StringOperation . getPatternMatchingRegions ( pattern , 0 , patternLength , name , 0 , nameLength , false ) ; case SearchPattern . R_PATTERN_MATCH | SearchPattern . R_CASE_SENSITIVE : return StringOperation . getPatternMatchingRegions ( pattern , 0 , patternLength , name , 0 , nameLength , true ) ; } return null ; } public static SearchPattern createAndPattern ( SearchPattern leftPattern , SearchPattern rightPattern ) { return new AndPattern ( leftPattern , rightPattern ) ; } private static SearchPattern createFieldPattern ( String patternString , int limitTo , int matchRule ) { Scanner scanner = new Scanner ( false , true , false , ClassFileConstants . JDK1_3 , null , null , true ) ; scanner . setSource ( patternString . toCharArray ( ) ) ; final int InsideDeclaringPart = 1 ; final int InsideType = 2 ; int lastToken = - 1 ; String declaringType = null , fieldName = null ; String type = null ; int mode = InsideDeclaringPart ; int token ; try { token = scanner . getNextToken ( ) ; } catch ( InvalidInputException e ) { return null ; } while ( token != TerminalTokens . TokenNameEOF ) { switch ( mode ) { 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 : if ( fieldName == null ) fieldName = scanner . getCurrentTokenString ( ) ; else fieldName += scanner . getCurrentTokenString ( ) ; } break ; case InsideType : switch ( token ) { case TerminalTokens . TokenNameWHITESPACE : break ; default : 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 ; 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 ; } 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 { 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 ; } return new FieldPattern ( fieldNameChars , declaringTypeQualification , declaringTypeSimpleName , typeQualification , typeSimpleName , limitTo , matchRule ) ; } private static SearchPattern createMethodOrConstructorPattern ( String patternString , int limitTo , int matchRule , boolean isConstructor ) { Scanner scanner = new Scanner ( false , true , false , ClassFileConstants . JDK1_3 , null , null , true ) ; 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 ; 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 ) { 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 ; if ( declaringType == null ) { if ( selector == null ) return null ; 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 : if ( selector == null ) selector = scanner . getCurrentTokenString ( ) ; else selector += scanner . getCurrentTokenString ( ) ; break ; } } else { if ( declaringType == null ) return null ; 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 ; case InsideTypeArguments : if ( typeArgumentsString == null ) return null ; 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 ; typeArguments = Signature . getTypeArguments ( Signature . createTypeSignature ( pseudoType , false ) . toCharArray ( ) ) ; mode = InsideSelector ; } break ; case TerminalTokens . TokenNameLESS : argCount ++ ; break ; } break ; 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 ; default : if ( parameterType == null ) parameterType = scanner . getCurrentTokenString ( ) ; else parameterType += scanner . getCurrentTokenString ( ) ; } } else { if ( parameterType == null ) return null ; 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 ; 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 ; default : if ( returnType == null ) returnType = scanner . getCurrentTokenString ( ) ; else returnType += scanner . getCurrentTokenString ( ) ; } } else { if ( returnType == null ) return null ; 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 ; } } if ( parameterCount > 0 && ! foundClosingParenthesis ) return null ; if ( argCount > 0 ) return null ; char [ ] selectorChars = null ; if ( isConstructor ) { if ( declaringType == null ) declaringType = selector ; else if ( selector != null ) declaringType += '.' + selector ; } else { 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 ; String declaringTypeSignature = null ; String returnTypeSignature = null ; String [ ] parameterTypeSignatures = null ; if ( declaringType != null ) { 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 ) { 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 ; } if ( parameterCount >= 0 ) { parameterTypeQualifications = new char [ parameterCount ] [ ] ; parameterTypeSimpleNames = new char [ parameterCount ] [ ] ; parameterTypeSignatures = new String [ parameterCount ] ; for ( int i = 0 ; i < parameterCount ; i ++ ) { 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 ) { 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 { 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 ; } } if ( returnType != null ) { 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 ) { 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 { 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 ; } 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 ) ; } } public static SearchPattern createOrPattern ( SearchPattern leftPattern , SearchPattern rightPattern ) { return new OrPattern ( leftPattern , rightPattern ) ; } private static SearchPattern createPackagePattern ( String patternString , int limitTo , int matchRule ) { switch ( limitTo ) { case IJavaSearchConstants . DECLARATIONS : return new PackageDeclarationPattern ( patternString . toCharArray ( ) , matchRule ) ; case IJavaSearchConstants . REFERENCES : return new PackageReferencePattern ( patternString . toCharArray ( ) , matchRule ) ; case IJavaSearchConstants . ALL_OCCURRENCES : return new OrPattern ( new PackageDeclarationPattern ( patternString . toCharArray ( ) , matchRule ) , new PackageReferencePattern ( patternString . toCharArray ( ) , matchRule ) ) ; } return null ; } public static SearchPattern createPattern ( String stringPattern , int searchFor , int limitTo , int matchRule ) { if ( stringPattern == null || stringPattern . length ( ) == 0 ) return null ; if ( ( matchRule = validateMatchRule ( stringPattern , matchRule ) ) == - 1 ) { return null ; } limitTo &= ~ ( IJavaSearchConstants . IGNORE_DECLARING_TYPE + IJavaSearchConstants . IGNORE_RETURN_TYPE ) ; switch ( searchFor ) { case IJavaSearchConstants . CLASS : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . CLASS_SUFFIX ) ; case IJavaSearchConstants . CLASS_AND_INTERFACE : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . CLASS_AND_INTERFACE_SUFFIX ) ; case IJavaSearchConstants . CLASS_AND_ENUM : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . CLASS_AND_ENUM_SUFFIX ) ; case IJavaSearchConstants . INTERFACE : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . INTERFACE_SUFFIX ) ; case IJavaSearchConstants . INTERFACE_AND_ANNOTATION : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . INTERFACE_AND_ANNOTATION_SUFFIX ) ; case IJavaSearchConstants . ENUM : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . ENUM_SUFFIX ) ; case IJavaSearchConstants . ANNOTATION_TYPE : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . ANNOTATION_TYPE_SUFFIX ) ; case IJavaSearchConstants . TYPE : return createTypePattern ( stringPattern , limitTo , matchRule , IIndexConstants . TYPE_SUFFIX ) ; case IJavaSearchConstants . METHOD : return createMethodOrConstructorPattern ( stringPattern , limitTo , matchRule , false ) ; case IJavaSearchConstants . CONSTRUCTOR : return createMethodOrConstructorPattern ( stringPattern , limitTo , matchRule , true ) ; case IJavaSearchConstants . FIELD : return createFieldPattern ( stringPattern , limitTo , matchRule ) ; case IJavaSearchConstants . PACKAGE : return createPackagePattern ( stringPattern , limitTo , matchRule ) ; } return null ; } public static SearchPattern createPattern ( IJavaElement element , int limitTo ) { return createPattern ( element , limitTo , R_EXACT_MATCH | R_CASE_SENSITIVE | R_ERASURE_MATCH ) ; } public static SearchPattern createPattern ( IJavaElement element , int limitTo , int matchRule ) { SearchPattern searchPattern = null ; int lastDot ; boolean ignoreDeclaringType = false ; boolean ignoreReturnType = false ; int maskedLimitTo = limitTo & ~ ( IJavaSearchConstants . IGNORE_DECLARING_TYPE + IJavaSearchConstants . IGNORE_RETURN_TYPE ) ; if ( maskedLimitTo == IJavaSearchConstants . DECLARATIONS || maskedLimitTo == IJavaSearchConstants . ALL_OCCURRENCES ) { ignoreDeclaringType = ( limitTo & IJavaSearchConstants . IGNORE_DECLARING_TYPE ) != 0 ; ignoreReturnType = ( limitTo & IJavaSearchConstants . IGNORE_RETURN_TYPE ) != 0 ; } if ( ( matchRule = validateMatchRule ( null , matchRule ) ) == - 1 ) { return null ; } char [ ] declaringSimpleName = null ; char [ ] declaringQualification = null ; switch ( element . getElementType ( ) ) { case IJavaElement . FIELD : IField field = ( IField ) element ; if ( ! ignoreDeclaringType ) { IType declaringClass = field . getDeclaringType ( ) ; declaringSimpleName = declaringClass . getElementName ( ) . toCharArray ( ) ; declaringQualification = declaringClass . getPackageFragment ( ) . getElementName ( ) . toCharArray ( ) ; char [ ] [ ] enclosingNames = enclosingTypeNames ( declaringClass ) ; if ( enclosingNames . length > 0 ) { declaringQualification = CharOperation . concat ( declaringQualification , CharOperation . concatWith ( enclosingNames , '.' ) , '.' ) ; } } char [ ] name = field . getElementName ( ) . toCharArray ( ) ; char [ ] typeSimpleName = null ; char [ ] typeQualification = null ; String typeSignature = null ; if ( ! ignoreReturnType ) { try { typeSignature = field . getTypeSignature ( ) ; char [ ] signature = typeSignature . toCharArray ( ) ; char [ ] typeErasure = Signature . toCharArray ( Signature . getTypeErasure ( signature ) ) ; CharOperation . replace ( typeErasure , '$' , '.' ) ; if ( ( lastDot = CharOperation . lastIndexOf ( '.' , typeErasure ) ) == - 1 ) { typeSimpleName = typeErasure ; } else { typeSimpleName = CharOperation . subarray ( typeErasure , lastDot + 1 , typeErasure . length ) ; typeQualification = CharOperation . subarray ( typeErasure , 0 , lastDot ) ; if ( ! field . isBinary ( ) ) { typeQualification = CharOperation . concat ( IIndexConstants . ONE_STAR , typeQualification ) ; } } } catch ( JavaModelException e ) { return null ; } } searchPattern = new FieldPattern ( name , declaringQualification , declaringSimpleName , typeQualification , typeSimpleName , typeSignature , limitTo , matchRule ) ; break ; case IJavaElement . IMPORT_DECLARATION : String elementName = element . getElementName ( ) ; lastDot = elementName . lastIndexOf ( '.' ) ; if ( lastDot == - 1 ) return null ; IImportDeclaration importDecl = ( IImportDeclaration ) element ; if ( importDecl . isOnDemand ( ) ) { searchPattern = createPackagePattern ( elementName . substring ( 0 , lastDot ) , maskedLimitTo , matchRule ) ; } else { searchPattern = createTypePattern ( elementName . substring ( lastDot + 1 ) . toCharArray ( ) , elementName . substring ( 0 , lastDot ) . toCharArray ( ) , null , null , null , maskedLimitTo , matchRule ) ; } break ; case IJavaElement . LOCAL_VARIABLE : LocalVariable localVar = ( LocalVariable ) element ; searchPattern = new LocalVariablePattern ( localVar , limitTo , matchRule ) ; break ; case IJavaElement . TYPE_PARAMETER : ITypeParameter typeParam = ( ITypeParameter ) element ; boolean findParamDeclarations = true ; boolean findParamReferences = true ; switch ( maskedLimitTo ) { case IJavaSearchConstants . DECLARATIONS : findParamReferences = false ; break ; case IJavaSearchConstants . REFERENCES : findParamDeclarations = false ; break ; } searchPattern = new TypeParameterPattern ( findParamDeclarations , findParamReferences , typeParam , matchRule ) ; break ; case IJavaElement . METHOD : IMethod method = ( IMethod ) element ; boolean isConstructor ; try { isConstructor = method . isConstructor ( ) ; } catch ( JavaModelException e ) { return null ; } IType declaringClass = method . getDeclaringType ( ) ; if ( ignoreDeclaringType ) { if ( isConstructor ) declaringSimpleName = declaringClass . getElementName ( ) . toCharArray ( ) ; } else { declaringSimpleName = declaringClass . getElementName ( ) . toCharArray ( ) ; declaringQualification = declaringClass . getPackageFragment ( ) . getElementName ( ) . toCharArray ( ) ; char [ ] [ ] enclosingNames = enclosingTypeNames ( declaringClass ) ; if ( enclosingNames . length > 0 ) { declaringQualification = CharOperation . concat ( declaringQualification , CharOperation . concatWith ( enclosingNames , '.' ) , '.' ) ; } } char [ ] selector = method . getElementName ( ) . toCharArray ( ) ; char [ ] returnSimpleName = null ; char [ ] returnQualification = null ; String returnSignature = null ; if ( ! ignoreReturnType ) { try { returnSignature = method . getReturnType ( ) ; char [ ] signature = returnSignature . toCharArray ( ) ; char [ ] returnErasure = Signature . toCharArray ( Signature . getTypeErasure ( signature ) ) ; CharOperation . replace ( returnErasure , '$' , '.' ) ; if ( ( lastDot = CharOperation . lastIndexOf ( '.' , returnErasure ) ) == - 1 ) { returnSimpleName = returnErasure ; } else { returnSimpleName = CharOperation . subarray ( returnErasure , lastDot + 1 , returnErasure . length ) ; returnQualification = CharOperation . subarray ( returnErasure , 0 , lastDot ) ; if ( ! method . isBinary ( ) ) { CharOperation . concat ( IIndexConstants . ONE_STAR , returnQualification ) ; } } } catch ( JavaModelException e ) { return null ; } } String [ ] parameterTypes = method . getParameterTypes ( ) ; int paramCount = parameterTypes . length ; char [ ] [ ] parameterSimpleNames = new char [ paramCount ] [ ] ; char [ ] [ ] parameterQualifications = new char [ paramCount ] [ ] ; String [ ] parameterSignatures = new String [ paramCount ] ; for ( int i = 0 ; i < paramCount ; i ++ ) { parameterSignatures [ i ] = parameterTypes [ i ] ; char [ ] signature = parameterSignatures [ i ] . toCharArray ( ) ; char [ ] paramErasure = Signature . toCharArray ( Signature . getTypeErasure ( signature ) ) ; CharOperation . replace ( paramErasure , '$' , '.' ) ; if ( ( lastDot = CharOperation . lastIndexOf ( '.' , paramErasure ) ) == - 1 ) { parameterSimpleNames [ i ] = paramErasure ; parameterQualifications [ i ] = null ; } else { parameterSimpleNames [ i ] = CharOperation . subarray ( paramErasure , lastDot + 1 , paramErasure . length ) ; parameterQualifications [ i ] = CharOperation . subarray ( paramErasure , 0 , lastDot ) ; if ( ! method . isBinary ( ) ) { CharOperation . concat ( IIndexConstants . ONE_STAR , parameterQualifications [ i ] ) ; } } } if ( isConstructor ) { searchPattern = new ConstructorPattern ( declaringSimpleName , declaringQualification , parameterQualifications , parameterSimpleNames , parameterSignatures , method , limitTo , matchRule ) ; } else { searchPattern = new MethodPattern ( selector , declaringQualification , declaringSimpleName , returnQualification , returnSimpleName , returnSignature , parameterQualifications , parameterSimpleNames , parameterSignatures , method , limitTo , matchRule ) ; } break ; case IJavaElement . TYPE : IType type = ( IType ) element ; searchPattern = createTypePattern ( type . getElementName ( ) . toCharArray ( ) , type . getPackageFragment ( ) . getElementName ( ) . toCharArray ( ) , ignoreDeclaringType ? null : enclosingTypeNames ( type ) , null , type , maskedLimitTo , matchRule ) ; break ; case IJavaElement . PACKAGE_DECLARATION : case IJavaElement . PACKAGE_FRAGMENT : searchPattern = createPackagePattern ( element . getElementName ( ) , maskedLimitTo , matchRule ) ; break ; } if ( searchPattern != null ) MatchLocator . setFocus ( searchPattern , element ) ; return searchPattern ; } private static SearchPattern createTypePattern ( char [ ] simpleName , char [ ] packageName , char [ ] [ ] enclosingTypeNames , String typeSignature , IType type , int limitTo , int matchRule ) { switch ( limitTo ) { case IJavaSearchConstants . DECLARATIONS : return new TypeDeclarationPattern ( packageName , enclosingTypeNames , simpleName , IIndexConstants . TYPE_SUFFIX , matchRule ) ; case IJavaSearchConstants . REFERENCES : if ( type != null ) { return new TypeReferencePattern ( CharOperation . concatWith ( packageName , enclosingTypeNames , '.' ) , simpleName , type , matchRule ) ; } return new TypeReferencePattern ( CharOperation . concatWith ( packageName , enclosingTypeNames , '.' ) , simpleName , typeSignature , matchRule ) ; case IJavaSearchConstants . IMPLEMENTORS : return new SuperTypeReferencePattern ( CharOperation . concatWith ( packageName , enclosingTypeNames , '.' ) , simpleName , SuperTypeReferencePattern . ONLY_SUPER_INTERFACES , matchRule ) ; case IJavaSearchConstants . ALL_OCCURRENCES : return new OrPattern ( new TypeDeclarationPattern ( packageName , enclosingTypeNames , simpleName , IIndexConstants . TYPE_SUFFIX , matchRule ) , ( type != null ) ? new TypeReferencePattern ( CharOperation . concatWith ( packageName , enclosingTypeNames , '.' ) , simpleName , type , matchRule ) : new TypeReferencePattern ( CharOperation . concatWith ( packageName , enclosingTypeNames , '.' ) , simpleName , typeSignature , matchRule ) ) ; default : if ( type != null ) { return new TypeReferencePattern ( CharOperation . concatWith ( packageName , enclosingTypeNames , '.' ) , simpleName , type , limitTo , matchRule ) ; } } return null ; } private static SearchPattern createTypePattern ( String patternString , int limitTo , int matchRule , char indexSuffix ) { Scanner scanner = new Scanner ( false , true , false , ClassFileConstants . JDK1_3 , null , null , true ) ; scanner . setSource ( patternString . toCharArray ( ) ) ; String type = null ; int token ; try { token = scanner . getNextToken ( ) ; } catch ( InvalidInputException e ) { return null ; } int argCount = 0 ; while ( token != TerminalTokens . TokenNameEOF ) { if ( argCount == 0 ) { switch ( token ) { case TerminalTokens . TokenNameWHITESPACE : break ; case TerminalTokens . TokenNameLESS : argCount ++ ; default : if ( type == null ) type = scanner . getCurrentTokenString ( ) ; else type += scanner . getCurrentTokenString ( ) ; } } else { switch ( token ) { case TerminalTokens . TokenNameGREATER : case TerminalTokens . TokenNameRIGHT_SHIFT : case TerminalTokens . TokenNameUNSIGNED_RIGHT_SHIFT : argCount -- ; break ; case TerminalTokens . TokenNameLESS : argCount ++ ; break ; } if ( type == null ) return null ; type += scanner . getCurrentTokenString ( ) ; } try { token = scanner . getNextToken ( ) ; } catch ( InvalidInputException e ) { return null ; } } if ( type == null ) return null ; String typeSignature = null ; char [ ] qualificationChars = null , typeChars = null ; char [ ] typePart = null ; try { typeSignature = Signature . createTypeSignature ( type , false ) ; if ( typeSignature . indexOf ( Signature . C_GENERIC_START ) < 0 ) { typePart = type . toCharArray ( ) ; } else { typePart = Signature . toCharArray ( Signature . getTypeErasure ( typeSignature . toCharArray ( ) ) ) ; } } catch ( IllegalArgumentException iae ) { return null ; } int lastDotPosition = CharOperation . lastIndexOf ( '.' , typePart ) ; if ( lastDotPosition >= 0 ) { qualificationChars = CharOperation . subarray ( typePart , 0 , lastDotPosition ) ; if ( qualificationChars . length == 1 && qualificationChars [ 0 ] == '*' ) qualificationChars = null ; typeChars = CharOperation . subarray ( typePart , lastDotPosition + 1 , typePart . length ) ; } else { typeChars = typePart ; } if ( typeChars . length == 1 && typeChars [ 0 ] == '*' ) { typeChars = null ; } switch ( limitTo ) { case IJavaSearchConstants . DECLARATIONS : return new QualifiedTypeDeclarationPattern ( 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 ) , new TypeReferencePattern ( qualificationChars , typeChars , typeSignature , indexSuffix , matchRule ) ) ; default : return new TypeReferencePattern ( qualificationChars , typeChars , typeSignature , limitTo , indexSuffix , matchRule ) ; } } private static char [ ] [ ] enclosingTypeNames ( IType type ) { IJavaElement parent = type . getParent ( ) ; switch ( parent . getElementType ( ) ) { case IJavaElement . CLASS_FILE : IType declaringType = type . getDeclaringType ( ) ; if ( declaringType == null ) return CharOperation . NO_CHAR_CHAR ; return CharOperation . arrayConcat ( enclosingTypeNames ( declaringType ) , declaringType . getElementName ( ) . toCharArray ( ) ) ; case IJavaElement . COMPILATION_UNIT : return CharOperation . NO_CHAR_CHAR ; case IJavaElement . FIELD : case IJavaElement . INITIALIZER : case IJavaElement . METHOD : IType declaringClass = ( ( IMember ) parent ) . getDeclaringType ( ) ; return CharOperation . arrayConcat ( enclosingTypeNames ( declaringClass ) , new char [ ] [ ] { declaringClass . getElementName ( ) . toCharArray ( ) , IIndexConstants . ONE_STAR } ) ; case IJavaElement . TYPE : return CharOperation . arrayConcat ( enclosingTypeNames ( ( IType ) parent ) , parent . getElementName ( ) . toCharArray ( ) ) ; default : return null ; } } public void decodeIndexKey ( char [ ] key ) { } public void findIndexMatches ( Index index , IndexQueryRequestor requestor , SearchParticipant participant , IJavaSearchScope scope , IProgressMonitor monitor ) throws IOException { if ( monitor != null && monitor . isCanceled ( ) ) throw new OperationCanceledException ( ) ; try { index . startQuery ( ) ; SearchPattern pattern = currentPattern ( ) ; EntryResult [ ] entries = pattern . queryIn ( index ) ; if ( entries == null ) return ; SearchPattern decodedResult = pattern . getBlankPattern ( ) ; String containerPath = index . containerPath ; char separator = index . separator ; for ( int i = 0 , l = entries . length ; i < l ; i ++ ) { if ( monitor != null && monitor . isCanceled ( ) ) throw new OperationCanceledException ( ) ; EntryResult entry = entries [ i ] ; decodedResult . decodeIndexKey ( entry . getWord ( ) ) ; if ( pattern . matchesDecodedKey ( decodedResult ) ) { String [ ] names = entry . getDocumentNames ( index ) ; for ( int j = 0 , n = names . length ; j < n ; j ++ ) acceptMatch ( names [ j ] , containerPath , separator , decodedResult , requestor , participant , scope , monitor ) ; } } } finally { index . stopQuery ( ) ; } } public abstract SearchPattern getBlankPattern ( ) ; public char [ ] getIndexKey ( ) { return null ; } public char [ ] [ ] getIndexCategories ( ) { return CharOperation . NO_CHAR_CHAR ; } public final int getMatchRule ( ) { return this . matchRule ; } public boolean isPolymorphicSearch ( ) { return false ; } public boolean matchesDecodedKey ( SearchPattern decodedPattern ) { return true ; } public boolean matchesName ( char [ ] pattern , char [ ] name ) { if ( pattern == null ) return true ; if ( name != null ) { boolean isCaseSensitive = ( this . matchRule & R_CASE_SENSITIVE ) != 0 ; int matchMode = this . matchRule & MODE_MASK ; boolean emptyPattern = pattern . length == 0 ; if ( emptyPattern && ( this . matchRule & R_PREFIX_MATCH ) != 0 ) return true ; boolean sameLength = pattern . length == name . length ; boolean canBePrefix = name . length >= pattern . length ; boolean matchFirstChar = ! isCaseSensitive || emptyPattern || ( name . length > 0 && pattern [ 0 ] == name [ 0 ] ) ; switch ( matchMode ) { case R_EXACT_MATCH : if ( sameLength && matchFirstChar ) { return CharOperation . equals ( pattern , name , isCaseSensitive ) ; } break ; case R_PREFIX_MATCH : if ( canBePrefix && matchFirstChar ) { return CharOperation . prefixEquals ( pattern , name , isCaseSensitive ) ; } break ; case R_PATTERN_MATCH : if ( ! isCaseSensitive ) pattern = CharOperation . toLowerCase ( pattern ) ; return CharOperation . match ( pattern , name , isCaseSensitive ) ; case SearchPattern . R_CAMELCASE_MATCH : if ( matchFirstChar && CharOperation . camelCaseMatch ( pattern , name , false ) ) { return true ; } if ( ! isCaseSensitive && matchFirstChar && CharOperation . prefixEquals ( pattern , name , false ) ) { return true ; } break ; case SearchPattern . R_CAMELCASE_SAME_PART_COUNT_MATCH : return matchFirstChar && CharOperation . camelCaseMatch ( pattern , name , true ) ; case R_REGEXP_MATCH : return true ; } } return false ; } public static int validateMatchRule ( String stringPattern , int matchRule ) { if ( ( matchRule & R_REGEXP_MATCH ) != 0 ) { if ( ( matchRule & R_PATTERN_MATCH ) != 0 || ( matchRule & R_PREFIX_MATCH ) != 0 || ( matchRule & R_CAMELCASE_MATCH ) != 0 || ( matchRule & R_CAMELCASE_SAME_PART_COUNT_MATCH ) != 0 ) { return - 1 ; } } if ( stringPattern != null ) { int starIndex = stringPattern . indexOf ( '*' ) ; int questionIndex = stringPattern . indexOf ( '?' ) ; if ( starIndex < 0 && questionIndex < 0 ) { matchRule &= ~ R_PATTERN_MATCH ; } else { matchRule |= R_PATTERN_MATCH ; } } if ( ( matchRule & R_PATTERN_MATCH ) != 0 ) { matchRule &= ~ R_CAMELCASE_MATCH ; matchRule &= ~ R_CAMELCASE_SAME_PART_COUNT_MATCH ; matchRule &= ~ R_PREFIX_MATCH ; return matchRule ; } if ( ( matchRule & R_CAMELCASE_MATCH ) != 0 ) { matchRule &= ~ R_CAMELCASE_SAME_PART_COUNT_MATCH ; matchRule &= ~ R_PREFIX_MATCH ; boolean validCamelCase = validateCamelCasePattern ( stringPattern ) ; if ( ! validCamelCase ) { matchRule &= ~ R_CAMELCASE_MATCH ; matchRule |= R_PREFIX_MATCH ; } return matchRule ; } if ( ( matchRule & R_CAMELCASE_SAME_PART_COUNT_MATCH ) != 0 ) { matchRule &= ~ R_PREFIX_MATCH ; boolean validCamelCase = validateCamelCasePattern ( stringPattern ) ; if ( ! validCamelCase ) { matchRule &= ~ R_CAMELCASE_SAME_PART_COUNT_MATCH ; } return matchRule ; } return matchRule ; } private static boolean validateCamelCasePattern ( String stringPattern ) { if ( stringPattern == null ) return true ; int length = stringPattern . length ( ) ; boolean validCamelCase = true ; boolean lowerCamelCase = false ; int uppercase = 0 ; for ( int i = 0 ; i < length && validCamelCase ; i ++ ) { char ch = stringPattern . charAt ( i ) ; validCamelCase = i == 0 ? ScannerHelper . isJavaIdentifierStart ( ch ) : ScannerHelper . isJavaIdentifierPart ( ch ) ; if ( ScannerHelper . isUpperCase ( ch ) ) uppercase ++ ; if ( i == 0 ) lowerCamelCase = uppercase == 0 ; } if ( validCamelCase ) { validCamelCase = lowerCamelCase ? uppercase > 0 : uppercase > 1 ; } return validCamelCase ; } public EntryResult [ ] queryIn ( Index index ) throws IOException { return index . query ( getIndexCategories ( ) , getIndexKey ( ) , getMatchRule ( ) ) ; } public String toString ( ) { return "" ; } } </s>