Interface IJavaElement
-
- All Superinterfaces:
IAdaptable
- All Known Subinterfaces:
IAnnotation,IClassFile,ICompilationUnit,IField,IImportContainer,IImportDeclaration,IInitializer,IJavaModel,IJavaProject,ILocalVariable,IMember,IMethod,IModularClassFile,IModuleDescription,IOrdinaryClassFile,IPackageDeclaration,IPackageFragment,IPackageFragmentRoot,IType,ITypeParameter,ITypeRoot
public interface IJavaElement extends IAdaptable
Common protocol for all elements provided by the Java model. Java model elements are exposed to clients as handles to the actual underlying element. The Java model may hand out any number of handles for each element. Handles that refer to the same element are guaranteed to be equal, but not necessarily identical.Methods annotated as "handle-only" do not require underlying elements to exist. Methods that require underlying elements to exist throw a
JavaModelExceptionwhen an underlying element is missing.JavaModelException.isDoesNotExistcan be used to recognize this common special case.- Restriction:
- This interface is not intended to be implemented by clients.
-
-
Field Summary
Fields Modifier and Type Field Description static intANNOTATIONConstant representing an annotation.static intCLASS_FILEConstant representing a class file.static intCOMPILATION_UNITConstant representing a Java compilation unit.static intFIELDConstant representing a field.static intIMPORT_CONTAINERConstant representing all import declarations within a compilation unit.static intIMPORT_DECLARATIONConstant representing an import declaration within a compilation unit.static intINITIALIZERConstant representing a stand-alone instance or class initializer.static intJAVA_MODELConstant representing a Java model (workspace level object).static intJAVA_MODULEConstant represents a module descriptor.static intJAVA_PROJECTConstant representing a Java project.static intLOCAL_VARIABLEConstant representing a local variable declaration.static intMETHODConstant representing a method or constructor.static intPACKAGE_DECLARATIONConstant representing a package declaration within a compilation unit.static intPACKAGE_FRAGMENTConstant representing a package fragment.static intPACKAGE_FRAGMENT_ROOTConstant representing a package fragment root.static intTYPEConstant representing a type (a class or interface).static intTYPE_PARAMETERConstant representing a type parameter declaration.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleanexists()Returns whether this Java element exists in the model.IJavaElementgetAncestor(int ancestorType)Returns this Java element or the first ancestor of this element that has the given type.StringgetAttachedJavadoc(IProgressMonitor monitor)Returns the Javadoc as HTML source if this element has attached Javadoc,nullotherwise.IResourcegetCorrespondingResource()Returns the resource that corresponds directly to this element, ornullif there is no resource that corresponds to this element.StringgetElementName()Returns the name of this element.intgetElementType()Returns this element's kind encoded as an integer.StringgetHandleIdentifier()Returns a string representation of this element handle.IJavaModelgetJavaModel()Returns the Java model.IJavaProjectgetJavaProject()Returns the Java project this element is contained in, ornullif this element is not contained in any Java project (for instance, theIJavaModelis not contained in any Java project).IOpenablegetOpenable()Returns the first openable parent.IJavaElementgetParent()Returns the element directly containing this element, ornullif this element has no parent.IPathgetPath()Returns the path to the innermost resource enclosing this element.IJavaElementgetPrimaryElement()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).IResourcegetResource()Returns the innermost resource enclosing this element.ISchedulingRulegetSchedulingRule()Returns the scheduling rule associated with this Java element.IResourcegetUnderlyingResource()Returns the smallest underlying resource that contains this element, ornullif this element is not contained in a resource.booleanisReadOnly()Returns whether this Java element is read-only.booleanisStructureKnown()Returns whether the structure of this element is known.-
Methods inherited from interface org.eclipse.core.runtime.IAdaptable
getAdapter
-
-
-
-
Field Detail
-
JAVA_MODEL
static final int JAVA_MODEL
Constant representing a Java model (workspace level object). A Java element with this type can be safely cast toIJavaModel.- See Also:
- Constant Field Values
-
JAVA_PROJECT
static final int JAVA_PROJECT
Constant representing a Java project. A Java element with this type can be safely cast toIJavaProject.- See Also:
- Constant Field Values
-
PACKAGE_FRAGMENT_ROOT
static final int PACKAGE_FRAGMENT_ROOT
Constant representing a package fragment root. A Java element with this type can be safely cast toIPackageFragmentRoot.- See Also:
- Constant Field Values
-
PACKAGE_FRAGMENT
static final int PACKAGE_FRAGMENT
Constant representing a package fragment. A Java element with this type can be safely cast toIPackageFragment.- See Also:
- Constant Field Values
-
COMPILATION_UNIT
static final int COMPILATION_UNIT
Constant representing a Java compilation unit. A Java element with this type can be safely cast toICompilationUnit.- See Also:
- Constant Field Values
-
CLASS_FILE
static final int CLASS_FILE
Constant representing a class file. A Java element with this type can be safely cast toIClassFile.- See Also:
- Constant Field Values
-
TYPE
static final int TYPE
Constant representing a type (a class or interface). A Java element with this type can be safely cast toIType.- See Also:
- Constant Field Values
-
FIELD
static final int FIELD
Constant representing a field. A Java element with this type can be safely cast toIField.- See Also:
- Constant Field Values
-
METHOD
static final int METHOD
Constant representing a method or constructor. A Java element with this type can be safely cast toIMethod.- See Also:
- Constant Field Values
-
INITIALIZER
static final int INITIALIZER
Constant representing a stand-alone instance or class initializer. A Java element with this type can be safely cast toIInitializer.- See Also:
- Constant Field Values
-
PACKAGE_DECLARATION
static final int PACKAGE_DECLARATION
Constant representing a package declaration within a compilation unit. A Java element with this type can be safely cast toIPackageDeclaration.- See Also:
- Constant Field Values
-
IMPORT_CONTAINER
static final int IMPORT_CONTAINER
Constant representing all import declarations within a compilation unit. A Java element with this type can be safely cast toIImportContainer.- See Also:
- Constant Field Values
-
IMPORT_DECLARATION
static final int IMPORT_DECLARATION
Constant representing an import declaration within a compilation unit. A Java element with this type can be safely cast toIImportDeclaration.- See Also:
- Constant Field Values
-
LOCAL_VARIABLE
static final int LOCAL_VARIABLE
Constant representing a local variable declaration. A Java element with this type can be safely cast toILocalVariable.- Since:
- 3.0
- See Also:
- Constant Field Values
-
TYPE_PARAMETER
static final int TYPE_PARAMETER
Constant representing a type parameter declaration. A Java element with this type can be safely cast toITypeParameter.- Since:
- 3.1
- See Also:
- Constant Field Values
-
ANNOTATION
static final int ANNOTATION
Constant representing an annotation. A Java element with this type can be safely cast toIAnnotation.- Since:
- 3.4
- See Also:
- Constant Field Values
-
JAVA_MODULE
static final int JAVA_MODULE
Constant represents a module descriptor. A Java element with this type can be safely cast toIModuleDescription.- Since:
- 3.14
- See Also:
- Constant Field Values
-
-
Method Detail
-
exists
boolean exists()
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 (withIWorkingCopy.destroy). Unlike regular Java elements, a working copy never shows up among the children of its parent element (which may or may not exist).- Returns:
trueif this element exists in the Java model, andfalseif this element does not exist
-
getAncestor
IJavaElement getAncestor(int ancestorType)
Returns this Java element or the first ancestor of this element that has the given type. Returnsnullif no such element can be found. This is a handle-only method.- Parameters:
ancestorType- the given type- Returns:
- this Java element or the first ancestor of this element that has the given type, or
nullif no such element can be found - Since:
- 2.0
-
getAttachedJavadoc
String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException
Returns the Javadoc as HTML source if this element has attached Javadoc,
nullotherwise.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 the Javadoc doesn't exist for this element,
nullis returned.The HTML is extracted from the attached Javadoc and provided as is. No transformation or validation is done.
- Parameters:
monitor- the given progress monitor, can benull- Returns:
- the extracted javadoc from the attached javadoc,
nullif 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,...)
- Since:
- 3.2
- See Also:
IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME
-
getCorrespondingResource
IResource getCorrespondingResource() throws JavaModelException
Returns the resource that corresponds directly to this element, ornullif there is no resource that corresponds to this element.For example, the corresponding resource for an
ICompilationUnitis its underlyingIFile. The corresponding resource for anIPackageFragmentthat is not contained in an archive is its underlyingIFolder. AnIPackageFragmentcontained in an archive has no corresponding resource. Similarly, there are no corresponding resources forIMethods,IFields, etc.- Returns:
- the corresponding resource, or
nullif none - Throws:
JavaModelException- if this element does not exist or if an exception occurs while accessing its corresponding resource
-
getElementName
String getElementName()
Returns the name of this element. This is a handle-only method.- Returns:
- the element name
-
getElementType
int getElementType()
Returns this element's kind encoded as an integer. This is a handle-only method.- Returns:
- the kind of element; one of the constants declared in
IJavaElement - See Also:
IJavaElement
-
getHandleIdentifier
String getHandleIdentifier()
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 theJavaCore.create(String)method.- Returns:
- the string handle identifier
- See Also:
JavaCore.create(java.lang.String)
-
getJavaModel
IJavaModel getJavaModel()
Returns the Java model. This is a handle-only method.- Returns:
- the Java model
-
getJavaProject
IJavaProject getJavaProject()
Returns the Java project this element is contained in, ornullif this element is not contained in any Java project (for instance, theIJavaModelis not contained in any Java project). This is a handle-only method.- Returns:
- the containing Java project, or
nullif this element is not contained in a Java project
-
getOpenable
IOpenable getOpenable()
Returns the first openable parent. If this element is openable, the element itself is returned. Returnsnullif this element doesn't have an openable parent. This is a handle-only method.- Returns:
- the first openable parent or
nullif this element doesn't have an openable parent. - Since:
- 2.0
-
getParent
IJavaElement getParent()
Returns the element directly containing this element, ornullif this element has no parent. This is a handle-only method.- Returns:
- the parent element, or
nullif this element has no parent
-
getPath
IPath getPath()
Returns the path to the innermost resource enclosing this element. If this element is not included in an external library, the path returned is the full, absolute path to the underlying resource, relative to the workbench. If this element is included in an external library, the path returned is the absolute path to the archive or to the folder in the file system. This is a handle-only method.- Returns:
- the path to the innermost resource enclosing this element
- Since:
- 2.0
-
getPrimaryElement
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. it is a binary member). The returned element may or may not exist.- Returns:
- the primary element this working copy element was created from, or this element.
- Since:
- 3.0
-
getResource
IResource getResource()
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,nullis returned. This is a handle-only method.- Returns:
- the innermost resource enclosing this element,
nullif this element is included in an external archive - Since:
- 2.0
-
getSchedulingRule
ISchedulingRule getSchedulingRule()
Returns the scheduling rule associated with this Java element. This is a handle-only method.- Returns:
- the scheduling rule associated with this Java element
- Since:
- 3.0
-
getUnderlyingResource
IResource getUnderlyingResource() throws JavaModelException
Returns the smallest underlying resource that contains this element, ornullif this element is not contained in a resource.- Returns:
- the underlying resource, or
nullif none - Throws:
JavaModelException- if this element does not exist or if an exception occurs while accessing its underlying resource
-
isReadOnly
boolean isReadOnly()
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.
- Returns:
trueif this element is read-only
-
isStructureKnown
boolean isStructureKnown() throws JavaModelExceptionReturns whether the structure of this element is known. For example, for a compilation unit that has syntax errors,falseis returned. If the structure of an element is unknown, navigations will return reasonable defaults. For example,getChildrenfor 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.
- Returns:
trueif 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
-
-