org.eclipse.jdt.internal.core
Class JavaModel

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
              extended by org.eclipse.jdt.internal.core.JavaModel
All Implemented Interfaces:
org.eclipse.core.runtime.IAdaptable, IBufferChangedListener, IJavaElement, IJavaModel, IOpenable, IParent

public class JavaModel
extends Openable
implements IJavaModel

Implementation of 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(IWorkspace) method.

See Also:
IJavaModel

Field Summary
static java.util.HashSet existingExternalConfirmedFiles
          A set of external files (existingExternalFiles) which have been confirmed as file (ie.
static java.util.HashSet existingExternalFiles
          A set of java.io.Files used as a cache of external jars that are known to be existing.
 
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
 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, org.eclipse.core.runtime.IProgressMonitor monitor)
          Copies the given elements to the specified container(s).
 void delete(IJavaElement[] elements, boolean force, org.eclipse.core.runtime.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.
static void flushExternalFileCache()
          Flushes the cache of external files known to be existing.
 int getElementType()
          Returns this element's kind encoded as an integer.
static java.lang.Object getExternalTarget(org.eclipse.core.runtime.IPath path, boolean checkResourceExistence)
           
static java.io.File getFile(java.lang.Object target)
          Helper method - returns the file item (ie.
 IJavaElement getHandleFromMemento(java.lang.String token, MementoTokenizer memento, WorkingCopyOwner owner)
           
 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.
 org.eclipse.core.runtime.IPath getPath()
          Returns the path to the innermost resource enclosing this element.
static java.lang.Object getTarget(org.eclipse.core.runtime.IPath path, boolean checkResourceExistence)
          Helper method - returns the targeted item (IResource if internal or java.io.File if external), or null if unbound 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(org.eclipse.core.runtime.IPath path)
           
static boolean isFile(java.lang.Object target)
          Helper method - returns whether an object is afile (ie.
 void move(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, java.lang.String[] renamings, boolean force, org.eclipse.core.runtime.IProgressMonitor monitor)
          Moves the given elements to the specified container(s).
 void refreshExternalArchives(IJavaElement[] elementsScope, org.eclipse.core.runtime.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, org.eclipse.core.runtime.IProgressMonitor monitor)
          Renames the given elements as specified.
 IResource resource(PackageFragmentRoot root)
           
 
Methods inherited from class org.eclipse.jdt.internal.core.Openable
bufferChanged, canBeRemovedFromCache, canBufferBeRemovedFromCache, exists, findRecommendedLineSeparator, getBuffer, getBufferFactory, getCorrespondingResource, getOpenable, getPackageFragmentRoot, getResource, hasUnsavedChanges, isConsistent, isOpen, isStructureKnown, makeConsistent, open, resource, save
 
Methods inherited from class org.eclipse.jdt.internal.core.JavaElement
close, findNode, getAncestor, getAttachedJavadoc, getChildren, getChildrenOfType, getClassFile, getCompilationUnit, getElementInfo, getElementInfo, getElementName, 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.IJavaElement
exists, getAncestor, getAttachedJavadoc, getCorrespondingResource, getElementName, getHandleIdentifier, getJavaModel, getJavaProject, getOpenable, getParent, getPrimaryElement, getResource, getSchedulingRule, isReadOnly, isStructureKnown
 
Methods inherited from interface org.eclipse.core.runtime.IAdaptable
getAdapter
 
Methods inherited from interface org.eclipse.jdt.core.IOpenable
close, findRecommendedLineSeparator, getBuffer, hasUnsavedChanges, isConsistent, isOpen, makeConsistent, open, save
 
Methods inherited from interface org.eclipse.jdt.core.IParent
getChildren, hasChildren
 

Field Detail

existingExternalFiles

public static java.util.HashSet existingExternalFiles
A set of java.io.Files used as a cache of external jars that are known to be existing. Note this cache is kept for the whole session.


existingExternalConfirmedFiles

public static java.util.HashSet existingExternalConfirmedFiles
A set of external files (existingExternalFiles) which have been confirmed as file (ie. which returns true to File.isFile(). Note this cache is kept for the whole session.

Method Detail

contains

public boolean contains(IResource resource)
Description copied from interface: IJavaModel
Returns whether this Java model contains an IJavaElement 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.

Specified by:
contains in interface IJavaModel
Parameters:
resource - the resource to check
Returns:
true if the resource is accessible through the Java model

copy

public void copy(IJavaElement[] elements,
                 IJavaElement[] containers,
                 IJavaElement[] siblings,
                 java.lang.String[] renamings,
                 boolean force,
                 org.eclipse.core.runtime.IProgressMonitor monitor)
          throws JavaModelException
Description copied from interface: IJavaModel
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 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.

Specified by:
copy in interface IJavaModel
Parameters:
elements - the elements to copy
containers - the container, or list of containers
siblings - the list of siblings element any of which may be null; or null
renamings - the list of new names any of which may be null; or null
force - 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 collision
monitor - 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 CoreException occurred 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 replace has been specified as false (NAME_COLLISION)
  • A container or element is read-only (READ_ONLY)
See Also:
IJavaModel

delete

public void delete(IJavaElement[] elements,
                   boolean force,
                   org.eclipse.core.runtime.IProgressMonitor monitor)
            throws JavaModelException
Description copied from interface: IJavaModel
Deletes the given elements, forcing the operation if necessary and specified.

Specified by:
delete in interface IJavaModel
Parameters:
elements - the elements to delete
force - a flag controlling whether underlying resources that are not in sync with the local file system will be tolerated
monitor - 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 CoreException occurred while updating an underlying resource
  • An element is read-only (READ_ONLY)
See Also:
IJavaModel

equals

public boolean equals(java.lang.Object o)
Description copied from class: JavaElement
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 JavaElement
See Also:
Object.equals(java.lang.Object)

getElementType

public int getElementType()
Description copied from interface: IJavaElement
Returns this element's kind encoded as an integer. This is a handle-only method.

Specified by:
getElementType in interface IJavaElement
Returns:
the kind of element; one of the constants declared in IJavaElement
See Also:
IJavaElement

flushExternalFileCache

public static void flushExternalFileCache()
Flushes the cache of external files known to be existing.


getHandleFromMemento

public IJavaElement getHandleFromMemento(java.lang.String token,
                                         MementoTokenizer memento,
                                         WorkingCopyOwner owner)
Specified by:
getHandleFromMemento in class JavaElement

getJavaProject

public IJavaProject getJavaProject(java.lang.String projectName)
Description copied from interface: IJavaModel
Returns the Java project with the given name. This is a handle-only method. The project may or may not exist.

Specified by:
getJavaProject in interface IJavaModel
Parameters:
projectName - the name of the Java project
Returns:
the Java project with the given name
See Also:
IJavaModel

getJavaProject

public 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.

Throws:
java.lang.IllegalArgumentException - if the given resource is not one of an IProject, IFolder, or IFile.

getJavaProjects

public IJavaProject[] getJavaProjects()
                               throws JavaModelException
Description copied from interface: IJavaModel
Returns the Java projects in this Java model, or an empty array if there are none.

Specified by:
getJavaProjects in interface IJavaModel
Returns:
the Java projects in this Java model, or an empty array if there are none
Throws:
JavaModelException - if this request fails.
See Also:
IJavaModel

getNonJavaResources

public java.lang.Object[] getNonJavaResources()
                                       throws JavaModelException
Description copied from interface: IJavaModel
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).

Specified by:
getNonJavaResources in interface IJavaModel
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
See Also:
IJavaModel

getPath

public org.eclipse.core.runtime.IPath getPath()
Description copied from interface: IJavaElement
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 in the file system. This is a handle-only method.

Specified by:
getPath in interface IJavaElement
Returns:
the path to the innermost resource enclosing this element

resource

public IResource resource(PackageFragmentRoot root)

getUnderlyingResource

public IResource getUnderlyingResource()
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
Overrides:
getUnderlyingResource in class Openable
Returns:
the underlying resource, or null if none
See Also:
IOpenable

getWorkspace

public IWorkspace getWorkspace()
Returns the workbench associated with this object.

Specified by:
getWorkspace in interface IJavaModel
Returns:
the workspace associated with this Java model

move

public void move(IJavaElement[] elements,
                 IJavaElement[] containers,
                 IJavaElement[] siblings,
                 java.lang.String[] renamings,
                 boolean force,
                 org.eclipse.core.runtime.IProgressMonitor monitor)
          throws JavaModelException
Description copied from interface: IJavaModel
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 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.

Specified by:
move in interface IJavaModel
Parameters:
elements - the elements to move
containers - the container, or list of containers
siblings - the list of siblings element any of which may be null; or null
renamings - the list of new names any of which may be null; or null
force - 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 collision
monitor - 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 CoreException occurred 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 replace has been specified as false (NAME_COLLISION)
  • A container or element is read-only (READ_ONLY)
See Also:
IJavaModel

refreshExternalArchives

public void refreshExternalArchives(IJavaElement[] elementsScope,
                                    org.eclipse.core.runtime.IProgressMonitor monitor)
                             throws JavaModelException
Description copied from interface: IJavaModel
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 (passing null along 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.

Specified by:
refreshExternalArchives in interface IJavaModel
Parameters:
elementsScope - - a collection of elements defining the scope of the refresh
monitor - - a progress monitor used to report progress
Throws:
JavaModelException - in one of the corresponding situation:
  • an exception occurs while accessing project resources
See Also:
IJavaModel.refreshExternalArchives(IJavaElement[], IProgressMonitor)

rename

public void rename(IJavaElement[] elements,
                   IJavaElement[] destinations,
                   java.lang.String[] renamings,
                   boolean force,
                   org.eclipse.core.runtime.IProgressMonitor monitor)
            throws JavaModelException
Description copied from interface: IJavaModel
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.

Specified by:
rename in interface IJavaModel
Parameters:
elements - the elements to rename
destinations - the container, or list of containers
renamings - the list of new names
force - 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 collision
monitor - 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 CoreException occurred while updating an underlying resource
  • A new name is invalid (INVALID_NAME)
  • A child already exists with the same name and replace has been specified as false (NAME_COLLISION)
  • An element is read-only (READ_ONLY)
See Also:
IJavaModel

getTarget

public static java.lang.Object getTarget(org.eclipse.core.runtime.IPath path,
                                         boolean checkResourceExistence)
Helper method - returns the targeted item (IResource if internal or java.io.File if external), or null if unbound Internal items must be referred to using container relative paths.


getWorkspaceTarget

public static IResource getWorkspaceTarget(org.eclipse.core.runtime.IPath path)

getExternalTarget

public static java.lang.Object getExternalTarget(org.eclipse.core.runtime.IPath path,
                                                 boolean checkResourceExistence)

isFile

public static boolean isFile(java.lang.Object target)
Helper method - returns whether an object is afile (ie. which returns true to File.isFile().


getFile

public static java.io.File getFile(java.lang.Object target)
Helper method - returns the file item (ie. which returns true to File.isFile(), or null if unbound