<s> package org . eclipse . jdt . internal . core . search ; import java . io . File ; import java . util . HashMap ; import java . util . HashSet ; import java . util . Iterator ; import org . eclipse . core . resources . * ; import org . eclipse . core . runtime . IPath ; import org . eclipse . core . runtime . IProgressMonitor ; import org . eclipse . jdt . core . * ; import org . eclipse . jdt . internal . compiler . util . SuffixConstants ; import org . eclipse . jdt . internal . core . * ; import org . eclipse . jdt . internal . core . hierarchy . TypeHierarchy ; public class HierarchyScope extends AbstractSearchScope implements SuffixConstants { public IType focusType ; private String focusPath ; private WorkingCopyOwner owner ; private ITypeHierarchy hierarchy ; private HashSet resourcePaths ; private IPath [ ] enclosingProjectsAndJars ; protected IResource [ ] elements ; protected int elementCount ; public boolean needsRefresh ; private HashSet subTypes = null ; private IJavaProject javaProject = null ; private boolean allowMemberAndEnclosingTypes = true ; private boolean includeFocusType = true ; public void add ( IResource element ) { if ( this . elementCount == this . elements . length ) { System . arraycopy ( this . elements , 0 , this . elements = new IResource [ this . elementCount * 2 ] , 0 , this . elementCount ) ; } this . elements [ this . elementCount ++ ] = element ; } public HierarchyScope ( IJavaProject project , IType type , WorkingCopyOwner owner , boolean onlySubtypes , boolean noMembersOrEnclosingTypes , boolean includeFocusType ) throws JavaModelException { this ( type , owner ) ; this . javaProject = project ; if ( onlySubtypes ) { this . subTypes = new HashSet ( ) ; } this . includeFocusType = includeFocusType ; this . allowMemberAndEnclosingTypes = ! noMembersOrEnclosingTypes ; } public HierarchyScope ( IType type , WorkingCopyOwner owner ) throws JavaModelException { this . focusType = type ; this . owner = owner ; this . enclosingProjectsAndJars = computeProjectsAndJars ( type ) ; IPackageFragmentRoot root = ( IPackageFragmentRoot ) type . getPackageFragment ( ) . getParent ( ) ; if ( root . isArchive ( ) ) { IPath jarPath = root . getPath ( ) ; Object target = JavaModel . getTarget ( jarPath , true ) ; String zipFileName ; if ( target instanceof IFile ) { zipFileName = jarPath . toString ( ) ; } else if ( target instanceof File ) { zipFileName = ( ( File ) target ) . getPath ( ) ; } else { return ; } this . focusPath = zipFileName + JAR_FILE_ENTRY_SEPARATOR + type . getFullyQualifiedName ( ) . replace ( '.' , '/' ) + SUFFIX_STRING_class ; } else { this . focusPath = type . getPath ( ) . toString ( ) ; } this . needsRefresh = true ; } private void buildResourceVector ( ) { HashMap resources = new HashMap ( ) ; HashMap paths = new HashMap ( ) ; IType [ ] types = null ; if ( this . subTypes != null ) { types = this . hierarchy . getAllSubtypes ( this . focusType ) ; if ( this . includeFocusType ) { int len = types . length ; System . arraycopy ( types , 0 , types = new IType [ len + 1 ] , 0 , len ) ; types [ len ] = this . focusType ; } } else { types = this . hierarchy . getAllTypes ( ) ; } for ( int i = 0 ; i < types . length ; i ++ ) { IType type = types [ i ] ; if ( this . subTypes != null ) { this . subTypes . add ( type ) ; } IResource resource = ( ( JavaElement ) type ) . resource ( ) ; if ( resource != null && resources . get ( resource ) == null ) { resources . put ( resource , resource ) ; add ( resource ) ; } IPackageFragmentRoot root = ( IPackageFragmentRoot ) type . getPackageFragment ( ) . getParent ( ) ; if ( root instanceof JarPackageFragmentRoot ) { JarPackageFragmentRoot jar = ( JarPackageFragmentRoot ) root ; IPath jarPath = jar . getPath ( ) ; Object target = JavaModel . getTarget ( jarPath , true ) ; String zipFileName ; if ( target instanceof IFile ) { zipFileName = jarPath . toString ( ) ; } else if ( target instanceof File ) { zipFileName = ( ( File ) target ) . getPath ( ) ; } else { continue ; } String resourcePath = zipFileName + JAR_FILE_ENTRY_SEPARATOR + type . getFullyQualifiedName ( ) . replace ( '.' , '/' ) + SUFFIX_STRING_class ; this . resourcePaths . add ( resourcePath ) ; paths . put ( jarPath , type ) ; } else { paths . put ( type . getJavaProject ( ) . getProject ( ) . getFullPath ( ) , type ) ; } } this . enclosingProjectsAndJars = new IPath [ paths . size ( ) ] ; int i = 0 ; for ( Iterator iter = paths . keySet ( ) . iterator ( ) ; iter . hasNext ( ) ; ) { this . enclosingProjectsAndJars [ i ++ ] = ( IPath ) iter . next ( ) ; } } private IPath [ ] computeProjectsAndJars ( IType type ) throws JavaModelException { HashSet set = new HashSet ( ) ; IPackageFragmentRoot root = ( IPackageFragmentRoot ) type . getPackageFragment ( ) . getParent ( ) ; if ( root . isArchive ( ) ) { set . add ( root . getPath ( ) ) ; IPath rootPath = root . getPath ( ) ; IJavaModel model = JavaModelManager . getJavaModelManager ( ) . getJavaModel ( ) ; IJavaProject [ ] projects = model . getJavaProjects ( ) ; HashSet visited = new HashSet ( ) ; for ( int i = 0 ; i < projects . length ; i ++ ) { JavaProject project = ( JavaProject ) projects [ i ] ; IClasspathEntry entry = project . getClasspathEntryFor ( rootPath ) ; if ( entry != null ) { IPackageFragmentRoot [ ] roots = project . getAllPackageFragmentRoots ( ) ; set . add ( project . getPath ( ) ) ; for ( int k = 0 ; k < roots . length ; k ++ ) { IPackageFragmentRoot pkgFragmentRoot = roots [ k ] ; if ( pkgFragmentRoot . getKind ( ) == IPackageFragmentRoot . K_BINARY ) { set . add ( pkgFragmentRoot . getPath ( ) ) ; } } computeDependents ( project , set , visited ) ; } } } else { IJavaProject project = ( IJavaProject ) root . getParent ( ) ; IPackageFragmentRoot [ ] roots = project . getAllPackageFragmentRoots ( ) ; for ( int i = 0 ; i < roots . length ; i ++ ) { IPackageFragmentRoot pkgFragmentRoot = roots [ i ] ; if ( pkgFragmentRoot . getKind ( ) == IPackageFragmentRoot . K_BINARY ) { set . add ( pkgFragmentRoot . getPath ( ) ) ; } else { set . add ( pkgFragmentRoot . getParent ( ) . getPath ( ) ) ; } } computeDependents ( project , set , new HashSet ( ) ) ; } IPath [ ] result = new IPath [ set . size ( ) ] ; set . toArray ( result ) ; return result ; } private void computeDependents ( IJavaProject project , HashSet set , HashSet visited ) { if ( visited . contains ( project ) ) return ; visited . add ( project ) ; IProject [ ] dependents = project . getProject ( ) . getReferencingProjects ( ) ; for ( int i = 0 ; i < dependents . length ; i ++ ) { try { IJavaProject dependent = JavaCore . create ( dependents [ i ] ) ; IPackageFragmentRoot [ ] roots = dependent . getPackageFragmentRoots ( ) ; set . add ( dependent . getPath ( ) ) ; for ( int j = 0 ; j < roots . length ; j ++ ) { IPackageFragmentRoot pkgFragmentRoot = roots [ j ] ; if ( pkgFragmentRoot . isArchive ( ) ) { set . add ( pkgFragmentRoot . getPath ( ) ) ; } } computeDependents ( dependent , set , visited ) ; } catch ( JavaModelException e ) { } } } public boolean encloses ( String resourcePath ) { return encloses ( resourcePath , null ) ; } public boolean encloses ( String resourcePath , IProgressMonitor progressMonitor ) { if ( this . hierarchy == null ) { if ( resourcePath . equals ( this . focusPath ) ) { return true ; } else { if ( this . needsRefresh ) { try { initialize ( progressMonitor ) ; } catch ( JavaModelException e ) { return false ; } } else { return true ; } } } if ( this . needsRefresh ) { try { refresh ( progressMonitor ) ; } catch ( JavaModelException e ) { return false ; } } int separatorIndex = resourcePath . indexOf ( JAR_FILE_ENTRY_SEPARATOR ) ; if ( separatorIndex != - 1 ) { return this . resourcePaths . contains ( resourcePath ) ; } else { for ( int i = 0 ; i < this . elementCount ; i ++ ) { if ( resourcePath . startsWith ( this . elements [ i ] . getFullPath ( ) . toString ( ) ) ) { return true ; } } } return false ; } public boolean enclosesFineGrained ( IJavaElement element ) { if ( ( this . subTypes == null ) && this . allowMemberAndEnclosingTypes ) return true ; return encloses ( element , null ) ; } public boolean encloses ( IJavaElement element ) { return encloses ( element , null ) ; } public boolean encloses ( IJavaElement element , IProgressMonitor progressMonitor ) { if ( this . hierarchy == null ) { if ( this . includeFocusType && this . focusType . equals ( element . getAncestor ( IJavaElement . TYPE ) ) ) { return true ; } else { if ( this . needsRefresh ) { try { initialize ( progressMonitor ) ; } catch ( JavaModelException e ) { return false ; } } else { return true ; } } } if ( this . needsRefresh ) { try { refresh ( progressMonitor ) ; } catch ( JavaModelException e ) { return false ; } } IType type = null ; if ( element instanceof IType ) { type = ( IType ) element ; } else if ( element instanceof IMember ) { type = ( ( IMember ) element ) . getDeclaringType ( ) ; } if ( type != null ) { if ( this . focusType . equals ( type ) ) return this . includeFocusType ; if ( enclosesType ( type , this . allowMemberAndEnclosingTypes ) ) { return true ; } if ( this . allowMemberAndEnclosingTypes ) { IType enclosing = type . getDeclaringType ( ) ; while ( enclosing != null ) { if ( enclosesType ( enclosing , false ) ) { return true ; } enclosing = enclosing . getDeclaringType ( ) ; } } } return false ; } private boolean enclosesType ( IType type , boolean recurse ) { if ( this . subTypes != null ) { if ( this . subTypes . contains ( type ) ) { return true ; } IType original = type . isBinary ( ) ? null : ( IType ) type . getPrimaryElement ( ) ; if ( original != type && this . subTypes . contains ( original ) ) { return true ; } } else { if ( this . hierarchy . contains ( type ) ) { return true ; } else { IType original ; if ( ! type . isBinary ( ) && ( original = ( IType ) type . getPrimaryElement ( ) ) != null ) { if ( this . hierarchy . contains ( original ) ) { return true ; } } } } if ( recurse ) { try { IType [ ] memberTypes = type . getTypes ( ) ; for ( int i = 0 ; i < memberTypes . length ; i ++ ) { if ( enclosesType ( memberTypes [ i ] , recurse ) ) { return true ; } } } catch ( JavaModelException e ) { return false ; } } return false ; } public IPath [ ] enclosingProjectsAndJars ( ) { if ( this . needsRefresh ) { try { refresh ( null ) ; } catch ( JavaModelException e ) { return new IPath [ 0 ] ; } } return this . enclosingProjectsAndJars ; } protected void initialize ( ) throws JavaModelException { initialize ( null ) ; } protected void initialize ( IProgressMonitor progressMonitor ) throws JavaModelException { this . resourcePaths = new HashSet ( ) ; this . elements = new IResource [ 5 ] ; this . elementCount = 0 ; this . needsRefresh = false ; if ( this . hierarchy == null ) { if ( this . javaProject != null ) { this . hierarchy = this . focusType . newTypeHierarchy ( this . javaProject , this . owner , progressMonitor ) ; } else { this . hierarchy = this . focusType . newTypeHierarchy ( this . owner , progressMonitor ) ; } } else { this . hierarchy . refresh ( progressMonitor ) ; } buildResourceVector ( ) ; } public void processDelta ( IJavaElementDelta delta , int eventType ) { if ( this . needsRefresh ) return ; this . needsRefresh = this . hierarchy == null ? false : ( ( TypeHierarchy ) this . hierarchy ) . isAffected ( delta , eventType ) ; } protected void refresh ( ) throws JavaModelException { refresh ( null ) ; } protected void refresh ( IProgressMonitor progressMonitor ) throws JavaModelException { if ( this . hierarchy != null ) { initialize ( progressMonitor ) ; } } public String toString ( ) { return "" + ( ( JavaElement ) this . focusType ) . toStringWithAncestors ( ) ; } } </s>