public class JavaModel extends Openable implements IJavaModel
IJavaModel. The Java Model maintains a cache of active
IJavaProjects in a workspace. A Java Model is specific to a workspace.
To retrieve a workspace's model, use the
#getJavaModel() method.IJavaModelJEM_ANNOTATION, JEM_CLASSFILE, JEM_COMPILATIONUNIT, JEM_COUNT, JEM_DELIMITER_ESCAPE, JEM_ESCAPE, JEM_FIELD, JEM_IMPORTDECLARATION, JEM_INITIALIZER, JEM_JAVAPROJECT, JEM_LAMBDA_EXPRESSION, JEM_LAMBDA_METHOD, JEM_LOCALVARIABLE, JEM_METHOD, JEM_PACKAGEDECLARATION, JEM_PACKAGEFRAGMENT, JEM_PACKAGEFRAGMENTROOT, JEM_STRING, JEM_TYPE, JEM_TYPE_PARAMETER, NO_ELEMENTS, NO_INFO, parentANNOTATION, 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| Modifier | Constructor and Description |
|---|---|
protected |
JavaModel()
Constructs a new Java Model on the given workspace.
|
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
buildStructure(OpenableElementInfo info,
IProgressMonitor pm,
java.util.Map newElements,
IResource underlyingResource)
Builds this element's structure and properties in the given
info object, based on this element's current contents (reuse buffer
contents if this element has an open buffer, or resource contents
if this element does not have an open buffer).
|
boolean |
contains(IResource resource)
Returns whether this Java model contains an
IJavaElement whose
resource is the given resource or a non-Java resource which is the given resource. |
void |
copy(IJavaElement[] elements,
IJavaElement[] containers,
IJavaElement[] siblings,
java.lang.String[] renamings,
boolean force,
IProgressMonitor monitor)
Copies the given elements to the specified container(s).
|
protected java.lang.Object |
createElementInfo()
Returns a new element info for this element.
|
void |
delete(IJavaElement[] elements,
boolean force,
IProgressMonitor monitor)
Deletes the given elements, forcing the operation if necessary and specified.
|
boolean |
equals(java.lang.Object o)
Returns true if this handle represents the same Java element
as the given handle.
|
int |
getElementType()
Returns this element's kind encoded as an integer.
|
static java.lang.Object |
getExternalTarget(IPath path,
boolean checkResourceExistence)
Helper method - returns either the linked
IFolder or the File corresponding
to the provided IPath. |
static java.io.File |
getFile(java.lang.Object target)
Helper method - returns the
File item if target is a file (i.e., the target
returns true to File.isFile(). |
IJavaElement |
getHandleFromMemento(java.lang.String token,
MementoTokenizer memento,
WorkingCopyOwner owner) |
protected void |
getHandleMemento(java.lang.StringBuffer buff) |
protected char |
getHandleMementoDelimiter()
Returns the
char that marks the start of this handles
contribution to a memento. |
IJavaProject |
getJavaProject(IResource resource)
Returns the active Java project associated with the specified
resource, or
null if no Java project yet exists
for the resource. |
IJavaProject |
getJavaProject(java.lang.String projectName)
Returns the Java project with the given name.
|
IJavaProject[] |
getJavaProjects()
Returns the Java projects in this Java model, or an empty array if there
are none.
|
java.lang.Object[] |
getNonJavaResources()
Returns an array of non-Java resources (that is, non-Java projects) in
the workspace.
|
IPath |
getPath()
Returns the path to the innermost resource enclosing this element.
|
static java.lang.Object |
getTarget(IPath path,
boolean checkResourceExistence)
Helper method - for the provided
IPath, returns:
If the path corresponds to an internal file or folder, the IResource for that resource
If the path corresponds to an external folder linked through ExternalFoldersManager,
the IFolder for that folder
If the path corresponds to an external library archive, the File for that archive
Can return null if checkResourceExistence is true
and the entity referred to by the path does not exist on the file system
Internal items must be referred to using container-relative paths. |
IResource |
getUnderlyingResource()
Returns the smallest underlying resource that contains
this element, or
null if this element is not contained
in a resource. |
IWorkspace |
getWorkspace()
Returns the workbench associated with this object.
|
static IResource |
getWorkspaceTarget(IPath path)
Helper method - returns the
IResource corresponding to the provided IPath,
or null if no such resource exists. |
static boolean |
isFile(java.lang.Object target)
Helper method - returns whether an object is a file (i.e., it returns
true
to File.isFile(). |
void |
move(IJavaElement[] elements,
IJavaElement[] containers,
IJavaElement[] siblings,
java.lang.String[] renamings,
boolean force,
IProgressMonitor monitor)
Moves the given elements to the specified container(s).
|
void |
refreshExternalArchives(IJavaElement[] elementsScope,
IProgressMonitor monitor)
Triggers an update of the JavaModel with respect to the referenced external archives.
|
void |
rename(IJavaElement[] elements,
IJavaElement[] destinations,
java.lang.String[] renamings,
boolean force,
IProgressMonitor monitor)
Renames the given elements as specified.
|
IResource |
resource(PackageFragmentRoot root) |
protected void |
runOperation(MultiOperation op,
IJavaElement[] elements,
IJavaElement[] siblings,
java.lang.String[] renamings,
IProgressMonitor monitor)
Configures and runs the
MultiOperation. |
protected void |
toStringInfo(int tab,
java.lang.StringBuffer buffer,
java.lang.Object info,
boolean showResolvedInfo)
Debugging purposes
|
protected IStatus |
validateExistence(IResource underlyingResource) |
bufferChanged, canBeRemovedFromCache, canBufferBeRemovedFromCache, closeBuffer, closing, codeComplete, codeSelect, exists, findRecommendedLineSeparator, generateInfos, getBuffer, getBufferFactory, getBufferManager, getCorrespondingResource, getOpenable, getPackageFragmentRoot, getResource, hasBuffer, hasUnsavedChanges, isConsistent, isOpen, isSourceElement, isStructureKnown, makeConsistent, open, openAncestors, openBuffer, resource, resourceExists, saveappendEscapedDelimiter, close, escapeMementoName, findNode, getAncestor, getAttachedJavadoc, getChildren, getChildrenOfType, getClassFile, getCompilationUnit, getElementInfo, getElementInfo, getElementName, getHandleFromMemento, getHandleIdentifier, getHandleMemento, getJavadocBaseLocation, getJavaModel, getJavaProject, getLibraryJavadocLocation, getOpenableParent, getParent, getPrimaryElement, getPrimaryElement, getSchedulingRule, getSourceElementAt, getSourceMapper, getURLContents, hasChildren, hashCode, isAncestorOf, isReadOnly, newDoesNotExistStatus, newJavaModelException, newNotPresentException, openWhenClosed, readableName, resolved, tabString, toDebugString, toString, toString, toStringAncestors, toStringChildren, toStringInfo, toStringName, toStringWithAncestors, toStringWithAncestors, unresolved, validateAndCacheclone, finalize, getClass, notify, notifyAll, wait, wait, waitexists, getAncestor, getAttachedJavadoc, getCorrespondingResource, getElementName, getHandleIdentifier, getJavaModel, getJavaProject, getOpenable, getParent, getPrimaryElement, getResource, getSchedulingRule, isReadOnly, isStructureKnownclose, findRecommendedLineSeparator, getBuffer, hasUnsavedChanges, isConsistent, isOpen, makeConsistent, open, savegetChildren, hasChildrenprotected JavaModel()
throws java.lang.Error
java.lang.Error - if called more than onceprotected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, java.util.Map newElements, IResource underlyingResource)
OpenablebuildStructure in class Openablepublic boolean contains(IResource resource)
IJavaModelIJavaElement whose
resource is the given resource or a non-Java resource which is the given resource.
Note: no existency check is performed on the argument resource. If it is not accessible
(see IResource.isAccessible()) yet but would be located in Java model
range, then it will return true.
If the resource is accessible, it can be reached by navigating the Java model down using the
getChildren() and/or getNonJavaResources() methods.
contains in interface IJavaModelresource - the resource to checkpublic void copy(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, java.lang.String[] renamings, boolean force, IProgressMonitor monitor) throws JavaModelException
IJavaModel
Optionally, each copy can positioned before a sibling
element. If null is specified for a given sibling, the copy
is inserted as the last child of its associated container.
Optionally, each copy can be renamed. If
null is specified for the new name, the copy
is not renamed.
Optionally, any existing child in the destination container with
the same name can be replaced by specifying true for
force. Otherwise an exception is thrown in the event that a name
collision occurs.
copy in interface IJavaModelelements - the elements to copycontainers - the container, or list of containerssiblings - the list of siblings element any of which may be
null; or nullrenamings - the list of new names any of which may be
null; or nullforce - true if any existing child in a target container
with the target name should be replaced, and false to throw an
exception in the event of a name collisionmonitor - a progress monitorJavaModelException - if an element could not be copied. Reasons include:
CoreException occurred while updating an underlying resourceINVALID_DESTINATION)INVALID_SIBLING)INVALID_NAME)replace has been specified as false (NAME_COLLISION)READ_ONLY) IJavaModelprotected java.lang.Object createElementInfo()
createElementInfo in class Openablepublic void delete(IJavaElement[] elements, boolean force, IProgressMonitor monitor) throws JavaModelException
IJavaModeldelete in interface IJavaModelelements - the elements to deleteforce - a flag controlling whether underlying resources that are not
in sync with the local file system will be toleratedmonitor - a progress monitorJavaModelException - if an element could not be deleted. Reasons include:
CoreException occurred while updating an underlying resourceREAD_ONLY) IJavaModelpublic boolean equals(java.lang.Object o)
JavaElementIf a subclass has other requirements for equality, this method must be overridden.
equals in class JavaElementObject.equals(java.lang.Object)public int getElementType()
IJavaElementgetElementType in interface IJavaElementIJavaElementIJavaElementpublic IJavaElement getHandleFromMemento(java.lang.String token, MementoTokenizer memento, WorkingCopyOwner owner)
getHandleFromMemento in class JavaElementprotected void getHandleMemento(java.lang.StringBuffer buff)
getHandleMemento in class JavaElementJavaElement.getHandleMemento(StringBuffer)protected char getHandleMementoDelimiter()
char that marks the start of this handles
contribution to a memento.getHandleMementoDelimiter in class JavaElementpublic IJavaProject getJavaProject(java.lang.String projectName)
IJavaModelIPath#isValidSegment(String).
This is a handle-only method.
The project may or may not exist.getJavaProject in interface IJavaModelprojectName - the name of the Java projectIJavaModelpublic IJavaProject getJavaProject(IResource resource)
null if no Java project yet exists
for the resource.getJavaProject in interface IJavaModelresource - the name of the Java projectjava.lang.IllegalArgumentException - if the given resource
is not one of an IProject, IFolder, or IFile.public IJavaProject[] getJavaProjects() throws JavaModelException
IJavaModelgetJavaProjects in interface IJavaModelJavaModelException - if this request fails.IJavaModelpublic java.lang.Object[] getNonJavaResources()
throws JavaModelException
IJavaModelNon-Java projects include all projects that are closed (even if they have the Java nature).
getNonJavaResources in interface IJavaModelIProjects) contained
in the workspace.JavaModelException - if this element does not exist or if an
exception occurs while accessing its corresponding resourceIJavaModelpublic IPath getPath()
IJavaElementgetPath in interface IJavaElementpublic IResource resource(PackageFragmentRoot root)
public IResource getUnderlyingResource()
IJavaElementnull if this element is not contained
in a resource.getUnderlyingResource in interface IJavaElementgetUnderlyingResource in class Openablenull if noneIOpenablepublic IWorkspace getWorkspace()
getWorkspace in interface IJavaModelpublic void move(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, java.lang.String[] renamings, boolean force, IProgressMonitor monitor) throws JavaModelException
IJavaModel
Optionally, each element can positioned before a sibling
element. If null is specified for sibling, the element
is inserted as the last child of its associated container.
Optionally, each element can be renamed. If
null is specified for the new name, the element
is not renamed.
Optionally, any existing child in the destination container with
the same name can be replaced by specifying true for
force. Otherwise an exception is thrown in the event that a name
collision occurs.
move in interface IJavaModelelements - the elements to movecontainers - the container, or list of containerssiblings - the list of siblings element any of which may be
null; or nullrenamings - the list of new names any of which may be
null; or nullforce - true if any existing child in a target container
with the target name should be replaced, and false to throw an
exception in the event of a name collisionmonitor - a progress monitorJavaModelException - if an element could not be moved. Reasons include:
CoreException occurred while updating an underlying resourceINVALID_DESTINATION)INVALID_SIBLING)INVALID_NAME)replace has been specified as false (NAME_COLLISION)READ_ONLY) IJavaModelpublic void refreshExternalArchives(IJavaElement[] elementsScope, IProgressMonitor monitor) throws JavaModelException
IJavaModelnull along is equivalent to refreshing the entire mode).
The elements can be:
In case an archive is used by multiple projects, the delta issued will account for all of them. This means that even if a project was not part of the elements scope, it may still be notified of changes if it is referencing a library comprised in the scope.
Since 3.7, a project refresh automatically triggers a refresh of external archives. Hence, this method doesn't need to be explicitly called after a project refresh.
refreshExternalArchives in interface IJavaModelelementsScope - - a collection of elements defining the scope of the refreshmonitor - - a progress monitor used to report progressJavaModelException - in one of the corresponding situation:
IJavaModel.refreshExternalArchives(IJavaElement[], IProgressMonitor)public void rename(IJavaElement[] elements, IJavaElement[] destinations, java.lang.String[] renamings, boolean force, IProgressMonitor monitor) throws JavaModelException
IJavaModelrename in interface IJavaModelelements - the elements to renamedestinations - the container, or list of containersrenamings - the list of new namesforce - true if an existing child in a target container
with the target name should be replaced, and false to throw an
exception in the event of a name collisionmonitor - a progress monitorJavaModelException - if an element could not be renamed. Reasons include:
CoreException occurred while updating an underlying resource
INVALID_NAME)
replace has been specified as false (NAME_COLLISION)
READ_ONLY)
IJavaModelprotected void runOperation(MultiOperation op, IJavaElement[] elements, IJavaElement[] siblings, java.lang.String[] renamings, IProgressMonitor monitor) throws JavaModelException
MultiOperation.JavaModelExceptionprotected void toStringInfo(int tab,
java.lang.StringBuffer buffer,
java.lang.Object info,
boolean showResolvedInfo)
JavaElementtoStringInfo in class JavaElementshowResolvedInfo - TODOpublic static java.lang.Object getTarget(IPath path,
boolean checkResourceExistence)
IPath, returns:
IResource for that resource
ExternalFoldersManager,
the IFolder for that folder
File for that archive
null if checkResourceExistence is true
and the entity referred to by the path does not exist on the file system
public static IResource getWorkspaceTarget(IPath path)
IResource corresponding to the provided IPath,
or null if no such resource exists.public static java.lang.Object getExternalTarget(IPath path,
boolean checkResourceExistence)
IFolder or the File corresponding
to the provided IPath. If checkResourceExistence is false,
then the IFolder or File object is always returned, otherwise null is returned
if it does not exist on the file system.public static boolean isFile(java.lang.Object target)
true
to File.isFile().public static java.io.File getFile(java.lang.Object target)
File item if target is a file (i.e., the target
returns true to File.isFile(). Otherwise returns null.protected IStatus validateExistence(IResource underlyingResource)
validateExistence in class Openable