<s> package org . eclipse . jdt . internal . core ; import java . util . Enumeration ; import java . util . HashMap ; import java . util . Map ; import org . eclipse . core . resources . * ; import org . eclipse . core . runtime . IProgressMonitor ; import org . eclipse . core . runtime . IStatus ; import org . eclipse . core . runtime . OperationCanceledException ; import org . eclipse . core . runtime . PerformanceStats ; import org . eclipse . jdt . core . * ; import org . eclipse . jdt . internal . codeassist . CompletionEngine ; import org . eclipse . jdt . internal . codeassist . SelectionEngine ; import org . eclipse . jdt . internal . core . util . Util ; public abstract class Openable extends JavaElement implements IOpenable , IBufferChangedListener { protected Openable ( JavaElement parent ) { super ( parent ) ; } public void bufferChanged ( BufferChangedEvent event ) { if ( event . getBuffer ( ) . isClosed ( ) ) { JavaModelManager . getJavaModelManager ( ) . getElementsOutOfSynchWithBuffers ( ) . remove ( this ) ; getBufferManager ( ) . removeBuffer ( event . getBuffer ( ) ) ; } else { JavaModelManager . getJavaModelManager ( ) . getElementsOutOfSynchWithBuffers ( ) . add ( this ) ; } } protected abstract boolean buildStructure ( OpenableElementInfo info , IProgressMonitor pm , Map newElements , IResource underlyingResource ) throws JavaModelException ; public boolean canBeRemovedFromCache ( ) { try { return ! hasUnsavedChanges ( ) ; } catch ( JavaModelException e ) { return false ; } } public boolean canBufferBeRemovedFromCache ( IBuffer buffer ) { return ! buffer . hasUnsavedChanges ( ) ; } protected void closeBuffer ( ) { if ( ! hasBuffer ( ) ) return ; IBuffer buffer = getBufferManager ( ) . getBuffer ( this ) ; if ( buffer != null ) { buffer . close ( ) ; buffer . removeBufferChangedListener ( this ) ; } } protected void closing ( Object info ) { closeBuffer ( ) ; } protected void codeComplete ( org . eclipse . jdt . internal . compiler . env . ICompilationUnit cu , org . eclipse . jdt . internal . compiler . env . ICompilationUnit unitToSkip , int position , CompletionRequestor requestor , WorkingCopyOwner owner , ITypeRoot typeRoot , IProgressMonitor monitor ) throws JavaModelException { if ( requestor == null ) { throw new IllegalArgumentException ( "" ) ; } PerformanceStats performanceStats = CompletionEngine . PERF ? PerformanceStats . getStats ( JavaModelManager . COMPLETION_PERF , this ) : null ; if ( performanceStats != null ) { performanceStats . startRun ( new String ( cu . getFileName ( ) ) + " at " + position ) ; } IBuffer buffer = getBuffer ( ) ; if ( buffer == null ) { return ; } if ( position < - 1 || position > buffer . getLength ( ) ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . INDEX_OUT_OF_BOUNDS ) ) ; } JavaProject project = ( JavaProject ) getJavaProject ( ) ; SearchableEnvironment environment = project . newSearchableNameEnvironment ( owner ) ; environment . unitToSkip = unitToSkip ; CompletionEngine engine = new CompletionEngine ( environment , requestor , project . getOptions ( true ) , project , owner , monitor ) ; engine . complete ( cu , position , 0 , typeRoot ) ; if ( performanceStats != null ) { performanceStats . endRun ( ) ; } if ( NameLookup . VERBOSE ) { System . out . println ( Thread . currentThread ( ) + "" + environment . nameLookup . timeSpentInSeekTypesInSourcePackage + "ms" ) ; System . out . println ( Thread . currentThread ( ) + "" + environment . nameLookup . timeSpentInSeekTypesInBinaryPackage + "ms" ) ; } } protected IJavaElement [ ] codeSelect ( org . eclipse . jdt . internal . compiler . env . ICompilationUnit cu , int offset , int length , WorkingCopyOwner owner ) throws JavaModelException { PerformanceStats performanceStats = SelectionEngine . PERF ? PerformanceStats . getStats ( JavaModelManager . SELECTION_PERF , this ) : null ; if ( performanceStats != null ) { performanceStats . startRun ( new String ( cu . getFileName ( ) ) + " at [" + offset + "," + length + "]" ) ; } JavaProject project = ( JavaProject ) getJavaProject ( ) ; SearchableEnvironment environment = project . newSearchableNameEnvironment ( owner ) ; SelectionRequestor requestor = new SelectionRequestor ( environment . nameLookup , this ) ; IBuffer buffer = getBuffer ( ) ; if ( buffer == null ) { return requestor . getElements ( ) ; } int end = buffer . getLength ( ) ; if ( offset < 0 || length < 0 || offset + length > end ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . INDEX_OUT_OF_BOUNDS ) ) ; } SelectionEngine engine = new SelectionEngine ( environment , requestor , project . getOptions ( true ) , owner ) ; engine . select ( cu , offset , offset + length - 1 ) ; if ( performanceStats != null ) { performanceStats . endRun ( ) ; } if ( NameLookup . VERBOSE ) { System . out . println ( Thread . currentThread ( ) + "" + environment . nameLookup . timeSpentInSeekTypesInSourcePackage + "ms" ) ; System . out . println ( Thread . currentThread ( ) + "" + environment . nameLookup . timeSpentInSeekTypesInBinaryPackage + "ms" ) ; } return requestor . getElements ( ) ; } protected Object createElementInfo ( ) { return new OpenableElementInfo ( ) ; } public boolean exists ( ) { if ( JavaModelManager . getJavaModelManager ( ) . getInfo ( this ) != null ) return true ; switch ( getElementType ( ) ) { case IJavaElement . PACKAGE_FRAGMENT : PackageFragmentRoot root = getPackageFragmentRoot ( ) ; if ( root . isArchive ( ) ) { JarPackageFragmentRootInfo rootInfo ; try { rootInfo = ( JarPackageFragmentRootInfo ) root . getElementInfo ( ) ; } catch ( JavaModelException e ) { return false ; } return rootInfo . rawPackageInfo . containsKey ( ( ( PackageFragment ) this ) . names ) ; } break ; case IJavaElement . CLASS_FILE : if ( getPackageFragmentRoot ( ) . isArchive ( ) ) { return super . exists ( ) ; } break ; } return validateExistence ( resource ( ) ) . isOK ( ) ; } public String findRecommendedLineSeparator ( ) throws JavaModelException { IBuffer buffer = getBuffer ( ) ; String source = buffer == null ? null : buffer . getContents ( ) ; return Util . getLineSeparator ( source , getJavaProject ( ) ) ; } protected void generateInfos ( Object info , HashMap newElements , IProgressMonitor monitor ) throws JavaModelException { if ( JavaModelCache . VERBOSE ) { String element ; switch ( getElementType ( ) ) { case JAVA_PROJECT : element = "project" ; break ; case PACKAGE_FRAGMENT_ROOT : element = "root" ; break ; case PACKAGE_FRAGMENT : element = "package" ; break ; case CLASS_FILE : element = "class file" ; break ; case COMPILATION_UNIT : element = "" ; break ; default : element = "element" ; } System . out . println ( Thread . currentThread ( ) + " OPENING " + element + " " + this . toStringWithAncestors ( ) ) ; } openAncestors ( newElements , monitor ) ; IResource underlResource = resource ( ) ; IStatus status = validateExistence ( underlResource ) ; if ( ! status . isOK ( ) ) throw newJavaModelException ( status ) ; if ( monitor != null && monitor . isCanceled ( ) ) throw new OperationCanceledException ( ) ; newElements . put ( this , info ) ; try { OpenableElementInfo openableElementInfo = ( OpenableElementInfo ) info ; boolean isStructureKnown = buildStructure ( openableElementInfo , monitor , newElements , underlResource ) ; openableElementInfo . setIsStructureKnown ( isStructureKnown ) ; } catch ( JavaModelException e ) { newElements . remove ( this ) ; throw e ; } JavaModelManager . getJavaModelManager ( ) . getElementsOutOfSynchWithBuffers ( ) . remove ( this ) ; if ( JavaModelCache . VERBOSE ) { System . out . println ( JavaModelManager . getJavaModelManager ( ) . cacheToString ( "-> " ) ) ; } } public IBuffer getBuffer ( ) throws JavaModelException { if ( hasBuffer ( ) ) { Object info = getElementInfo ( ) ; IBuffer buffer = getBufferManager ( ) . getBuffer ( this ) ; if ( buffer == null ) { buffer = openBuffer ( null , info ) ; } if ( buffer instanceof NullBuffer ) { return null ; } return buffer ; } else { return null ; } } public IBufferFactory getBufferFactory ( ) { return getBufferManager ( ) . getDefaultBufferFactory ( ) ; } protected BufferManager getBufferManager ( ) { return BufferManager . getDefaultBufferManager ( ) ; } public IResource getCorrespondingResource ( ) throws JavaModelException { return getUnderlyingResource ( ) ; } public IOpenable getOpenable ( ) { return this ; } public IResource getUnderlyingResource ( ) throws JavaModelException { IResource parentResource = this . parent . getUnderlyingResource ( ) ; if ( parentResource == null ) { return null ; } int type = parentResource . getType ( ) ; if ( type == IResource . FOLDER || type == IResource . PROJECT ) { IContainer folder = ( IContainer ) parentResource ; IResource resource = folder . findMember ( getElementName ( ) ) ; if ( resource == null ) { throw newNotPresentException ( ) ; } else { return resource ; } } else { return parentResource ; } } protected boolean hasBuffer ( ) { return false ; } public boolean hasUnsavedChanges ( ) throws JavaModelException { if ( isReadOnly ( ) || ! isOpen ( ) ) { return false ; } IBuffer buf = getBuffer ( ) ; if ( buf != null && buf . hasUnsavedChanges ( ) ) { return true ; } int elementType = getElementType ( ) ; if ( elementType == PACKAGE_FRAGMENT || elementType == PACKAGE_FRAGMENT_ROOT || elementType == JAVA_PROJECT || elementType == JAVA_MODEL ) { Enumeration openBuffers = getBufferManager ( ) . getOpenBuffers ( ) ; while ( openBuffers . hasMoreElements ( ) ) { IBuffer buffer = ( IBuffer ) openBuffers . nextElement ( ) ; if ( buffer . hasUnsavedChanges ( ) ) { IJavaElement owner = ( IJavaElement ) buffer . getOwner ( ) ; if ( isAncestorOf ( owner ) ) { return true ; } } } } return false ; } public boolean isConsistent ( ) { return true ; } public boolean isOpen ( ) { return JavaModelManager . getJavaModelManager ( ) . getInfo ( this ) != null ; } protected boolean isSourceElement ( ) { return false ; } public boolean isStructureKnown ( ) throws JavaModelException { return ( ( OpenableElementInfo ) getElementInfo ( ) ) . isStructureKnown ( ) ; } public void makeConsistent ( IProgressMonitor monitor ) throws JavaModelException { } public void open ( IProgressMonitor pm ) throws JavaModelException { getElementInfo ( pm ) ; } protected IBuffer openBuffer ( IProgressMonitor pm , Object info ) throws JavaModelException { return null ; } public IResource getResource ( ) { PackageFragmentRoot root = getPackageFragmentRoot ( ) ; if ( root != null ) { if ( root . isExternal ( ) ) return null ; if ( root . isArchive ( ) ) return root . resource ( root ) ; } return resource ( root ) ; } public IResource resource ( ) { PackageFragmentRoot root = getPackageFragmentRoot ( ) ; if ( root != null && root . isArchive ( ) ) return root . resource ( root ) ; return resource ( root ) ; } protected abstract IResource resource ( PackageFragmentRoot root ) ; protected boolean resourceExists ( IResource underlyingResource ) { return underlyingResource . isAccessible ( ) ; } public void save ( IProgressMonitor pm , boolean force ) throws JavaModelException { if ( isReadOnly ( ) ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . READ_ONLY , this ) ) ; } IBuffer buf = getBuffer ( ) ; if ( buf != null ) { buf . save ( pm , force ) ; makeConsistent ( pm ) ; } } public PackageFragmentRoot getPackageFragmentRoot ( ) { return ( PackageFragmentRoot ) getAncestor ( IJavaElement . PACKAGE_FRAGMENT_ROOT ) ; } abstract protected IStatus validateExistence ( IResource underlyingResource ) ; protected void openAncestors ( HashMap newElements , IProgressMonitor monitor ) throws JavaModelException { Openable openableParent = ( Openable ) getOpenableParent ( ) ; if ( openableParent != null && ! openableParent . isOpen ( ) ) { openableParent . generateInfos ( openableParent . createElementInfo ( ) , newElements , monitor ) ; } } } </s>