|
Eclipse JDT Release 3.1 |
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Represents an entire Java compilation unit (.java
source file).
Compilation unit elements need to be opened before they can be navigated or manipulated.
The children are of type IPackageDeclaration
,
IImportContainer
, and IType
,
and appear in the order in which they are declared in the source.
If a .java
file cannot be parsed, its structure remains unknown.
Use IJavaElement.isStructureKnown()
to determine whether this is
the case.
This interface is not intended to be implemented by clients.
Field Summary | |
---|---|
static int |
NO_AST
Constant indicating that a reconcile operation should not return an AST. |
Fields inherited from interface org.eclipse.jdt.core.IJavaElement |
---|
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 |
becomeWorkingCopy(IProblemRequestor problemRequestor,
IProgressMonitor monitor)
Changes this compilation unit handle into a working copy. |
void |
commitWorkingCopy(boolean force,
IProgressMonitor monitor)
Commits the contents of this working copy to its underlying resource. |
IImportDeclaration |
createImport(String name,
IJavaElement sibling,
int flags,
IProgressMonitor monitor)
Creates and returns an import declaration in this compilation unit with the given name. |
IImportDeclaration |
createImport(String name,
IJavaElement sibling,
IProgressMonitor monitor)
Creates and returns an non-static import declaration in this compilation unit with the given name. |
IPackageDeclaration |
createPackageDeclaration(String name,
IProgressMonitor monitor)
Creates and returns a package declaration in this compilation unit with the given package name. |
IType |
createType(String contents,
IJavaElement sibling,
boolean force,
IProgressMonitor monitor)
Creates and returns a type in this compilation unit with the given contents. |
void |
discardWorkingCopy()
Changes this compilation unit in working copy mode back to its original mode. |
IJavaElement[] |
findElements(IJavaElement element)
Finds the elements in this compilation unit that correspond to the given element. |
IType |
findPrimaryType()
Finds the primary type of this compilation unit (that is, the type with the same name as the compilation unit), or null if no such a type exists. |
ICompilationUnit |
findWorkingCopy(WorkingCopyOwner owner)
Finds the working copy for this compilation unit, given a WorkingCopyOwner .
|
IType[] |
getAllTypes()
Returns all types declared in this compilation unit in the order in which they appear in the source. |
IJavaElement |
getElementAt(int position)
Returns the smallest element within this compilation unit that includes the given source position (that is, a method, field, etc.), or null if there is no element other than the compilation
unit itself at the given position, or if the given position is not
within the source range of this compilation unit. |
IImportDeclaration |
getImport(String name)
Returns the first import declaration in this compilation unit with the given name. |
IImportContainer |
getImportContainer()
Returns the import container for this compilation unit. |
IImportDeclaration[] |
getImports()
Returns the import declarations in this compilation unit in the order in which they appear in the source. |
WorkingCopyOwner |
getOwner()
Returns the working copy owner of this working copy. |
IPackageDeclaration |
getPackageDeclaration(String name)
Returns the first package declaration in this compilation unit with the given package name (there normally is at most one package declaration). |
IPackageDeclaration[] |
getPackageDeclarations()
Returns the package declarations in this compilation unit in the order in which they appear in the source. |
ICompilationUnit |
getPrimary()
Returns the primary compilation unit (whose owner is the primary owner) this working copy was created from, or this compilation unit if this a primary compilation unit. |
IType |
getType(String name)
Returns the top-level type declared in this compilation unit with the given simple type name. |
IType[] |
getTypes()
Returns the top-level types declared in this compilation unit in the order in which they appear in the source. |
ICompilationUnit |
getWorkingCopy(IProgressMonitor monitor)
Returns a new working copy of this compilation unit if it is a primary compilation unit, or this compilation unit if it is already a non-primary working copy. |
ICompilationUnit |
getWorkingCopy(WorkingCopyOwner owner,
IProblemRequestor problemRequestor,
IProgressMonitor monitor)
Returns a shared working copy on this compilation unit using the given working copy owner to create the buffer, or this compilation unit if it is already a non-primary working copy. |
boolean |
hasResourceChanged()
Returns whether the resource of this working copy has changed since the inception of this working copy. |
boolean |
isWorkingCopy()
Returns whether this element is a working copy. |
CompilationUnit |
reconcile(int astLevel,
boolean forceProblemDetection,
WorkingCopyOwner owner,
IProgressMonitor monitor)
Reconciles the contents of this working copy, sends out a Java delta notification indicating the nature of the change of the working copy since the last time it was either reconciled or made consistent ( IOpenable.makeConsistent(IProgressMonitor) ), and returns a
compilation unit AST if requested.
|
void |
restore()
Restores the contents of this working copy to the current contents of this working copy's original element. |
Methods inherited from interface org.eclipse.jdt.core.IJavaElement |
---|
exists, getAncestor, getCorrespondingResource, getElementName, getElementType, getHandleIdentifier, getJavaModel, getJavaProject, getOpenable, getParent, getPath, getPrimaryElement, getResource, getSchedulingRule, getUnderlyingResource, isReadOnly, isStructureKnown |
Methods inherited from interface org.eclipse.core.runtime.IAdaptable |
---|
getAdapter |
Methods inherited from interface org.eclipse.jdt.core.ISourceReference |
---|
exists, getSource, getSourceRange |
Methods inherited from interface org.eclipse.jdt.core.IParent |
---|
getChildren, hasChildren |
Methods inherited from interface org.eclipse.jdt.core.IOpenable |
---|
close, getBuffer, hasUnsavedChanges, isConsistent, isOpen, makeConsistent, open, save |
Methods inherited from interface org.eclipse.jdt.core.IWorkingCopy |
---|
commit, destroy, findSharedWorkingCopy, getOriginal, getOriginalElement, getSharedWorkingCopy, getWorkingCopy, getWorkingCopy, isBasedOn, reconcile, reconcile |
Methods inherited from interface org.eclipse.jdt.core.ISourceManipulation |
---|
copy, delete, move, rename |
Methods inherited from interface org.eclipse.jdt.core.ICodeAssist |
---|
codeComplete, codeComplete, codeComplete, codeComplete, codeComplete, codeSelect, codeSelect |
Field Detail |
public static final int NO_AST
Method Detail |
public void becomeWorkingCopy(IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException
IBuffer
is
created using this compilation unit handle's owner. Uses the primary owner is none was
specified when this compilation unit handle was created.
When switching to working copy mode, problems are reported to given
IProblemRequestor
. Note that once in working copy mode, the given
IProblemRequestor
is ignored. Only the original IProblemRequestor
is used to report subsequent problems.
Once in working copy mode, changes to this compilation unit or its children are done in memory.
Only the new buffer is affected. Using commitWorkingCopy(boolean, IProgressMonitor)
will bring the underlying resource in sync with this compilation unit.
If this compilation unit was already in working copy mode, an internal counter is incremented and no
other action is taken on this compilation unit. To bring this compilation unit back into the original mode
(where it reflects the underlying resource), discardWorkingCopy()
must be call as many
times as becomeWorkingCopy(IProblemRequestor, IProgressMonitor)
.
problemRequestor
- a requestor which will get notified of problems detected during
reconciling as they are discovered. The requestor can be set to null
indicating
that the client is not interested in problems.monitor
- a progress monitor used to report progress while opening this compilation unit
or null
if no progress should be reported
JavaModelException
- if this compilation unit could not become a working copy.discardWorkingCopy()
public void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws JavaModelException
It is possible that the contents of the original resource have changed
since this working copy was created, in which case there is an update conflict.
The value of the force
parameter effects the resolution of
such a conflict:
true
- in this case the contents of this working copy are applied to
the underlying resource even though this working copy was created before
a subsequent change in the resourcefalse
- in this case a JavaModelException
is thrownSince 2.1, a working copy can be created on a not-yet existing compilation unit. In particular, such a working copy can then be committed in order to create the corresponding compilation unit.
force
- a flag to handle the cases when the contents of the original resource have changed
since this working copy was createdmonitor
- the given progress monitor
JavaModelException
- if this working copy could not commit. Reasons include:
CoreException
occurred while updating an underlying resource
public IImportDeclaration createImport(String name, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException
createImport(name, Flags.AccDefault, sibling, monitor)
.
name
- the name of the import declaration to add as defined by JLS2 7.5. (For example: "java.io.File"
or
"java.awt.*"
)sibling
- the existing element which the import declaration will be inserted immediately before (if
null
, then this import will be inserted as the last import declaration.monitor
- the progress monitor to notify
JavaModelException
- if the element could not be created. Reasons include:
CoreException
occurred while updating an underlying resource
createImport(String, IJavaElement, int, IProgressMonitor)
public IImportDeclaration createImport(String name, IJavaElement sibling, int flags, IProgressMonitor monitor) throws JavaModelException
Optionally, the new element can be positioned before the specified sibling. If no sibling is specified, the element will be inserted as the last import declaration in this compilation unit.
If the compilation unit already includes the specified import declaration,
the import is not generated (it does not generate duplicates).
Note that it is valid to specify both a single-type import and an on-demand import
for the same package, for example "java.io.File"
and
"java.io.*"
, in which case both are preserved since the semantics
of this are not the same as just importing "java.io.*"
.
Importing "java.lang.*"
, or the package in which the compilation unit
is defined, are not treated as special cases. If they are specified, they are
included in the result.
Note: This API element is only needed for dealing with Java code that uses new language features of J2SE 5.0.
name
- the name of the import declaration to add as defined by JLS2 7.5. (For example: "java.io.File"
or
"java.awt.*"
)sibling
- the existing element which the import declaration will be inserted immediately before (if
null
, then this import will be inserted as the last import declaration.flags
- Flags.AccStatic
for static imports, or
Flags.AccDefault
for regular imports; other modifier flags
are ignoredmonitor
- the progress monitor to notify
JavaModelException
- if the element could not be created. Reasons include:
CoreException
occurred while updating an underlying resource
Flags
public IPackageDeclaration createPackageDeclaration(String name, IProgressMonitor monitor) throws JavaModelException
If the compilation unit already includes the specified package declaration, it is not generated (it does not generate duplicates).
name
- the name of the package declaration to add as defined by JLS2 7.4. (For example, "java.lang"
)monitor
- the progress monitor to notify
JavaModelException
- if the element could not be created. Reasons include:
CoreException
occurred while updating an underlying resource
public IType createType(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException
Optionally, the new type can be positioned before the specified
sibling. If sibling
is null
, the type will be appended
to the end of this compilation unit.
It is possible that a type with the same name already exists in this compilation unit.
The value of the force
parameter effects the resolution of
such a conflict:
true
- in this case the type is created with the new contentsfalse
- in this case a JavaModelException
is thrown
contents
- the source contents of the type declaration to add.sibling
- the existing element which the type will be inserted immediately before (if
null
, then this type will be inserted as the last type declaration.force
- a boolean
flag indicating how to deal with duplicatesmonitor
- the progress monitor to notify
JavaModelException
- if the element could not be created. Reasons include:
CoreException
occurred while updating an underlying resource
public void discardWorkingCopy() throws JavaModelException
This has no effect if this compilation unit was not in working copy mode.
If becomeWorkingCopy(org.eclipse.jdt.core.IProblemRequestor, org.eclipse.core.runtime.IProgressMonitor)
was called several times on this
compilation unit, discardWorkingCopy()
must be called as
many times before it switches back to the original mode.
JavaModelException
- if this working copy could not return in its original mode.becomeWorkingCopy(IProblemRequestor, IProgressMonitor)
public IJavaElement[] findElements(IJavaElement element)
null
if no such java elements can be found
or if the given element is not included in a compilation unit.
findElements
in interface IWorkingCopy
element
- the given element
public IType findPrimaryType()
null
if no such a type exists.
findPrimaryType
in interface IWorkingCopy
null
if no such a type existspublic ICompilationUnit findWorkingCopy(WorkingCopyOwner owner)
WorkingCopyOwner
.
If no working copy has been created for this compilation unit associated with this
working copy owner, returns null
.
Users of this method must not destroy the resulting working copy.
owner
- the given WorkingCopyOwner
null
if noneWorkingCopyOwner
public IType[] getAllTypes() throws JavaModelException
JavaModelException
- if this element does not exist or if an
exception occurs while accessing its corresponding resourcepublic IJavaElement getElementAt(int position) throws JavaModelException
null
if there is no element other than the compilation
unit itself at the given position, or if the given position is not
within the source range of this compilation unit.
position
- a source position inside the compilation unit
null
if none (excluding the compilation unit).
JavaModelException
- if the compilation unit does not exist or if an
exception occurs while accessing its corresponding resourcepublic IImportDeclaration getImport(String name)
name
- the name of the import to find as defined by JLS2 7.5. (For example: "java.io.File"
or "java.awt.*"
)
public IImportContainer getImportContainer()
public IImportDeclaration[] getImports() throws JavaModelException
JavaModelException
- if this element does not exist or if an
exception occurs while accessing its corresponding resourcepublic ICompilationUnit getPrimary()
Note that the returned primary compilation unit can be in working copy mode.
public WorkingCopyOwner getOwner()
null
public IPackageDeclaration getPackageDeclaration(String name)
name
- the name of the package declaration as defined by JLS2 7.4. (For example, "java.lang"
)
public IPackageDeclaration[] getPackageDeclarations() throws JavaModelException
JavaModelException
- if this element does not exist or if an
exception occurs while accessing its corresponding resourcepublic IType getType(String name)
name
- the simple name of the requested type in the compilation unit
JavaConventions.validateCompilationUnitName(String name)
public IType[] getTypes() throws JavaModelException
JavaModelException
- if this element does not exist or if an
exception occurs while accessing its corresponding resourcepublic ICompilationUnit getWorkingCopy(IProgressMonitor monitor) throws JavaModelException
Note: if intending to share a working copy amongst several clients, then
getWorkingCopy(WorkingCopyOwner, IProblemRequestor, IProgressMonitor)
should be used instead.
When the working copy instance is created, an ADDED IJavaElementDelta is reported on this working copy.
Once done with the working copy, users of this method must discard it using
discardWorkingCopy()
.
Since 2.1, a working copy can be created on a not-yet existing compilation unit. In particular, such a working copy can then be committed in order to create the corresponding compilation unit.
monitor
- a progress monitor used to report progress while opening this compilation unit
or null
if no progress should be reported
JavaModelException
- if the contents of this element can
not be determined.public ICompilationUnit getWorkingCopy(WorkingCopyOwner owner, IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException
Object.equals(java.lang.Object)
).
The life time of a shared working copy is as follows:
getWorkingCopy(WorkingCopyOwner, IProblemRequestor, IProgressMonitor)
creates a new working copy for this elementdiscardWorkingCopy()
decrements the internal counter.Note that the working copy owner will be used for the life time of this working copy, that is if the working copy is closed then reopened, this owner will be used. The buffer will be automatically initialized with the original's compilation unit content upon creation.
When the shared working copy instance is created, an ADDED IJavaElementDelta is reported on this working copy.
Since 2.1, a working copy can be created on a not-yet existing compilation unit. In particular, such a working copy can then be committed in order to create the corresponding compilation unit.
owner
- the working copy owner that creates a buffer that is used to get the content
of the working copyproblemRequestor
- a requestor which will get notified of problems detected during
reconciling as they are discovered. The requestor can be set to null
indicating
that the client is not interested in problems.monitor
- a progress monitor used to report progress while opening this compilation unit
or null
if no progress should be reported
JavaModelException
- if the contents of this element can
not be determined.public boolean hasResourceChanged()
false
if this compilation unit is not in working copy mode.
public boolean isWorkingCopy()
isWorkingCopy
in interface IWorkingCopy
public CompilationUnit reconcile(int astLevel, boolean forceProblemDetection, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException
IOpenable.makeConsistent(IProgressMonitor)
), and returns a
compilation unit AST if requested.
It performs the reconciliation by locally caching the contents of the working copy, updating the contents, then creating a delta over the cached contents and the new contents, and finally firing this delta.
The boolean argument allows to force problem detection even if the working copy is already consistent.
This functionality allows to specify a working copy owner which is used
during problem detection. All references contained in the working copy are
resolved against other units; for which corresponding owned working copies
are going to take precedence over their original compilation units. If
null
is passed in, then the primary working copy owner is used.
Compilation problems found in the new contents are notified through the
IProblemRequestor
interface which was passed at
creation, and no longer as transient markers.
Note: Since 3.0, added/removed/changed inner types generate change deltas.
If requested, a DOM AST representing the compilation unit is returned.
Its bindings are computed only if the problem requestor is active, or if the
problem detection is forced. This method returns null
if the
creation of the DOM AST was not requested, or if the requested level of AST
API is not supported, or if the working copy was already consistent.
astLevel
- either NO_AST
if no AST is wanted,
or the AST API level of the AST if one is wantedforceProblemDetection
- boolean indicating whether problem should be
recomputed even if the source hasn't changedowner
- the owner of working copies that take precedence over the
original compilation units, or null
if the primary working
copy owner should be usedmonitor
- a progress monitor
null
if not requested,
or if the requested level of AST API is not supported,
or if the working copy was consistent
JavaModelException
- if the contents of the original element
cannot be accessed. Reasons include:
public void restore() throws JavaModelException
Note: This is the inverse of committing the content of the
working copy to the original element with commitWorkingCopy(boolean, IProgressMonitor)
.
restore
in interface IWorkingCopy
JavaModelException
- if the contents of the original element
cannot be accessed. Reasons include:
|
Eclipse JDT Release 3.1 |
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |