org.eclipse.jdt.internal.core
Class Openable

java.lang.Object
  extended by org.eclipse.core.runtime.PlatformObject
      extended by org.eclipse.jdt.internal.core.JavaElement
          extended by org.eclipse.jdt.internal.core.Openable
All Implemented Interfaces:
org.eclipse.core.runtime.IAdaptable, IBufferChangedListener, IJavaElement, IOpenable
Direct Known Subclasses:
ClassFile, CompilationUnit, JavaModel, JavaProject, OverflowingCacheTests.OverflowingTestOpenable, PackageFragment, PackageFragmentRoot

public abstract class Openable
extends JavaElement
implements IOpenable, IBufferChangedListener

Abstract class for implementations of java elements which are IOpenable.

See Also:
IJavaElement, IOpenable

Field Summary
 
Fields inherited from class org.eclipse.jdt.internal.core.JavaElement
JEM_ANNOTATION, JEM_CLASSFILE, JEM_COMPILATIONUNIT, JEM_COUNT, JEM_ESCAPE, JEM_FIELD, JEM_IMPORTDECLARATION, JEM_INITIALIZER, JEM_JAVAPROJECT, JEM_LOCALVARIABLE, JEM_METHOD, JEM_PACKAGEDECLARATION, JEM_PACKAGEFRAGMENT, JEM_PACKAGEFRAGMENTROOT, JEM_TYPE, JEM_TYPE_PARAMETER
 
Fields inherited from interface org.eclipse.jdt.core.IJavaElement
ANNOTATION, CLASS_FILE, COMPILATION_UNIT, FIELD, IMPORT_CONTAINER, IMPORT_DECLARATION, INITIALIZER, JAVA_MODEL, JAVA_PROJECT, LOCAL_VARIABLE, METHOD, PACKAGE_DECLARATION, PACKAGE_FRAGMENT, PACKAGE_FRAGMENT_ROOT, TYPE, TYPE_PARAMETER
 
Method Summary
 void bufferChanged(BufferChangedEvent event)
          The buffer associated with this element has changed.
 boolean canBeRemovedFromCache()
           
 boolean canBufferBeRemovedFromCache(IBuffer buffer)
           
 boolean exists()
          Returns whether this Java element exists in the model.
 java.lang.String findRecommendedLineSeparator()
          Finds and returns the recommended line separator for this element.
 IBuffer getBuffer()
          Note: a buffer with no unsaved changes can be closed by the Java Model since it has a finite number of buffers allowed open at one time.
 IBufferFactory getBufferFactory()
          Deprecated.  
 IResource getCorrespondingResource()
          Return my underlying resource.
 IOpenable getOpenable()
          Returns the first openable parent.
 PackageFragmentRoot getPackageFragmentRoot()
          Find enclosing package fragment root if any
 IResource getResource()
          Returns the innermost resource enclosing this element.
 IResource getUnderlyingResource()
          Returns the smallest underlying resource that contains this element, or null if this element is not contained in a resource.
 boolean hasUnsavedChanges()
          Returns true if this element is open and: its buffer has unsaved changes, or one of its descendants has unsaved changes, or a working copy has been created on one of this element's children and has not yet destroyed
 boolean isConsistent()
          Subclasses must override as required.
 boolean isOpen()
          Returns whether this openable is open.
 boolean isStructureKnown()
          Returns whether the structure of this element is known.
 void makeConsistent(org.eclipse.core.runtime.IProgressMonitor monitor)
          Makes this element consistent with its underlying resource or buffer by updating the element's structure and properties as necessary.
 void open(org.eclipse.core.runtime.IProgressMonitor pm)
          Opens this element and all parent elements that are not already open.
 IResource resource()
           
 void save(org.eclipse.core.runtime.IProgressMonitor pm, boolean force)
          Saves any changes in this element's buffer to its underlying resource via a workspace resource operation.
 
Methods inherited from class org.eclipse.jdt.internal.core.JavaElement
close, equals, findNode, getAncestor, getAttachedJavadoc, getChildren, getChildrenOfType, getClassFile, getCompilationUnit, getElementInfo, getElementInfo, getElementName, getHandleFromMemento, getHandleFromMemento, getHandleIdentifier, getHandleMemento, getJavaModel, getJavaProject, getOpenableParent, getParent, getPrimaryElement, getPrimaryElement, getSchedulingRule, getSourceMapper, hasChildren, hashCode, isAncestorOf, isReadOnly, newJavaModelException, newNotPresentException, readableName, resolved, toDebugString, toString, toStringInfo, toStringWithAncestors, toStringWithAncestors, unresolved
 
Methods inherited from class org.eclipse.core.runtime.PlatformObject
getAdapter
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.eclipse.jdt.core.IOpenable
close
 
Methods inherited from interface org.eclipse.jdt.core.IJavaElement
getElementType, getPath
 
Methods inherited from interface org.eclipse.core.runtime.IAdaptable
getAdapter
 

Method Detail

bufferChanged

public void bufferChanged(BufferChangedEvent event)
The buffer associated with this element has changed. Registers this element as being out of synch with its buffer's contents. If the buffer has been closed, this element is set as NOT out of synch with the contents.

Specified by:
bufferChanged in interface IBufferChangedListener
Parameters:
event - the change event
See Also:
IBufferChangedListener

canBeRemovedFromCache

public boolean canBeRemovedFromCache()

canBufferBeRemovedFromCache

public boolean canBufferBeRemovedFromCache(IBuffer buffer)

exists

public boolean exists()
Description copied from interface: IJavaElement
Returns whether this Java element exists in the model.

Java elements are handle objects that may or may not be backed by an actual element. Java elements that are backed by an actual element are said to "exist", and this method returns true. For Java elements that are not working copies, it is always the case that if the element exists, then its parent also exists (provided it has one) and includes the element as one of its children. It is therefore possible to navigated to any existing Java element from the root of the Java model along a chain of existing Java elements. On the other hand, working copies are said to exist until they are destroyed (with IWorkingCopy.destroy). Unlike regular Java elements, a working copy never shows up among the children of its parent element (which may or may not exist).

Specified by:
exists in interface IJavaElement
Overrides:
exists in class JavaElement
Returns:
true if this element exists in the Java model, and false if this element does not exist
See Also:
IJavaElement

findRecommendedLineSeparator

public java.lang.String findRecommendedLineSeparator()
                                              throws JavaModelException
Description copied from interface: IOpenable
Finds and returns the recommended line separator for this element. The element's buffer is first searched and the first line separator in this buffer is returned if any. Otherwise the preference Platform.PREF_LINE_SEPARATOR on this element's project or workspace is returned. Finally if no such preference is set, the system line separator is returned.

Specified by:
findRecommendedLineSeparator in interface IOpenable
Returns:
the recommended line separator for this element
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource.

getBuffer

public IBuffer getBuffer()
                  throws JavaModelException
Note: a buffer with no unsaved changes can be closed by the Java Model since it has a finite number of buffers allowed open at one time. If this is the first time a request is being made for the buffer, an attempt is made to create and fill this element's buffer. If the buffer has been closed since it was first opened, the buffer is re-created.

Specified by:
getBuffer in interface IOpenable
Returns:
the buffer opened for this element, or null if this element does not have a buffer
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource.
See Also:
IOpenable

getBufferFactory

public IBufferFactory getBufferFactory()
Deprecated. 

Answers the buffer factory to use for creating new buffers


getCorrespondingResource

public IResource getCorrespondingResource()
                                   throws JavaModelException
Return my underlying resource. Elements that may not have a corresponding resource must override this method.

Specified by:
getCorrespondingResource in interface IJavaElement
Returns:
the corresponding resource, or null if none
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource
See Also:
IJavaElement

getOpenable

public IOpenable getOpenable()
Description copied from interface: IJavaElement
Returns the first openable parent. If this element is openable, the element itself is returned. Returns null if this element doesn't have an openable parent. This is a handle-only method.

Specified by:
getOpenable in interface IJavaElement
Overrides:
getOpenable in class JavaElement
Returns:
the first openable parent or null if this element doesn't have an openable parent.

getUnderlyingResource

public IResource getUnderlyingResource()
                                throws JavaModelException
Description copied from interface: IJavaElement
Returns the smallest underlying resource that contains this element, or null if this element is not contained in a resource.

Specified by:
getUnderlyingResource in interface IJavaElement
Returns:
the underlying resource, or null if none
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its underlying resource
See Also:
IJavaElement

hasUnsavedChanges

public boolean hasUnsavedChanges()
                          throws JavaModelException
Description copied from interface: IOpenable
Returns true if this element is open and:

Specified by:
hasUnsavedChanges in interface IOpenable
Returns:
true if this element is open and:
  • its buffer has unsaved changes, or
  • one of its descendants has unsaved changes, or
  • a working copy has been created on one of this element's children and has not yet destroyed
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource.
See Also:
IOpenable

isConsistent

public boolean isConsistent()
Subclasses must override as required.

Specified by:
isConsistent in interface IOpenable
Returns:
true if the element is consistent with its underlying resource or buffer, false otherwise.
See Also:
IOpenable

isOpen

public boolean isOpen()
Description copied from interface: IOpenable
Returns whether this openable is open. This is a handle-only method.

Specified by:
isOpen in interface IOpenable
Returns:
true if this openable is open, false otherwise
See Also:
IOpenable

isStructureKnown

public boolean isStructureKnown()
                         throws JavaModelException
Description copied from interface: IJavaElement
Returns whether the structure of this element is known. For example, for a compilation unit that has syntax errors, false is returned. If the structure of an element is unknown, navigations will return reasonable defaults. For example, getChildren for a compilation unit with syntax errors will return a collection of the children that could be parsed.

Note: This does not imply anything about consistency with the underlying resource/buffer contents.

Specified by:
isStructureKnown in interface IJavaElement
Returns:
true if the structure of this element is known
Throws:
JavaModelException - if this element does not exist or if an exception occurs while accessing its corresponding resource
See Also:
IJavaElement

makeConsistent

public void makeConsistent(org.eclipse.core.runtime.IProgressMonitor monitor)
                    throws JavaModelException
Description copied from interface: IOpenable
Makes this element consistent with its underlying resource or buffer by updating the element's structure and properties as necessary.

Note: Using this functionality on a working copy will interfere with any subsequent reconciling operation. Indeed, the next ICompilationUnit.reconcile(int, boolean, WorkingCopyOwner, IProgressMonitor) or ICompilationUnit.reconcile(int, boolean, boolean, WorkingCopyOwner, IProgressMonitor) operation will not account for changes which occurred before an explicit use of IOpenable.makeConsistent(IProgressMonitor)

Specified by:
makeConsistent in interface IOpenable
Parameters:
monitor - the given progress monitor
Throws:
JavaModelException - if the element is unable to access the contents of its underlying resource. Reasons include:
  • This Java element does not exist (ELEMENT_DOES_NOT_EXIST)
See Also:
IOpenable

open

public void open(org.eclipse.core.runtime.IProgressMonitor pm)
          throws JavaModelException
Description copied from interface: IOpenable
Opens this element and all parent elements that are not already open. For compilation units, a buffer is opened on the contents of the underlying resource.

Note: although IOpenable.open(org.eclipse.core.runtime.IProgressMonitor) is exposed in the API, clients are not expected to open and close elements - the Java model does this automatically as elements are accessed.

Specified by:
open in interface IOpenable
Parameters:
pm - the given progress monitor
Throws:
JavaModelException - if an error occurs accessing the contents of its underlying resource. Reasons include:
  • This Java element does not exist (ELEMENT_DOES_NOT_EXIST)
See Also:
IOpenable

getResource

public IResource getResource()
Description copied from interface: IJavaElement
Returns the innermost resource enclosing this element. If this element is included in an archive and this archive is not external, this is the underlying resource corresponding to the archive. If this element is included in an external library, null is returned. This is a handle-only method.

Specified by:
getResource in interface IJavaElement
Overrides:
getResource in class JavaElement
Returns:
the innermost resource enclosing this element, null if this element is included in an external archive

resource

public IResource resource()
Specified by:
resource in class JavaElement

save

public void save(org.eclipse.core.runtime.IProgressMonitor pm,
                 boolean force)
          throws JavaModelException
Description copied from interface: IOpenable
Saves any changes in this element's buffer to its underlying resource via a workspace resource operation. This has no effect if the element has no underlying buffer, or if there are no unsaved changed in the buffer.

The force parameter controls how this method deals with cases where the workbench is not completely in sync with the local file system. If false is specified, this method will only attempt to overwrite a corresponding file in the local file system provided it is in sync with the workbench. This option ensures there is no unintended data loss; it is the recommended setting. However, if true is specified, an attempt will be made to write a corresponding file in the local file system, overwriting any existing one if need be. In either case, if this method succeeds, the resource will be marked as being local (even if it wasn't before).

As a result of this operation, the element is consistent with its underlying resource or buffer.

Specified by:
save in interface IOpenable
Parameters:
pm - the given progress monitor
force - it controls how this method deals with cases where the workbench is not completely in sync with the local file system
Throws:
JavaModelException - if an error occurs accessing the contents of its underlying resource. Reasons include:
  • This Java element does not exist (ELEMENT_DOES_NOT_EXIST)
  • This Java element is read-only (READ_ONLY)
See Also:
IOpenable

getPackageFragmentRoot

public PackageFragmentRoot getPackageFragmentRoot()
Find enclosing package fragment root if any