Interface IJavaModel
- All Superinterfaces:
org.eclipse.core.runtime.IAdaptable, IJavaElement, IOpenable, IParent
- All Known Implementing Classes:
JavaModel
IJavaProjects.
This interface provides methods for performing copy, move, rename, and delete operations on multiple Java elements.
An instance of one of these handles can be created via
JavaCore.create(workspace.getRoot()).
- See Also:
-
Field Summary
Fields inherited from interface IJavaElement
ANNOTATION, CLASS_FILE, COMPILATION_UNIT, FIELD, IMPORT_CONTAINER, IMPORT_DECLARATION, INITIALIZER, JAVA_MODEL, JAVA_MODULE, JAVA_PROJECT, LOCAL_VARIABLE, METHOD, PACKAGE_DECLARATION, PACKAGE_FRAGMENT, PACKAGE_FRAGMENT_ROOT, TYPE, TYPE_PARAMETERModifier and TypeFieldDescriptionstatic final intConstant representing an annotation.static final intConstant representing a class file.static final intConstant representing a Java compilation unit.static final intConstant representing a field.static final intConstant representing all import declarations within a compilation unit.static final intConstant representing an import declaration within a compilation unit.static final intConstant representing a stand-alone instance or class initializer.static final intConstant representing a Java model (workspace level object).static final intConstant represents a module descriptor.static final intConstant representing a Java project.static final intConstant representing a local variable declaration.static final intConstant representing a method or constructor.static final intConstant representing a package declaration within a compilation unit.static final intConstant representing a package fragment.static final intConstant representing a package fragment root.static final intConstant representing a type (a class or interface).static final intConstant representing a type parameter declaration. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(org.eclipse.core.resources.IResource resource) Returns whether this Java model contains anIJavaElementwhose resource is the given resource or a non-Java resource which is the given resource.voidcopy(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String[] renamings, boolean replace, org.eclipse.core.runtime.IProgressMonitor monitor) Copies the given elements to the specified container(s).voiddelete(IJavaElement[] elements, boolean force, org.eclipse.core.runtime.IProgressMonitor monitor) Deletes the given elements, forcing the operation if necessary and specified.getJavaProject(String name) Returns the Java project with the given name.Returns the Java projects in this Java model, or an empty array if there are none.Object[]Returns an array of non-Java resources (that is, non-Java projects) in the workspace.org.eclipse.core.resources.IWorkspaceReturns the workspace associated with this Java model.voidmove(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String[] renamings, boolean replace, org.eclipse.core.runtime.IProgressMonitor monitor) Moves the given elements to the specified container(s).voidrefreshExternalArchives(IJavaElement[] elementsScope, org.eclipse.core.runtime.IProgressMonitor monitor) Triggers an update of the JavaModel with respect to the referenced external archives.voidrename(IJavaElement[] elements, IJavaElement[] destinations, String[] names, boolean replace, org.eclipse.core.runtime.IProgressMonitor monitor) Renames the given elements as specified.Methods inherited from interface org.eclipse.core.runtime.IAdaptable
getAdapterMethods inherited from interface IJavaElement
exists, getAncestor, getAttachedJavadoc, getCorrespondingResource, getElementName, getElementType, getHandleIdentifier, getJavaModel, getJavaProject, getOpenable, getParent, getPath, getPrimaryElement, getResource, getSchedulingRule, getUnderlyingResource, isReadOnly, isStructureKnownModifier and TypeMethodDescriptionbooleanexists()Returns whether this Java element exists in the model.getAncestor(int ancestorType) Returns this Java element or the first ancestor of this element that has the given type.getAttachedJavadoc(org.eclipse.core.runtime.IProgressMonitor monitor) Returns the Javadoc as HTML source if this element has attached Javadoc,nullotherwise.org.eclipse.core.resources.IResourceReturns the resource that corresponds directly to this element, ornullif there is no resource that corresponds to this element.Returns the name of this element.intReturns this element's kind encoded as an integer.Returns a string representation of this element handle.Returns the Java model.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).Returns the first openable parent.Returns the element directly containing this element, ornullif this element has no parent.org.eclipse.core.runtime.IPathgetPath()Returns the path to the innermost resource enclosing this element.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.org.eclipse.core.resources.IResourceReturns the innermost resource enclosing this element.org.eclipse.core.runtime.jobs.ISchedulingRuleReturns the scheduling rule associated with this Java element.org.eclipse.core.resources.IResourceReturns the smallest underlying resource that contains this element, ornullif this element is not contained in a resource.booleanReturns whether this Java element is read-only.booleanReturns whether the structure of this element is known.Methods inherited from interface IOpenable
close, findRecommendedLineSeparator, getBuffer, hasUnsavedChanges, isConsistent, isOpen, makeConsistent, open, saveModifier and TypeMethodDescriptionvoidclose()Closes this element and its buffer (if any).Finds and returns the recommended line separator for this element.Returns the buffer opened for this element, ornullif this element does not have a buffer.booleanReturnstrueif 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 destroyedbooleanReturns whether the element is consistent with its underlying resource or buffer.booleanisOpen()Returns whether this openable is open.voidmakeConsistent(org.eclipse.core.runtime.IProgressMonitor progress) Makes this element consistent with its underlying resource or buffer by updating the element's structure and properties as necessary.voidopen(org.eclipse.core.runtime.IProgressMonitor progress) Opens this element and all parent elements that are not already open.voidsave(org.eclipse.core.runtime.IProgressMonitor progress, boolean force) Saves any changes in this element's buffer to its underlying resource via a workspace resource operation.Methods inherited from interface IParent
getChildren, hasChildrenModifier and TypeMethodDescriptionReturns the immediate children of this element.booleanReturns whether this element has one or more immediate children.
-
Method Details
-
contains
boolean contains(org.eclipse.core.resources.IResource resource) Returns whether this Java model contains anIJavaElementwhose 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 returntrue.If the resource is accessible, it can be reached by navigating the Java model down using the
getChildren()and/orgetNonJavaResources()methods.- Parameters:
resource- the resource to check- Returns:
- true if the resource is accessible through the Java model
- Since:
- 2.1
-
copy
void copy(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String[] renamings, boolean replace, org.eclipse.core.runtime.IProgressMonitor monitor) throws JavaModelException Copies the given elements to the specified container(s). If one container is specified, all elements are copied to that container. If more than one container is specified, the number of elements and containers must match, and each element is copied to its associated container.Optionally, each copy can positioned before a sibling element. If
nullis specified for a given sibling, the copy is inserted as the last child of its associated container.Optionally, each copy can be renamed. If
nullis 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
truefor force. Otherwise an exception is thrown in the event that a name collision occurs.- Parameters:
elements- the elements to copycontainers- the container, or list of containerssiblings- the list of siblings element any of which may benull; ornullrenamings- the list of new names any of which may benull; ornullreplace-trueif any existing child in a target container with the target name should be replaced, andfalseto throw an exception in the event of a name collisionmonitor- a progress monitor- Throws:
JavaModelException- if an element could not be copied. Reasons include:- There is no element to process (NO_ELEMENTS_TO_PROCESS). The given elements is null or empty
- A specified element, container, or sibling does not exist (ELEMENT_DOES_NOT_EXIST)
- A
CoreExceptionoccurred while updating an underlying resource - A container is of an incompatible type (
INVALID_DESTINATION) - A sibling is not a child of it associated container (
INVALID_SIBLING) - A new name is invalid (
INVALID_NAME) - A child in its associated container already exists with the same
name and
replacehas been specified asfalse(NAME_COLLISION) - A container or element is read-only (
READ_ONLY)
- See Also:
-
delete
void delete(IJavaElement[] elements, boolean force, org.eclipse.core.runtime.IProgressMonitor monitor) throws JavaModelException Deletes the given elements, forcing the operation if necessary and specified.- Parameters:
elements- 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 monitor- Throws:
JavaModelException- if an element could not be deleted. Reasons include:- There is no element to process (NO_ELEMENTS_TO_PROCESS). The given elements is null or empty
- A specified element does not exist (ELEMENT_DOES_NOT_EXIST)
- A
CoreExceptionoccurred while updating an underlying resource - An element is read-only (
READ_ONLY)
-
getJavaProject
Returns the Java project with the given name. The given name must be a valid path segment as defined byIPath.isValidSegment(String). This is a handle-only method. The project may or may not exist.- Parameters:
name- the name of the Java project- Returns:
- the Java project with the given name
-
getJavaProjects
Returns the Java projects in this Java model, or an empty array if there are none.- Returns:
- the Java projects in this Java model, or an empty array if there are none
- Throws:
JavaModelException- if this request fails.
-
getNonJavaResources
Returns an array of non-Java resources (that is, non-Java projects) in the workspace.Non-Java projects include all projects that are closed (even if they have the Java nature).
- Returns:
- an array of non-Java projects (
IProjects) contained in the workspace. - Throws:
JavaModelException- if this element does not exist or if an exception occurs while accessing its corresponding resource- Since:
- 2.1
-
getWorkspace
org.eclipse.core.resources.IWorkspace getWorkspace()Returns the workspace associated with this Java model.- Returns:
- the workspace associated with this Java model
-
move
void move(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String[] renamings, boolean replace, org.eclipse.core.runtime.IProgressMonitor monitor) throws JavaModelException Moves the given elements to the specified container(s). If one container is specified, all elements are moved to that container. If more than one container is specified, the number of elements and containers must match, and each element is moved to its associated container.Optionally, each element can positioned before a sibling element. If
nullis specified for sibling, the element is inserted as the last child of its associated container.Optionally, each element can be renamed. If
nullis 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
truefor force. Otherwise an exception is thrown in the event that a name collision occurs.- Parameters:
elements- the elements to movecontainers- the container, or list of containerssiblings- the list of siblings element any of which may benull; ornullrenamings- the list of new names any of which may benull; ornullreplace-trueif any existing child in a target container with the target name should be replaced, andfalseto throw an exception in the event of a name collisionmonitor- a progress monitor- Throws:
JavaModelException- if an element could not be moved. Reasons include:- There is no element to process (NO_ELEMENTS_TO_PROCESS). The given elements is null or empty
- A specified element, container, or sibling does not exist (ELEMENT_DOES_NOT_EXIST)
- A
CoreExceptionoccurred while updating an underlying resource - A container is of an incompatible type (
INVALID_DESTINATION) - A sibling is not a child of it associated container (
INVALID_SIBLING) - A new name is invalid (
INVALID_NAME) - A child in its associated container already exists with the same
name and
replacehas been specified asfalse(NAME_COLLISION) - A container or element is read-only (
READ_ONLY)
IllegalArgumentException- any element or container isnull- See Also:
-
refreshExternalArchives
void refreshExternalArchives(IJavaElement[] elementsScope, org.eclipse.core.runtime.IProgressMonitor monitor) throws JavaModelException Triggers an update of the JavaModel with respect to the referenced external archives. This operation will issue a JavaModel delta describing the discovered changes, in term of Java element package fragment roots added, removed or changed. Note that a collection of elements can be passed so as to narrow the set of archives to refresh (passingnullalong is equivalent to refreshing the entire mode). The elements can be:- package fragment roots corresponding to external archives
- Java projects, which referenced external archives will be refreshed
- Java model, all referenced external archives will be refreshed.
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.
- Parameters:
elementsScope- - a collection of elements defining the scope of the refreshmonitor- - a progress monitor used to report progress- Throws:
JavaModelException- in one of the corresponding situation:- an exception occurs while accessing project resources
- Since:
- 2.0
- See Also:
-
rename
void rename(IJavaElement[] elements, IJavaElement[] destinations, String[] names, boolean replace, org.eclipse.core.runtime.IProgressMonitor monitor) throws JavaModelException Renames the given elements as specified. If one container is specified, all elements are renamed within that container. If more than one container is specified, the number of elements and containers must match, and each element is renamed within its associated container.- Parameters:
elements- the elements to renamedestinations- the container, or list of containersnames- the list of new namesreplace-trueif an existing child in a target container with the target name should be replaced, andfalseto throw an exception in the event of a name collisionmonitor- a progress monitor- Throws:
JavaModelException- if an element could not be renamed. Reasons include:- There is no element to process (NO_ELEMENTS_TO_PROCESS). The given elements is null or empty
- A specified element does not exist (ELEMENT_DOES_NOT_EXIST)
- A
CoreExceptionoccurred while updating an underlying resource - A new name is invalid (
INVALID_NAME) - A child already exists with the same name and
replacehas been specified asfalse(NAME_COLLISION) - An element is read-only (
READ_ONLY)
-