1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core;
13 import java.util.HashMap;
15 import net.sourceforge.phpdt.internal.core.ImportContainer;
17 import org.eclipse.core.runtime.IProgressMonitor;
22 * Represents an entire Java compilation unit (<code>.java</code> source file).
23 * Compilation unit elements need to be opened before they can be navigated or manipulated.
24 * The children are of type <code>IPackageDeclaration</code>,
25 * <code>IImportContainer</code>, and <code>IType</code>,
26 * and appear in the order in which they are declared in the source.
27 * If a <code>.java</code> file cannot be parsed, its structure remains unknown.
28 * Use <code>IJavaElement.isStructureKnown</code> to determine whether this is
31 * This interface is not intended to be implemented by clients.
34 public interface ICompilationUnit extends IJavaElement, ISourceReference, IParent, IOpenable, IWorkingCopy, ISourceManipulation {
35 //extends IJavaElement, ISourceReference, IParent, IOpenable, IWorkingCopy, ISourceManipulation, ICodeAssist {
38 * Constant indicating that a reconcile operation should not return an AST.
41 public static final int NO_AST = 0;
44 * Changes this compilation unit handle into a working copy. A new <code>IBuffer</code> is
45 * created using this compilation unit handle's owner. Uses the primary owner is none was
46 * specified when this compilation unit handle was created.
48 * When switching to working copy mode, problems are reported to given
49 * <code>IProblemRequestor</code>.
52 * Once in working copy mode, changes to this compilation unit or its children are done in memory.
53 * Only the new buffer is affected. Using <code>commitWorkingCopy(boolean, IProgressMonitor)</code>
54 * will bring the underlying resource in sync with this compilation unit.
57 * If this compilation unit was already in working copy mode, an internal counter is incremented and no
58 * other action is taken on this compilation unit. To bring this compilation unit back into the original mode
59 * (where it reflects the underlying resource), <code>discardWorkingCopy</code> must be call as many
60 * times as <code>becomeWorkingCopy</code>.
63 * @param problemRequestor a requestor which will get notified of problems detected during
64 * reconciling as they are discovered. The requestor can be set to <code>null</code> indicating
65 * that the client is not interested in problems.
66 * @param monitor a progress monitor used to report progress while opening this compilation unit
67 * or <code>null</code> if no progress should be reported
68 * @throws JavaModelException if this compilation unit could not become a working copy.
69 * @see #discardWorkingCopy()
72 void becomeWorkingCopy(IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException;
74 * Commits the contents of this working copy to its underlying resource.
76 * <p>It is possible that the contents of the original resource have changed
77 * since this working copy was created, in which case there is an update conflict.
78 * The value of the <code>force</code> parameter effects the resolution of
79 * such a conflict:<ul>
80 * <li> <code>true</code> - in this case the contents of this working copy are applied to
81 * the underlying resource even though this working copy was created before
82 * a subsequent change in the resource</li>
83 * <li> <code>false</code> - in this case a <code>JavaModelException</code> is thrown</li>
86 * Since 2.1, a working copy can be created on a not-yet existing compilation
87 * unit. In particular, such a working copy can then be committed in order to create
88 * the corresponding compilation unit.
90 * @param force a flag to handle the cases when the contents of the original resource have changed
91 * since this working copy was created
92 * @param monitor the given progress monitor
93 * @throws JavaModelException if this working copy could not commit. Reasons include:
95 * <li> A <code>CoreException</code> occurred while updating an underlying resource
96 * <li> This element is not a working copy (INVALID_ELEMENT_TYPES)
97 * <li> A update conflict (described above) (UPDATE_CONFLICT)
101 void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws JavaModelException;
103 * Changes this compilation unit in working copy mode back to its original mode.
105 * This has no effect if this compilation unit was not in working copy mode.
108 * If <code>becomeWorkingCopy</code> was called several times on this
109 * compilation unit, <code>discardWorkingCopy</code> must be called as
110 * many times before it switches back to the original mode.
113 * @throws JavaModelException if this working copy could not return in its original mode.
114 * @see #becomeWorkingCopy(IProblemRequestor, IProgressMonitor)
117 void discardWorkingCopy() throws JavaModelException;
119 * Creates and returns an import declaration in this compilation unit
120 * with the given name.
122 * Optionally, the new element can be positioned before the specified
123 * sibling. If no sibling is specified, the element will be inserted
124 * as the last import declaration in this compilation unit.
126 * If the compilation unit already includes the specified import declaration,
127 * the import is not generated (it does not generate duplicates).
128 * Note that it is valid to specify both a single-type import and an on-demand import
129 * for the same package, for example <code>"java.io.File"</code> and
130 * <code>"java.io.*"</code>, in which case both are preserved since the semantics
131 * of this are not the same as just importing <code>"java.io.*"</code>.
132 * Importing <code>"java.lang.*"</code>, or the package in which the compilation unit
133 * is defined, are not treated as special cases. If they are specified, they are
134 * included in the result.
136 * @param name the name of the import declaration to add as defined by JLS2 7.5. (For example: <code>"java.io.File"</code> or
137 * <code>"java.awt.*"</code>)
138 * @param sibling the existing element which the import declaration will be inserted immediately before (if
139 * <code> null </code>, then this import will be inserted as the last import declaration.
140 * @param monitor the progress monitor to notify
141 * @return the newly inserted import declaration (or the previously existing one in case attempting to create a duplicate)
143 * @exception JavaModelException if the element could not be created. Reasons include:
145 * <li> This Java element does not exist or the specified sibling does not exist (ELEMENT_DOES_NOT_EXIST)</li>
146 * <li> A <code>CoreException</code> occurred while updating an underlying resource
147 * <li> The specified sibling is not a child of this compilation unit (INVALID_SIBLING)
148 * <li> The name is not a valid import name (INVALID_NAME)
151 //IImportDeclaration createImport(String name, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException;
153 * Creates and returns a package declaration in this compilation unit
154 * with the given package name.
156 * <p>If the compilation unit already includes the specified package declaration,
157 * it is not generated (it does not generate duplicates).
159 * @param name the name of the package declaration to add as defined by JLS2 7.4. (For example, <code>"java.lang"</code>)
160 * @param monitor the progress monitor to notify
161 * @return the newly inserted package declaration (or the previously existing one in case attempting to create a duplicate)
163 * @exception JavaModelException if the element could not be created. Reasons include:
165 * <li>This Java element does not exist (ELEMENT_DOES_NOT_EXIST)</li>
166 * <li> A <code>CoreException</code> occurred while updating an underlying resource
167 * <li> The name is not a valid package name (INVALID_NAME)
170 // IPackageDeclaration createPackageDeclaration(String name, IProgressMonitor monitor) throws JavaModelException;
172 * Creates and returns a type in this compilation unit with the
173 * given contents. If this compilation unit does not exist, one
174 * will be created with an appropriate package declaration.
176 * Optionally, the new type can be positioned before the specified
177 * sibling. If <code>sibling</code> is <code>null</code>, the type will be appended
178 * to the end of this compilation unit.
180 * <p>It is possible that a type with the same name already exists in this compilation unit.
181 * The value of the <code>force</code> parameter effects the resolution of
182 * such a conflict:<ul>
183 * <li> <code>true</code> - in this case the type is created with the new contents</li>
184 * <li> <code>false</code> - in this case a <code>JavaModelException</code> is thrown</li>
187 * @param contents the source contents of the type declaration to add.
188 * @param sibling the existing element which the type will be inserted immediately before (if
189 * <code> null </code>, then this type will be inserted as the last type declaration.
190 * @param force a <code> boolean </code> flag indicating how to deal with duplicates
191 * @param monitor the progress monitor to notify
192 * @return the newly inserted type
194 * @exception JavaModelException if the element could not be created. Reasons include:
196 * <li>The specified sibling element does not exist (ELEMENT_DOES_NOT_EXIST)</li>
197 * <li> A <code>CoreException</code> occurred while updating an underlying resource
198 * <li> The specified sibling is not a child of this compilation unit (INVALID_SIBLING)
199 * <li> The contents could not be recognized as a type declaration (INVALID_CONTENTS)
200 * <li> There was a naming collision with an existing type (NAME_COLLISION)
203 //IType createType(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException;
205 * Returns all types declared in this compilation unit in the order
206 * in which they appear in the source.
207 * This includes all top-level types and nested member types.
208 * It does NOT include local types (types defined in methods).
210 * @return the array of top-level and member types defined in a compilation unit, in declaration order.
211 * @exception JavaModelException if this element does not exist or if an
212 * exception occurs while accessing its corresponding resource
214 IType[] getAllTypes() throws JavaModelException;
216 * Returns the smallest element within this compilation unit that
217 * includes the given source position (that is, a method, field, etc.), or
218 * <code>null</code> if there is no element other than the compilation
219 * unit itself at the given position, or if the given position is not
220 * within the source range of this compilation unit.
222 * @param position a source position inside the compilation unit
223 * @return the innermost Java element enclosing a given source position or <code>null</code>
224 * if none (excluding the compilation unit).
225 * @exception JavaModelException if the compilation unit does not exist or if an
226 * exception occurs while accessing its corresponding resource
228 IJavaElement getElementAt(int position) throws JavaModelException;
230 * Returns the first import declaration in this compilation unit with the given name.
231 * This is a handle-only method. The import declaration may or may not exist. This
232 * is a convenience method - imports can also be accessed from a compilation unit's
235 * @param name the name of the import to find as defined by JLS2 7.5. (For example: <code>"java.io.File"</code>
236 * or <code>"java.awt.*"</code>)
237 * @return a handle onto the corresponding import declaration. The import declaration may or may not exist.
239 IImportDeclaration getImport(String name) ;
241 * Returns the import declarations in this compilation unit
242 * in the order in which they appear in the source. This is
243 * a convenience method - import declarations can also be
244 * accessed from a compilation unit's import container.
246 * @return the import declarations in this compilation unit
247 * @throws JavaModelException if this element does not exist or if an
248 * exception occurs while accessing its corresponding resource
250 IImportDeclaration[] getImports() throws JavaModelException;
252 * Returns the import container for this compilation unit.
253 * This is a handle-only method. The import container may or
254 * may not exist. The import container can used to access the
256 * @return a handle onto the corresponding import container. The
257 * import contain may or may not exist.
259 IImportContainer getImportContainer();
261 * Returns the import declarations in this compilation unit
262 * in the order in which they appear in the source. This is
263 * a convenience method - import declarations can also be
264 * accessed from a compilation unit's import container.
266 * @exception JavaModelException if this element does not exist or if an
267 * exception occurs while accessing its corresponding resource
269 //IImportDeclaration[] getImports() throws JavaModelException;
271 * Returns the first package declaration in this compilation unit with the given package name
272 * (there normally is at most one package declaration).
273 * This is a handle-only method. The package declaration may or may not exist.
275 * @param name the name of the package declaration as defined by JLS2 7.4. (For example, <code>"java.lang"</code>)
277 IPackageDeclaration getPackageDeclaration(String name);
279 * Returns the package declarations in this compilation unit
280 * in the order in which they appear in the source.
281 * There normally is at most one package declaration.
283 * @return an array of package declaration (normally of size one)
285 * @exception JavaModelException if this element does not exist or if an
286 * exception occurs while accessing its corresponding resource
288 IPackageDeclaration[] getPackageDeclarations() throws JavaModelException;
290 * Returns the primary compilation unit (whose owner is the primary owner)
291 * this working copy was created from, or this compilation unit if this a primary
294 * Note that the returned primary compilation unit can be in working copy mode.
297 * @return the primary compilation unit this working copy was created from,
298 * or this compilation unit if it is primary
301 ICompilationUnit getPrimary();
303 * Returns the top-level type declared in this compilation unit with the given simple type name.
304 * The type name has to be a valid compilation unit name.
305 * This is a handle-only method. The type may or may not exist.
307 * @param name the simple name of the requested type in the compilation unit
308 * @return a handle onto the corresponding type. The type may or may not exist.
309 * @see JavaConventions#validateCompilationUnitName(String name)
311 IType getType(String name);
313 * Returns the top-level types declared in this compilation unit
314 * in the order in which they appear in the source.
316 * @exception JavaModelException if this element does not exist or if an
317 * exception occurs while accessing its corresponding resource
319 IType[] getTypes() throws JavaModelException;