<s> package org . eclipse . jdt . internal . core ; import org . eclipse . core . runtime . IProgressMonitor ; import org . eclipse . core . runtime . OperationCanceledException ; import org . eclipse . jdt . core . * ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . core . search . * ; import org . eclipse . jdt . internal . codeassist . ISearchRequestor ; import org . eclipse . jdt . internal . compiler . env . AccessRestriction ; import org . eclipse . jdt . internal . compiler . env . IBinaryType ; import org . eclipse . jdt . internal . compiler . env . ICompilationUnit ; import org . eclipse . jdt . internal . compiler . env . INameEnvironment ; import org . eclipse . jdt . internal . compiler . env . ISourceType ; import org . eclipse . jdt . internal . compiler . env . NameEnvironmentAnswer ; import org . eclipse . jdt . internal . core . search . BasicSearchEngine ; import org . eclipse . jdt . internal . core . search . IRestrictedAccessConstructorRequestor ; import org . eclipse . jdt . internal . core . search . IRestrictedAccessTypeRequestor ; import org . eclipse . jdt . internal . core . search . indexing . IndexManager ; import org . eclipse . jdt . internal . core . util . Util ; public class SearchableEnvironment implements INameEnvironment , IJavaSearchConstants { public NameLookup nameLookup ; protected ICompilationUnit unitToSkip ; protected org . eclipse . jdt . core . ICompilationUnit [ ] workingCopies ; protected WorkingCopyOwner owner ; protected JavaProject project ; protected IJavaSearchScope searchScope ; protected boolean checkAccessRestrictions ; public SearchableEnvironment ( JavaProject project , org . eclipse . jdt . core . ICompilationUnit [ ] workingCopies ) throws JavaModelException { this . project = project ; this . checkAccessRestrictions = ! JavaCore . IGNORE . equals ( project . getOption ( JavaCore . COMPILER_PB_FORBIDDEN_REFERENCE , true ) ) || ! JavaCore . IGNORE . equals ( project . getOption ( JavaCore . COMPILER_PB_DISCOURAGED_REFERENCE , true ) ) ; this . workingCopies = workingCopies ; this . nameLookup = project . newNameLookup ( workingCopies ) ; } public SearchableEnvironment ( JavaProject project , WorkingCopyOwner owner ) throws JavaModelException { this ( project , owner == null ? null : JavaModelManager . getJavaModelManager ( ) . getWorkingCopies ( owner , true ) ) ; this . owner = owner ; } private static int convertSearchFilterToModelFilter ( int searchFilter ) { switch ( searchFilter ) { case IJavaSearchConstants . CLASS : return NameLookup . ACCEPT_CLASSES ; case IJavaSearchConstants . INTERFACE : return NameLookup . ACCEPT_INTERFACES ; case IJavaSearchConstants . ENUM : return NameLookup . ACCEPT_ENUMS ; case IJavaSearchConstants . ANNOTATION_TYPE : return NameLookup . ACCEPT_ANNOTATIONS ; case IJavaSearchConstants . CLASS_AND_ENUM : return NameLookup . ACCEPT_CLASSES | NameLookup . ACCEPT_ENUMS ; case IJavaSearchConstants . CLASS_AND_INTERFACE : return NameLookup . ACCEPT_CLASSES | NameLookup . ACCEPT_INTERFACES ; default : return NameLookup . ACCEPT_ALL ; } } protected NameEnvironmentAnswer find ( String typeName , String packageName ) { if ( packageName == null ) packageName = IPackageFragment . DEFAULT_PACKAGE_NAME ; if ( this . owner != null ) { String source = this . owner . findSource ( typeName , packageName ) ; if ( source != null ) { ICompilationUnit cu = new BasicCompilationUnit ( source . toCharArray ( ) , CharOperation . splitOn ( '.' , packageName . toCharArray ( ) ) , typeName + Util . defaultJavaExtension ( ) ) ; return new NameEnvironmentAnswer ( cu , null ) ; } } NameLookup . Answer answer = this . nameLookup . findType ( typeName , packageName , false , NameLookup . ACCEPT_ALL , this . checkAccessRestrictions ) ; if ( answer != null ) { if ( answer . type instanceof BinaryType ) { try { return new NameEnvironmentAnswer ( ( IBinaryType ) ( ( BinaryType ) answer . type ) . getElementInfo ( ) , answer . restriction ) ; } catch ( JavaModelException npe ) { } } else { try { SourceTypeElementInfo sourceType = ( SourceTypeElementInfo ) ( ( SourceType ) answer . type ) . getElementInfo ( ) ; ISourceType topLevelType = sourceType ; while ( topLevelType . getEnclosingType ( ) != null ) { topLevelType = topLevelType . getEnclosingType ( ) ; } IType [ ] types = sourceType . getHandle ( ) . getCompilationUnit ( ) . getTypes ( ) ; ISourceType [ ] sourceTypes = new ISourceType [ types . length ] ; sourceTypes [ 0 ] = sourceType ; int length = types . length ; for ( int i = 0 , index = 1 ; i < length ; i ++ ) { ISourceType otherType = ( ISourceType ) ( ( JavaElement ) types [ i ] ) . getElementInfo ( ) ; if ( ! otherType . equals ( topLevelType ) && index < length ) sourceTypes [ index ++ ] = otherType ; } return new NameEnvironmentAnswer ( sourceTypes , answer . restriction ) ; } catch ( JavaModelException npe ) { } } } return null ; } public void findPackages ( char [ ] prefix , ISearchRequestor requestor ) { this . nameLookup . seekPackageFragments ( new String ( prefix ) , true , new SearchableEnvironmentRequestor ( requestor ) ) ; } public void findExactTypes ( char [ ] name , final boolean findMembers , int searchFor , final ISearchRequestor storage ) { try { final String excludePath ; if ( this . unitToSkip != null ) { if ( ! ( this . unitToSkip instanceof IJavaElement ) ) { findExactTypes ( new String ( name ) , storage , convertSearchFilterToModelFilter ( searchFor ) ) ; return ; } excludePath = ( ( IJavaElement ) this . unitToSkip ) . getPath ( ) . toString ( ) ; } else { excludePath = null ; } IProgressMonitor progressMonitor = new IProgressMonitor ( ) { boolean isCanceled = false ; public void beginTask ( String n , int totalWork ) { } public void done ( ) { } public void internalWorked ( double work ) { } public boolean isCanceled ( ) { return this . isCanceled ; } public void setCanceled ( boolean value ) { this . isCanceled = value ; } public void setTaskName ( String n ) { } public void subTask ( String n ) { } public void worked ( int work ) { } } ; IRestrictedAccessTypeRequestor typeRequestor = new IRestrictedAccessTypeRequestor ( ) { public void acceptType ( int modifiers , char [ ] packageName , char [ ] simpleTypeName , char [ ] [ ] enclosingTypeNames , String path , AccessRestriction access ) { if ( excludePath != null && excludePath . equals ( path ) ) return ; if ( ! findMembers && enclosingTypeNames != null && enclosingTypeNames . length > 0 ) return ; storage . acceptType ( packageName , simpleTypeName , enclosingTypeNames , modifiers , access ) ; } } ; try { new BasicSearchEngine ( this . workingCopies ) . searchAllTypeNames ( null , SearchPattern . R_EXACT_MATCH , name , SearchPattern . R_EXACT_MATCH , searchFor , getSearchScope ( ) , typeRequestor , CANCEL_IF_NOT_READY_TO_SEARCH , progressMonitor ) ; } catch ( OperationCanceledException e ) { findExactTypes ( new String ( name ) , storage , convertSearchFilterToModelFilter ( searchFor ) ) ; } } catch ( JavaModelException e ) { findExactTypes ( new String ( name ) , storage , convertSearchFilterToModelFilter ( searchFor ) ) ; } } private void findExactTypes ( String name , ISearchRequestor storage , int type ) { SearchableEnvironmentRequestor requestor = new SearchableEnvironmentRequestor ( storage , this . unitToSkip , this . project , this . nameLookup ) ; this . nameLookup . seekTypes ( name , null , false , type , requestor ) ; } public NameEnvironmentAnswer findType ( char [ ] [ ] compoundTypeName ) { if ( compoundTypeName == null ) return null ; int length = compoundTypeName . length ; if ( length <= 1 ) { if ( length == 0 ) return null ; return find ( new String ( compoundTypeName [ 0 ] ) , null ) ; } int lengthM1 = length - 1 ; char [ ] [ ] packageName = new char [ lengthM1 ] [ ] ; System . arraycopy ( compoundTypeName , 0 , packageName , 0 , lengthM1 ) ; return find ( new String ( compoundTypeName [ lengthM1 ] ) , CharOperation . toString ( packageName ) ) ; } public NameEnvironmentAnswer findType ( char [ ] name , char [ ] [ ] packageName ) { if ( name == null ) return null ; return find ( new String ( name ) , packageName == null || packageName . length == 0 ? null : CharOperation . toString ( packageName ) ) ; } public void findTypes ( char [ ] prefix , final boolean findMembers , boolean camelCaseMatch , int searchFor , final ISearchRequestor storage ) { findTypes ( prefix , findMembers , camelCaseMatch , searchFor , storage , null ) ; } public void findTypes ( char [ ] prefix , final boolean findMembers , boolean camelCaseMatch , int searchFor , final ISearchRequestor storage , IProgressMonitor monitor ) { try { final String excludePath ; if ( this . unitToSkip != null ) { if ( ! ( this . unitToSkip instanceof IJavaElement ) ) { findTypes ( new String ( prefix ) , storage , convertSearchFilterToModelFilter ( searchFor ) ) ; return ; } excludePath = ( ( IJavaElement ) this . unitToSkip ) . getPath ( ) . toString ( ) ; } else { excludePath = null ; } int lastDotIndex = CharOperation . lastIndexOf ( '.' , prefix ) ; char [ ] qualification , simpleName ; if ( lastDotIndex < 0 ) { qualification = null ; if ( camelCaseMatch ) { simpleName = prefix ; } else { simpleName = CharOperation . toLowerCase ( prefix ) ; } } else { qualification = CharOperation . subarray ( prefix , 0 , lastDotIndex ) ; if ( camelCaseMatch ) { simpleName = CharOperation . subarray ( prefix , lastDotIndex + 1 , prefix . length ) ; } else { simpleName = CharOperation . toLowerCase ( CharOperation . subarray ( prefix , lastDotIndex + 1 , prefix . length ) ) ; } } IProgressMonitor progressMonitor = new IProgressMonitor ( ) { boolean isCanceled = false ; public void beginTask ( String name , int totalWork ) { } public void done ( ) { } public void internalWorked ( double work ) { } public boolean isCanceled ( ) { return this . isCanceled ; } public void setCanceled ( boolean value ) { this . isCanceled = value ; } public void setTaskName ( String name ) { } public void subTask ( String name ) { } public void worked ( int work ) { } } ; IRestrictedAccessTypeRequestor typeRequestor = new IRestrictedAccessTypeRequestor ( ) { public void acceptType ( int modifiers , char [ ] packageName , char [ ] simpleTypeName , char [ ] [ ] enclosingTypeNames , String path , AccessRestriction access ) { if ( excludePath != null && excludePath . equals ( path ) ) return ; if ( ! findMembers && enclosingTypeNames != null && enclosingTypeNames . length > 0 ) return ; storage . acceptType ( packageName , simpleTypeName , enclosingTypeNames , modifiers , access ) ; } } ; int matchRule = SearchPattern . R_PREFIX_MATCH ; if ( camelCaseMatch ) matchRule |= SearchPattern . R_CAMELCASE_MATCH ; if ( monitor != null ) { IndexManager indexManager = JavaModelManager . getIndexManager ( ) ; if ( indexManager . awaitingJobsCount ( ) == 0 ) { new BasicSearchEngine ( this . workingCopies ) . searchAllTypeNames ( qualification , SearchPattern . R_EXACT_MATCH , simpleName , matchRule , searchFor , getSearchScope ( ) , typeRequestor , FORCE_IMMEDIATE_SEARCH , progressMonitor ) ; } else { try { Thread . sleep ( 100 ) ; } catch ( InterruptedException e ) { } if ( monitor . isCanceled ( ) ) { throw new OperationCanceledException ( ) ; } if ( indexManager . awaitingJobsCount ( ) == 0 ) { new BasicSearchEngine ( this . workingCopies ) . searchAllTypeNames ( qualification , SearchPattern . R_EXACT_MATCH , simpleName , matchRule , searchFor , getSearchScope ( ) , typeRequestor , FORCE_IMMEDIATE_SEARCH , progressMonitor ) ; } else { findTypes ( new String ( prefix ) , storage , convertSearchFilterToModelFilter ( searchFor ) ) ; } } } else { try { new BasicSearchEngine ( this . workingCopies ) . searchAllTypeNames ( qualification , SearchPattern . R_EXACT_MATCH , simpleName , matchRule , searchFor , getSearchScope ( ) , typeRequestor , CANCEL_IF_NOT_READY_TO_SEARCH , progressMonitor ) ; } catch ( OperationCanceledException e ) { findTypes ( new String ( prefix ) , storage , convertSearchFilterToModelFilter ( searchFor ) ) ; } } } catch ( JavaModelException e ) { findTypes ( new String ( prefix ) , storage , convertSearchFilterToModelFilter ( searchFor ) ) ; } } public void findConstructorDeclarations ( char [ ] prefix , boolean camelCaseMatch , final ISearchRequestor storage , IProgressMonitor monitor ) { try { final String excludePath ; if ( this . unitToSkip != null && this . unitToSkip instanceof IJavaElement ) { excludePath = ( ( IJavaElement ) this . unitToSkip ) . getPath ( ) . toString ( ) ; } else { excludePath = null ; } int lastDotIndex = CharOperation . lastIndexOf ( '.' , prefix ) ; char [ ] qualification , simpleName ; if ( lastDotIndex < 0 ) { qualification = null ; if ( camelCaseMatch ) { simpleName = prefix ; } else { simpleName = CharOperation . toLowerCase ( prefix ) ; } } else { qualification = CharOperation . subarray ( prefix , 0 , lastDotIndex ) ; if ( camelCaseMatch ) { simpleName = CharOperation . subarray ( prefix , lastDotIndex + 1 , prefix . length ) ; } else { simpleName = CharOperation . toLowerCase ( CharOperation . subarray ( prefix , lastDotIndex + 1 , prefix . length ) ) ; } } IProgressMonitor progressMonitor = new IProgressMonitor ( ) { boolean isCanceled = false ; public void beginTask ( String name , int totalWork ) { } public void done ( ) { } public void internalWorked ( double work ) { } public boolean isCanceled ( ) { return this . isCanceled ; } public void setCanceled ( boolean value ) { this . isCanceled = value ; } public void setTaskName ( String name ) { } public void subTask ( String name ) { } public void worked ( int work ) { } } ; IRestrictedAccessConstructorRequestor constructorRequestor = new IRestrictedAccessConstructorRequestor ( ) { public void acceptConstructor ( int modifiers , char [ ] simpleTypeName , int parameterCount , char [ ] signature , char [ ] [ ] parameterTypes , char [ ] [ ] parameterNames , int typeModifiers , char [ ] packageName , int extraFlags , String path , AccessRestriction access ) { if ( excludePath != null && excludePath . equals ( path ) ) return ; storage . acceptConstructor ( modifiers , simpleTypeName , parameterCount , signature , parameterTypes , parameterNames , typeModifiers , packageName , extraFlags , path , access ) ; } } ; int matchRule = SearchPattern . R_PREFIX_MATCH ; if ( camelCaseMatch ) matchRule |= SearchPattern . R_CAMELCASE_MATCH ; if ( monitor != null ) { IndexManager indexManager = JavaModelManager . getIndexManager ( ) ; while ( indexManager . awaitingJobsCount ( ) > 0 ) { try { Thread . sleep ( 50 ) ; } catch ( InterruptedException e ) { } if ( monitor . isCanceled ( ) ) { throw new OperationCanceledException ( ) ; } } new BasicSearchEngine ( this . workingCopies ) . searchAllConstructorDeclarations ( qualification , simpleName , matchRule , getSearchScope ( ) , constructorRequestor , FORCE_IMMEDIATE_SEARCH , progressMonitor ) ; } else { try { new BasicSearchEngine ( this . workingCopies ) . searchAllConstructorDeclarations ( qualification , simpleName , matchRule , getSearchScope ( ) , constructorRequestor , CANCEL_IF_NOT_READY_TO_SEARCH , progressMonitor ) ; } catch ( OperationCanceledException e ) { } } } catch ( JavaModelException e ) { } } private void findTypes ( String prefix , ISearchRequestor storage , int type ) { SearchableEnvironmentRequestor requestor = new SearchableEnvironmentRequestor ( storage , this . unitToSkip , this . project , this . nameLookup ) ; int index = prefix . lastIndexOf ( '.' ) ; if ( index == - 1 ) { this . nameLookup . seekTypes ( prefix , null , true , type , requestor ) ; } else { String packageName = prefix . substring ( 0 , index ) ; JavaElementRequestor elementRequestor = new JavaElementRequestor ( ) ; this . nameLookup . seekPackageFragments ( packageName , false , elementRequestor ) ; IPackageFragment [ ] fragments = elementRequestor . getPackageFragments ( ) ; if ( fragments != null ) { String className = prefix . substring ( index + 1 ) ; for ( int i = 0 , length = fragments . length ; i < length ; i ++ ) if ( fragments [ i ] != null ) this . nameLookup . seekTypes ( className , fragments [ i ] , true , type , requestor ) ; } } } private IJavaSearchScope getSearchScope ( ) { if ( this . searchScope == null ) { if ( this . checkAccessRestrictions ) { this . searchScope = BasicSearchEngine . createJavaSearchScope ( new IJavaElement [ ] { this . project } ) ; } else { this . searchScope = BasicSearchEngine . createJavaSearchScope ( this . nameLookup . packageFragmentRoots ) ; } } return this . searchScope ; } public boolean isPackage ( char [ ] [ ] parentPackageName , char [ ] subPackageName ) { String [ ] pkgName ; if ( parentPackageName == null ) pkgName = new String [ ] { new String ( subPackageName ) } ; else { int length = parentPackageName . length ; pkgName = new String [ length + 1 ] ; for ( int i = 0 ; i < length ; i ++ ) pkgName [ i ] = new String ( parentPackageName [ i ] ) ; pkgName [ length ] = new String ( subPackageName ) ; } return ( this . owner != null && this . owner . isPackage ( pkgName ) ) || this . nameLookup . isPackage ( pkgName ) ; } protected String toStringChar ( char [ ] name ) { return "[" + new String ( name ) + "]" ; } protected String toStringCharChar ( char [ ] [ ] names ) { StringBuffer result = new StringBuffer ( ) ; for ( int i = 0 ; i < names . length ; i ++ ) { result . append ( toStringChar ( names [ i ] ) ) ; } return result . toString ( ) ; } public void cleanup ( ) { } } </s>