org.eclipse.jdt.internal.core
Class JavaElement

java.lang.Object
  extended by org.eclipse.core.runtime.PlatformObject
      extended by org.eclipse.jdt.internal.core.JavaElement
All Implemented Interfaces:
org.eclipse.core.runtime.IAdaptable, IJavaElement
Direct Known Subclasses:
Openable, SourceRefElement

public abstract class JavaElement
extends org.eclipse.core.runtime.PlatformObject
implements IJavaElement

Root of Java element handle hierarchy.

See Also:
IJavaElement

Field Summary
static char JEM_ANNOTATION
           
static char JEM_CLASSFILE
           
static char JEM_COMPILATIONUNIT
           
static char JEM_COUNT
           
static char JEM_ESCAPE
           
static char JEM_FIELD
           
static char JEM_IMPORTDECLARATION
           
static char JEM_INITIALIZER
           
static char JEM_JAVAPROJECT
           
static char JEM_LOCALVARIABLE
           
static char JEM_METHOD
           
static char JEM_PACKAGEDECLARATION
           
static char JEM_PACKAGEFRAGMENT
           
static char JEM_PACKAGEFRAGMENTROOT
           
static char JEM_TYPE
           
static char 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 close()
           
 boolean equals(java.lang.Object o)
          Returns true if this handle represents the same Java element as the given handle.
 boolean exists()
          Returns whether this Java element exists in the model.
 ASTNode findNode(CompilationUnit ast)
          Returns the ASTNode that corresponds to this JavaElement or null if there is no corresponding node.
 IJavaElement getAncestor(int ancestorType)
          Returns the first ancestor of this Java element that has the given type.
 java.lang.String getAttachedJavadoc(org.eclipse.core.runtime.IProgressMonitor monitor)
          Returns the Javadoc as an html source if this element has an attached javadoc, null otherwise.
 IJavaElement[] getChildren()
           
 java.util.ArrayList getChildrenOfType(int type)
          Returns a collection of (immediate) children of this node of the specified type.
 IClassFile getClassFile()
           
 ICompilationUnit getCompilationUnit()
           
 java.lang.Object getElementInfo()
          Returns the info for this handle.
 java.lang.Object getElementInfo(org.eclipse.core.runtime.IProgressMonitor monitor)
          Returns the info for this handle.
 java.lang.String getElementName()
          Returns the name of this element.
 IJavaElement getHandleFromMemento(MementoTokenizer memento, WorkingCopyOwner owner)
           
abstract  IJavaElement getHandleFromMemento(java.lang.String token, MementoTokenizer memento, WorkingCopyOwner owner)
           
 java.lang.String getHandleIdentifier()
          Returns a string representation of this element handle.
 java.lang.String getHandleMemento()
           
 IJavaModel getJavaModel()
          Returns the Java model.
 IJavaProject getJavaProject()
          Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).
 IOpenable getOpenable()
          Returns the first openable parent.
 IOpenable getOpenableParent()
          Return the first instance of IOpenable in the parent hierarchy of this element.
 IJavaElement getParent()
          Returns the element directly containing this element, or null if this element has no parent.
 IJavaElement getPrimaryElement()
          Returns the primary element (whose compilation unit is the primary compilation unit) this working copy element was created from, or this element if it is a descendant of a primary compilation unit or if it is not a descendant of a working copy (e.g.
 IJavaElement getPrimaryElement(boolean checkOwner)
           
 IResource getResource()
          Returns the innermost resource enclosing this element.
 org.eclipse.core.runtime.jobs.ISchedulingRule getSchedulingRule()
          Returns the scheduling rule associated with this Java element.
 SourceMapper getSourceMapper()
          Returns the SourceMapper facility for this element, or null if this element does not have a SourceMapper.
 boolean hasChildren()
           
 int hashCode()
          Returns the hash code for this Java element.
 boolean isAncestorOf(IJavaElement e)
          Returns true if this element is an ancestor of the given element, otherwise false.
 boolean isReadOnly()
          Returns whether this Java element is read-only.
 JavaModelException newJavaModelException(org.eclipse.core.runtime.IStatus status)
          Creates and returns a new Java model exception for this element with the given status.
 JavaModelException newNotPresentException()
          Creates and returns a new not present exception for this element.
 java.lang.String readableName()
           
 JavaElement resolved(Binding binding)
           
abstract  IResource resource()
           
 java.lang.String toDebugString()
          Debugging purposes
 java.lang.String toString()
          Debugging purposes
 java.lang.Object toStringInfo(int tab, java.lang.StringBuffer buffer)
          Debugging purposes
 java.lang.String toStringWithAncestors()
          Debugging purposes
 java.lang.String toStringWithAncestors(boolean showResolvedInfo)
          Debugging purposes
 JavaElement 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.IJavaElement
getCorrespondingResource, getElementType, getPath, getUnderlyingResource, isStructureKnown
 
Methods inherited from interface org.eclipse.core.runtime.IAdaptable
getAdapter
 

Field Detail

JEM_ESCAPE

public static final char JEM_ESCAPE
See Also:
Constant Field Values

JEM_JAVAPROJECT

public static final char JEM_JAVAPROJECT
See Also:
Constant Field Values

JEM_PACKAGEFRAGMENTROOT

public static final char JEM_PACKAGEFRAGMENTROOT
See Also:
Constant Field Values

JEM_PACKAGEFRAGMENT

public static final char JEM_PACKAGEFRAGMENT
See Also:
Constant Field Values

JEM_FIELD

public static final char JEM_FIELD
See Also:
Constant Field Values

JEM_METHOD

public static final char JEM_METHOD
See Also:
Constant Field Values

JEM_INITIALIZER

public static final char JEM_INITIALIZER
See Also:
Constant Field Values

JEM_COMPILATIONUNIT

public static final char JEM_COMPILATIONUNIT
See Also:
Constant Field Values

JEM_CLASSFILE

public static final char JEM_CLASSFILE
See Also:
Constant Field Values

JEM_TYPE

public static final char JEM_TYPE
See Also:
Constant Field Values

JEM_PACKAGEDECLARATION

public static final char JEM_PACKAGEDECLARATION
See Also:
Constant Field Values

JEM_IMPORTDECLARATION

public static final char JEM_IMPORTDECLARATION
See Also:
Constant Field Values

JEM_COUNT

public static final char JEM_COUNT
See Also:
Constant Field Values

JEM_LOCALVARIABLE

public static final char JEM_LOCALVARIABLE
See Also:
Constant Field Values

JEM_TYPE_PARAMETER

public static final char JEM_TYPE_PARAMETER
See Also:
Constant Field Values

JEM_ANNOTATION

public static final char JEM_ANNOTATION
See Also:
Constant Field Values
Method Detail

close

public void close()
           throws JavaModelException
Throws:
JavaModelException
See Also:
IOpenable

equals

public boolean equals(java.lang.Object o)
Returns true if this handle represents the same Java element as the given handle. By default, two handles represent the same element if they are identical or if they represent the same type of element, have equal names, parents, and occurrence counts.

If a subclass has other requirements for equality, this method must be overridden.

Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

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
Returns:
true if this element exists in the Java model, and false if this element does not exist
See Also:
IJavaElement

findNode

public ASTNode findNode(CompilationUnit ast)
Returns the ASTNode that corresponds to this JavaElement or null if there is no corresponding node.


getAncestor

public IJavaElement getAncestor(int ancestorType)
Description copied from interface: IJavaElement
Returns the first ancestor of this Java element that has the given type. Returns null if no such an ancestor can be found. This is a handle-only method.

Specified by:
getAncestor in interface IJavaElement
Parameters:
ancestorType - the given type
Returns:
the first ancestor of this Java element that has the given type, null if no such an ancestor can be found
See Also:
IJavaElement

getChildren

public IJavaElement[] getChildren()
                           throws JavaModelException
Throws:
JavaModelException
See Also:
IParent

getChildrenOfType

public java.util.ArrayList getChildrenOfType(int type)
                                      throws JavaModelException
Returns a collection of (immediate) children of this node of the specified type.

Parameters:
type - - one of the JEM_* constants defined by JavaElement
Throws:
JavaModelException

getClassFile

public IClassFile getClassFile()
See Also:
IMember

getCompilationUnit

public ICompilationUnit getCompilationUnit()
See Also:
IMember

getElementInfo

public java.lang.Object getElementInfo()
                                throws JavaModelException
Returns the info for this handle. If this element is not already open, it and all of its parents are opened. Does not return null. NOTE: BinaryType infos are NOT rooted under JavaElementInfo.

Throws:
JavaModelException - if the element is not present or not accessible

getElementInfo

public java.lang.Object getElementInfo(org.eclipse.core.runtime.IProgressMonitor monitor)
                                throws JavaModelException
Returns the info for this handle. If this element is not already open, it and all of its parents are opened. Does not return null. NOTE: BinaryType infos are NOT rooted under JavaElementInfo.

Throws:
JavaModelException - if the element is not present or not accessible

getElementName

public java.lang.String getElementName()
Description copied from interface: IJavaElement
Returns the name of this element. This is a handle-only method.

Specified by:
getElementName in interface IJavaElement
Returns:
the element name
See Also:
IAdaptable

getHandleFromMemento

public abstract IJavaElement getHandleFromMemento(java.lang.String token,
                                                  MementoTokenizer memento,
                                                  WorkingCopyOwner owner)

getHandleFromMemento

public IJavaElement getHandleFromMemento(MementoTokenizer memento,
                                         WorkingCopyOwner owner)

getHandleIdentifier

public java.lang.String getHandleIdentifier()
Description copied from interface: IJavaElement
Returns a string representation of this element handle. The format of the string is not specified; however, the identifier is stable across workspace sessions, and can be used to recreate this handle via the JavaCore.create(String) method.

Specified by:
getHandleIdentifier in interface IJavaElement
Returns:
the string handle identifier
See Also:
IJavaElement

getHandleMemento

public java.lang.String getHandleMemento()
See Also:
getHandleMemento()

getJavaModel

public IJavaModel getJavaModel()
Description copied from interface: IJavaElement
Returns the Java model. This is a handle-only method.

Specified by:
getJavaModel in interface IJavaElement
Returns:
the Java model
See Also:
IJavaElement

getJavaProject

public IJavaProject getJavaProject()
Description copied from interface: IJavaElement
Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project). This is a handle-only method.

Specified by:
getJavaProject in interface IJavaElement
Returns:
the containing Java project, or null if this element is not contained in a Java project
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
Returns:
the first openable parent or null if this element doesn't have an openable parent.

getOpenableParent

public IOpenable getOpenableParent()
Return the first instance of IOpenable in the parent hierarchy of this element.

Subclasses that are not IOpenable's must override this method.


getParent

public IJavaElement getParent()
Description copied from interface: IJavaElement
Returns the element directly containing this element, or null if this element has no parent. This is a handle-only method.

Specified by:
getParent in interface IJavaElement
Returns:
the parent element, or null if this element has no parent
See Also:
IJavaElement

getPrimaryElement

public IJavaElement getPrimaryElement()
Description copied from interface: IJavaElement
Returns the primary element (whose compilation unit is the primary compilation unit) this working copy element was created from, or this element if it is a descendant of a primary compilation unit or if it is not a descendant of a working copy (e.g. it is a binary member). The returned element may or may not exist.

Specified by:
getPrimaryElement in interface IJavaElement
Returns:
the primary element this working copy element was created from, or this element.

getPrimaryElement

public IJavaElement getPrimaryElement(boolean checkOwner)

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
Returns:
the innermost resource enclosing this element, null if this element is included in an external archive

resource

public abstract IResource resource()

getSourceMapper

public SourceMapper getSourceMapper()
Returns the SourceMapper facility for this element, or null if this element does not have a SourceMapper.


getSchedulingRule

public org.eclipse.core.runtime.jobs.ISchedulingRule getSchedulingRule()
Description copied from interface: IJavaElement
Returns the scheduling rule associated with this Java element. This is a handle-only method.

Specified by:
getSchedulingRule in interface IJavaElement
Returns:
the scheduling rule associated with this Java element

hasChildren

public boolean hasChildren()
                    throws JavaModelException
Throws:
JavaModelException
See Also:
IParent

hashCode

public int hashCode()
Returns the hash code for this Java element. By default, the hash code for an element is a combination of its name and parent's hash code. Elements with other requirements must override this method.

Overrides:
hashCode in class java.lang.Object

isAncestorOf

public boolean isAncestorOf(IJavaElement e)
Returns true if this element is an ancestor of the given element, otherwise false.


isReadOnly

public boolean isReadOnly()
Description copied from interface: IJavaElement
Returns whether this Java element is read-only. An element is read-only if its structure cannot be modified by the java model.

Note this is different from IResource.isReadOnly(). For example, .jar files are read-only as the java model doesn't know how to add/remove elements in this file, but the underlying IFile can be writable.

This is a handle-only method.

Specified by:
isReadOnly in interface IJavaElement
Returns:
true if this element is read-only
See Also:
IJavaElement

newNotPresentException

public JavaModelException newNotPresentException()
Creates and returns a new not present exception for this element.


newJavaModelException

public JavaModelException newJavaModelException(org.eclipse.core.runtime.IStatus status)
Creates and returns a new Java model exception for this element with the given status.


readableName

public java.lang.String readableName()

resolved

public JavaElement resolved(Binding binding)

unresolved

public JavaElement unresolved()

toDebugString

public java.lang.String toDebugString()
Debugging purposes


toString

public java.lang.String toString()
Debugging purposes

Overrides:
toString in class java.lang.Object

toStringWithAncestors

public java.lang.String toStringWithAncestors()
Debugging purposes


toStringWithAncestors

public java.lang.String toStringWithAncestors(boolean showResolvedInfo)
Debugging purposes


toStringInfo

public java.lang.Object toStringInfo(int tab,
                                     java.lang.StringBuffer buffer)
Debugging purposes


getAttachedJavadoc

public java.lang.String getAttachedJavadoc(org.eclipse.core.runtime.IProgressMonitor monitor)
                                    throws JavaModelException
Description copied from interface: IJavaElement

Returns the Javadoc as an html source if this element has an attached javadoc, null otherwise.

This should be used only for binary elements. Source elements will always return null.

The encoding used to read the javadoc is the one defined by the content type of the file. If none is defined, then the project's encoding of this java element is used. If the project's encoding cannot be retrieved, then the platform encoding is used.

In case of the javadoc doesn't exist for this element, null is returned.

The html is extracted from the attached javadoc and provided as is. No transformation or validation is done.

Specified by:
getAttachedJavadoc in interface IJavaElement
Parameters:
monitor - the given progress monitor
Returns:
the extracted javadoc from the attached javadoc, null if none
Throws:
JavaModelException - if:
  • this element does not exist
  • retrieving the attached javadoc fails (timed-out, invalid URL, ...)
  • the format of the javadoc doesn't match expected standards (different anchors,...)
See Also:
IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME