<s> package org . eclipse . jdt . internal . core . hierarchy ; import java . util . HashMap ; import java . util . Map ; import org . eclipse . core . resources . IFile ; import org . eclipse . core . resources . IResource ; import org . eclipse . core . runtime . CoreException ; import org . eclipse . core . runtime . IProgressMonitor ; import org . eclipse . core . runtime . OperationCanceledException ; import org . eclipse . jdt . core . IType ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . core . util . CompilerUtils ; import org . eclipse . jdt . internal . compiler . ast . TypeDeclaration ; import org . eclipse . jdt . internal . compiler . env . IBinaryType ; import org . eclipse . jdt . internal . compiler . env . ICompilationUnit ; import org . eclipse . jdt . internal . compiler . env . IGenericType ; import org . eclipse . jdt . internal . compiler . lookup . ReferenceBinding ; import org . eclipse . jdt . internal . compiler . problem . DefaultProblemFactory ; import org . eclipse . jdt . internal . core . * ; import org . eclipse . jdt . internal . core . util . ResourceCompilationUnit ; import org . eclipse . jdt . internal . core . util . Util ; public abstract class HierarchyBuilder { protected TypeHierarchy hierarchy ; protected NameLookup nameLookup ; protected HierarchyResolver hierarchyResolver ; protected Map infoToHandle ; protected String focusQualifiedName ; public HierarchyBuilder ( TypeHierarchy hierarchy ) throws JavaModelException { this . hierarchy = hierarchy ; JavaProject project = ( JavaProject ) hierarchy . javaProject ( ) ; IType focusType = hierarchy . getType ( ) ; org . eclipse . jdt . core . ICompilationUnit unitToLookInside = focusType == null ? null : focusType . getCompilationUnit ( ) ; org . eclipse . jdt . core . ICompilationUnit [ ] workingCopies = this . hierarchy . workingCopies ; org . eclipse . jdt . core . ICompilationUnit [ ] unitsToLookInside ; if ( unitToLookInside != null ) { int wcLength = workingCopies == null ? 0 : workingCopies . length ; if ( wcLength == 0 ) { unitsToLookInside = new org . eclipse . jdt . core . ICompilationUnit [ ] { unitToLookInside } ; } else { unitsToLookInside = new org . eclipse . jdt . core . ICompilationUnit [ wcLength + 1 ] ; unitsToLookInside [ 0 ] = unitToLookInside ; System . arraycopy ( workingCopies , 0 , unitsToLookInside , 1 , wcLength ) ; } } else { unitsToLookInside = workingCopies ; } if ( project != null ) { Map optionMap = project . getOptions ( true ) ; CompilerUtils . configureOptionsBasedOnNature ( optionMap , project ) ; SearchableEnvironment searchableEnvironment = project . newSearchableNameEnvironment ( unitsToLookInside ) ; this . nameLookup = searchableEnvironment . nameLookup ; this . hierarchyResolver = new HierarchyResolver ( searchableEnvironment , optionMap , this , new DefaultProblemFactory ( ) ) ; } this . infoToHandle = new HashMap ( 5 ) ; this . focusQualifiedName = focusType == null ? null : focusType . getFullyQualifiedName ( ) ; } public abstract void build ( boolean computeSubtypes ) throws JavaModelException , CoreException ; protected void buildSupertypes ( ) { IType focusType = getType ( ) ; if ( focusType == null ) return ; IGenericType type ; try { type = ( IGenericType ) ( ( JavaElement ) focusType ) . getElementInfo ( ) ; } catch ( JavaModelException e ) { return ; } this . hierarchyResolver . resolve ( type ) ; if ( ! this . hierarchy . contains ( focusType ) ) { this . hierarchy . addRootClass ( focusType ) ; } } public void connect ( IGenericType type , IType typeHandle , IType superclassHandle , IType [ ] superinterfaceHandles ) { if ( typeHandle == null ) return ; if ( TypeHierarchy . DEBUG ) { System . out . println ( "Connecting: " + ( ( JavaElement ) typeHandle ) . toStringWithAncestors ( ) ) ; System . out . println ( "" + ( superclassHandle == null ? "<None>" : ( ( JavaElement ) superclassHandle ) . toStringWithAncestors ( ) ) ) ; System . out . print ( "" ) ; if ( superinterfaceHandles == null || superinterfaceHandles . length == 0 ) { System . out . println ( " <None>" ) ; } else { System . out . println ( ) ; for ( int i = 0 , length = superinterfaceHandles . length ; i < length ; i ++ ) { if ( superinterfaceHandles [ i ] == null ) continue ; System . out . println ( " " + ( ( JavaElement ) superinterfaceHandles [ i ] ) . toStringWithAncestors ( ) ) ; } } } switch ( TypeDeclaration . kind ( type . getModifiers ( ) ) ) { case TypeDeclaration . CLASS_DECL : case TypeDeclaration . ENUM_DECL : if ( superclassHandle == null ) { this . hierarchy . addRootClass ( typeHandle ) ; } else { this . hierarchy . cacheSuperclass ( typeHandle , superclassHandle ) ; } break ; case TypeDeclaration . INTERFACE_DECL : case TypeDeclaration . ANNOTATION_TYPE_DECL : if ( this . hierarchy . typeToSuperInterfaces . get ( typeHandle ) == null ) this . hierarchy . addInterface ( typeHandle ) ; break ; } if ( superinterfaceHandles == null ) { superinterfaceHandles = TypeHierarchy . NO_TYPE ; } this . hierarchy . cacheSuperInterfaces ( typeHandle , superinterfaceHandles ) ; this . hierarchy . cacheFlags ( typeHandle , type . getModifiers ( ) ) ; } protected IType getHandle ( IGenericType genericType , ReferenceBinding binding ) { if ( genericType == null ) return null ; if ( genericType instanceof HierarchyType ) { IType handle = ( IType ) this . infoToHandle . get ( genericType ) ; if ( handle == null ) { handle = ( ( HierarchyType ) genericType ) . typeHandle ; handle = ( IType ) ( ( JavaElement ) handle ) . resolved ( binding ) ; this . infoToHandle . put ( genericType , handle ) ; } return handle ; } else if ( genericType . isBinaryType ( ) ) { ClassFile classFile = ( ClassFile ) this . infoToHandle . get ( genericType ) ; if ( classFile == null ) { IType handle = lookupBinaryHandle ( ( IBinaryType ) genericType ) ; if ( handle == null ) return null ; classFile = ( ClassFile ) handle . getParent ( ) ; this . infoToHandle . put ( genericType , classFile ) ; } return new ResolvedBinaryType ( classFile , classFile . getTypeName ( ) , new String ( binding . computeUniqueKey ( ) ) ) ; } else if ( genericType instanceof SourceTypeElementInfo ) { IType handle = ( ( SourceTypeElementInfo ) genericType ) . getHandle ( ) ; return ( IType ) ( ( JavaElement ) handle ) . resolved ( binding ) ; } else return null ; } protected IType getType ( ) { return this . hierarchy . getType ( ) ; } protected IType lookupBinaryHandle ( IBinaryType typeInfo ) { int flag ; String qualifiedName ; switch ( TypeDeclaration . kind ( typeInfo . getModifiers ( ) ) ) { case TypeDeclaration . CLASS_DECL : flag = NameLookup . ACCEPT_CLASSES ; break ; case TypeDeclaration . INTERFACE_DECL : flag = NameLookup . ACCEPT_INTERFACES ; break ; case TypeDeclaration . ENUM_DECL : flag = NameLookup . ACCEPT_ENUMS ; break ; default : flag = NameLookup . ACCEPT_ANNOTATIONS ; break ; } char [ ] bName = typeInfo . getName ( ) ; qualifiedName = new String ( ClassFile . translatedName ( bName ) ) ; if ( qualifiedName . equals ( this . focusQualifiedName ) ) return getType ( ) ; NameLookup . Answer answer = this . nameLookup . findType ( qualifiedName , false , flag , true , false , false , null ) ; return answer == null || answer . type == null || ! answer . type . isBinary ( ) ? null : answer . type ; } protected void worked ( IProgressMonitor monitor , int work ) { if ( monitor != null ) { if ( monitor . isCanceled ( ) ) { throw new OperationCanceledException ( ) ; } else { monitor . worked ( work ) ; } } } protected ICompilationUnit createCompilationUnitFromPath ( Openable handle , IFile file ) { final char [ ] elementName = handle . getElementName ( ) . toCharArray ( ) ; return new ResourceCompilationUnit ( file , file . getLocationURI ( ) ) { public char [ ] getFileName ( ) { return elementName ; } } ; } protected IBinaryType createInfoFromClassFile ( Openable handle , IResource file ) { IBinaryType info = null ; try { info = Util . newClassFileReader ( file ) ; } catch ( org . eclipse . jdt . internal . compiler . classfmt . ClassFormatException e ) { if ( TypeHierarchy . DEBUG ) { e . printStackTrace ( ) ; } return null ; } catch ( java . io . IOException e ) { if ( TypeHierarchy . DEBUG ) { e . printStackTrace ( ) ; } return null ; } catch ( CoreException e ) { if ( TypeHierarchy . DEBUG ) { e . printStackTrace ( ) ; } return null ; } this . infoToHandle . put ( info , handle ) ; return info ; } protected IBinaryType createInfoFromClassFileInJar ( Openable classFile ) { PackageFragment pkg = ( PackageFragment ) classFile . getParent ( ) ; String classFilePath = Util . concatWith ( pkg . names , classFile . getElementName ( ) , '/' ) ; IBinaryType info = null ; java . util . zip . ZipFile zipFile = null ; try { zipFile = ( ( JarPackageFragmentRoot ) pkg . getParent ( ) ) . getJar ( ) ; info = org . eclipse . jdt . internal . compiler . classfmt . ClassFileReader . read ( zipFile , classFilePath ) ; } catch ( org . eclipse . jdt . internal . compiler . classfmt . ClassFormatException e ) { if ( TypeHierarchy . DEBUG ) { e . printStackTrace ( ) ; } return null ; } catch ( java . io . IOException e ) { if ( TypeHierarchy . DEBUG ) { e . printStackTrace ( ) ; } return null ; } catch ( CoreException e ) { if ( TypeHierarchy . DEBUG ) { e . printStackTrace ( ) ; } return null ; } finally { JavaModelManager . getJavaModelManager ( ) . closeZipFile ( zipFile ) ; } this . infoToHandle . put ( info , classFile ) ; return info ; } } </s>