<s> package org . eclipse . jdt . internal . core . search ; import org . eclipse . core . resources . IFolder ; import org . eclipse . core . resources . ResourcesPlugin ; import org . eclipse . core . runtime . IPath ; import org . eclipse . jdt . core . * ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . core . search . IJavaSearchScope ; import org . eclipse . jdt . core . search . SearchPattern ; import org . eclipse . jdt . internal . compiler . util . ObjectVector ; import org . eclipse . jdt . internal . compiler . util . SimpleSet ; import org . eclipse . jdt . internal . core . JarPackageFragmentRoot ; import org . eclipse . jdt . internal . core . JavaModel ; import org . eclipse . jdt . internal . core . JavaModelManager ; import org . eclipse . jdt . internal . core . JavaProject ; import org . eclipse . jdt . internal . core . builder . ReferenceCollection ; import org . eclipse . jdt . internal . core . builder . State ; import org . eclipse . jdt . internal . core . search . indexing . IndexManager ; import org . eclipse . jdt . internal . core . search . matching . MatchLocator ; import org . eclipse . jdt . internal . core . search . matching . MethodPattern ; public class IndexSelector { IJavaSearchScope searchScope ; SearchPattern pattern ; IPath [ ] indexLocations ; public IndexSelector ( IJavaSearchScope searchScope , SearchPattern pattern ) { this . searchScope = searchScope ; this . pattern = pattern ; } public static boolean canSeeFocus ( SearchPattern pattern , IPath projectOrJarPath ) { try { IJavaModel model = JavaModelManager . getJavaModelManager ( ) . getJavaModel ( ) ; IJavaProject project = getJavaProject ( projectOrJarPath , model ) ; IJavaElement [ ] focuses = getFocusedElementsAndTypes ( pattern , project , null ) ; if ( focuses . length == 0 ) return false ; if ( project != null ) { return canSeeFocus ( focuses , ( JavaProject ) project , null ) ; } IJavaProject [ ] allProjects = model . getJavaProjects ( ) ; for ( int i = 0 , length = allProjects . length ; i < length ; i ++ ) { JavaProject otherProject = ( JavaProject ) allProjects [ i ] ; IClasspathEntry entry = otherProject . getClasspathEntryFor ( projectOrJarPath ) ; if ( entry != null && entry . getEntryKind ( ) == IClasspathEntry . CPE_LIBRARY ) { if ( canSeeFocus ( focuses , otherProject , null ) ) { return true ; } } } return false ; } catch ( JavaModelException e ) { return false ; } } private static boolean canSeeFocus ( IJavaElement [ ] focuses , JavaProject javaProject , char [ ] [ ] [ ] focusQualifiedNames ) { int length = focuses . length ; for ( int i = 0 ; i < length ; i ++ ) { if ( canSeeFocus ( focuses [ i ] , javaProject , focusQualifiedNames ) ) return true ; } return false ; } private static boolean canSeeFocus ( IJavaElement focus , JavaProject javaProject , char [ ] [ ] [ ] focusQualifiedNames ) { try { if ( focus == null ) return false ; if ( focus . equals ( javaProject ) ) return true ; if ( focus instanceof JarPackageFragmentRoot ) { IPath focusPath = focus . getPath ( ) ; IClasspathEntry [ ] entries = javaProject . getExpandedClasspath ( ) ; for ( int i = 0 , length = entries . length ; i < length ; i ++ ) { IClasspathEntry entry = entries [ i ] ; if ( entry . getEntryKind ( ) == IClasspathEntry . CPE_LIBRARY && entry . getPath ( ) . equals ( focusPath ) ) return true ; } return false ; } IPath focusPath = ( ( JavaProject ) focus ) . getProject ( ) . getFullPath ( ) ; IClasspathEntry [ ] entries = javaProject . getExpandedClasspath ( ) ; for ( int i = 0 , length = entries . length ; i < length ; i ++ ) { IClasspathEntry entry = entries [ i ] ; if ( entry . getEntryKind ( ) == IClasspathEntry . CPE_PROJECT && entry . getPath ( ) . equals ( focusPath ) ) { if ( focusQualifiedNames != null ) { State projectState = ( State ) JavaModelManager . getJavaModelManager ( ) . getLastBuiltState ( javaProject . getProject ( ) , null ) ; if ( projectState != null ) { Object [ ] values = projectState . getReferences ( ) . valueTable ; int vLength = values . length ; for ( int j = 0 ; j < vLength ; j ++ ) { if ( values [ j ] == null ) continue ; ReferenceCollection references = ( ReferenceCollection ) values [ j ] ; if ( references . includes ( focusQualifiedNames , null , null ) ) { return true ; } } return false ; } } return true ; } } return false ; } catch ( JavaModelException e ) { return false ; } } private static IJavaElement [ ] getFocusedElementsAndTypes ( SearchPattern pattern , IJavaElement focusElement , ObjectVector superTypes ) throws JavaModelException { if ( pattern instanceof MethodPattern ) { IType type = ( IType ) pattern . focus . getAncestor ( IJavaElement . TYPE ) ; MethodPattern methodPattern = ( MethodPattern ) pattern ; String selector = new String ( methodPattern . selector ) ; int parameterCount = methodPattern . parameterCount ; ITypeHierarchy superHierarchy = type . newSupertypeHierarchy ( null ) ; IType [ ] allTypes = superHierarchy . getAllSupertypes ( type ) ; int length = allTypes . length ; SimpleSet focusSet = new SimpleSet ( length + 1 ) ; if ( focusElement != null ) focusSet . add ( focusElement ) ; for ( int i = 0 ; i < length ; i ++ ) { IMethod [ ] methods = allTypes [ i ] . getMethods ( ) ; int mLength = methods . length ; for ( int m = 0 ; m < mLength ; m ++ ) { if ( parameterCount == methods [ m ] . getNumberOfParameters ( ) && methods [ m ] . getElementName ( ) . equals ( selector ) ) { IPackageFragmentRoot root = ( IPackageFragmentRoot ) allTypes [ i ] . getAncestor ( IJavaElement . PACKAGE_FRAGMENT_ROOT ) ; IJavaElement element = root . isArchive ( ) ? root : root . getParent ( ) ; focusSet . add ( element ) ; if ( superTypes != null ) superTypes . add ( allTypes [ i ] ) ; break ; } } } IJavaElement [ ] focuses = new IJavaElement [ focusSet . elementSize ] ; Object [ ] values = focusSet . values ; int count = 0 ; for ( int i = values . length ; -- i >= 0 ; ) { if ( values [ i ] != null ) { focuses [ count ++ ] = ( IJavaElement ) values [ i ] ; } } return focuses ; } if ( focusElement == null ) return new IJavaElement [ 0 ] ; return new IJavaElement [ ] { focusElement } ; } private void initializeIndexLocations ( ) { IPath [ ] projectsAndJars = this . searchScope . enclosingProjectsAndJars ( ) ; IndexManager manager = JavaModelManager . getIndexManager ( ) ; SimpleSet locations = new SimpleSet ( ) ; IJavaElement focus = MatchLocator . projectOrJarFocus ( this . pattern ) ; if ( focus == null ) { for ( int i = 0 ; i < projectsAndJars . length ; i ++ ) { IPath path = projectsAndJars [ i ] ; Object target = JavaModel . getTarget ( path , false ) ; if ( target instanceof IFolder ) path = ( ( IFolder ) target ) . getFullPath ( ) ; locations . add ( manager . computeIndexLocation ( path ) ) ; } } else { try { int length = projectsAndJars . length ; JavaProject [ ] projectsCanSeeFocus = new JavaProject [ length ] ; SimpleSet visitedProjects = new SimpleSet ( length ) ; int projectIndex = 0 ; SimpleSet externalLibsToCheck = new SimpleSet ( length ) ; ObjectVector superTypes = new ObjectVector ( ) ; IJavaElement [ ] focuses = getFocusedElementsAndTypes ( this . pattern , focus , superTypes ) ; char [ ] [ ] [ ] focusQualifiedNames = null ; boolean isAutoBuilding = ResourcesPlugin . getWorkspace ( ) . getDescription ( ) . isAutoBuilding ( ) ; if ( isAutoBuilding && focus instanceof IJavaProject ) { focusQualifiedNames = getQualifiedNames ( superTypes ) ; } IJavaModel model = JavaModelManager . getJavaModelManager ( ) . getJavaModel ( ) ; for ( int i = 0 ; i < length ; i ++ ) { IPath path = projectsAndJars [ i ] ; JavaProject project = ( JavaProject ) getJavaProject ( path , model ) ; if ( project != null ) { visitedProjects . add ( project ) ; if ( canSeeFocus ( focuses , project , focusQualifiedNames ) ) { locations . add ( manager . computeIndexLocation ( path ) ) ; projectsCanSeeFocus [ projectIndex ++ ] = project ; } } else { externalLibsToCheck . add ( path ) ; } } for ( int i = 0 ; i < projectIndex && externalLibsToCheck . elementSize > 0 ; i ++ ) { IClasspathEntry [ ] entries = projectsCanSeeFocus [ i ] . getResolvedClasspath ( ) ; for ( int j = entries . length ; -- j >= 0 ; ) { IClasspathEntry entry = entries [ j ] ; if ( entry . getEntryKind ( ) == IClasspathEntry . CPE_LIBRARY ) { IPath path = entry . getPath ( ) ; if ( externalLibsToCheck . remove ( path ) != null ) { Object target = JavaModel . getTarget ( path , false ) ; if ( target instanceof IFolder ) path = ( ( IFolder ) target ) . getFullPath ( ) ; locations . add ( manager . computeIndexLocation ( path ) ) ; } } } } if ( externalLibsToCheck . elementSize > 0 ) { IJavaProject [ ] allProjects = model . getJavaProjects ( ) ; for ( int i = 0 , l = allProjects . length ; i < l && externalLibsToCheck . elementSize > 0 ; i ++ ) { JavaProject project = ( JavaProject ) allProjects [ i ] ; if ( ! visitedProjects . includes ( project ) ) { IClasspathEntry [ ] entries = project . getResolvedClasspath ( ) ; for ( int j = entries . length ; -- j >= 0 ; ) { IClasspathEntry entry = entries [ j ] ; if ( entry . getEntryKind ( ) == IClasspathEntry . CPE_LIBRARY ) { IPath path = entry . getPath ( ) ; if ( externalLibsToCheck . remove ( path ) != null ) { Object target = JavaModel . getTarget ( path , false ) ; if ( target instanceof IFolder ) path = ( ( IFolder ) target ) . getFullPath ( ) ; locations . add ( manager . computeIndexLocation ( path ) ) ; } } } } } } } catch ( JavaModelException e ) { } } this . indexLocations = new IPath [ locations . elementSize ] ; Object [ ] values = locations . values ; int count = 0 ; for ( int i = values . length ; -- i >= 0 ; ) if ( values [ i ] != null ) this . indexLocations [ count ++ ] = ( IPath ) values [ i ] ; } public IPath [ ] getIndexLocations ( ) { if ( this . indexLocations == null ) { initializeIndexLocations ( ) ; } return this . indexLocations ; } private static IJavaProject getJavaProject ( IPath path , IJavaModel model ) { IJavaProject project = model . getJavaProject ( path . lastSegment ( ) ) ; if ( project . exists ( ) ) { return project ; } return null ; } private char [ ] [ ] [ ] getQualifiedNames ( ObjectVector types ) { final int size = types . size ; char [ ] [ ] [ ] focusQualifiedNames = null ; IJavaElement javaElement = this . pattern . focus ; while ( javaElement != null && ! ( javaElement instanceof ITypeRoot ) ) { javaElement = javaElement . getParent ( ) ; } if ( javaElement != null ) { IType primaryType = ( ( ITypeRoot ) javaElement ) . findPrimaryType ( ) ; if ( primaryType != null ) { focusQualifiedNames = new char [ size + 1 ] [ ] [ ] ; focusQualifiedNames [ 0 ] = CharOperation . splitOn ( '.' , primaryType . getFullyQualifiedName ( ) . toCharArray ( ) ) ; } } for ( int i = 0 ; i < size ; i ++ ) { focusQualifiedNames [ i + 1 ] = CharOperation . splitOn ( '.' , ( ( IType ) ( types . elementAt ( i ) ) ) . getFullyQualifiedName ( ) . toCharArray ( ) ) ; } return focusQualifiedNames == null ? null : ReferenceCollection . internQualifiedNames ( focusQualifiedNames , true ) ; } } </s>