dd57c8fb94461afdf0cbbbeba67a9d9744061a3a
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / ICompilationUnit.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core;
12
13 import java.util.HashMap;
14
15 import net.sourceforge.phpdt.internal.core.ImportContainer;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18
19
20
21 /**
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 
29  * the case.
30  * <p>
31  * This interface is not intended to be implemented by clients.
32  * </p>
33  */ 
34 public interface ICompilationUnit extends IJavaElement, ISourceReference, IParent, IOpenable, IWorkingCopy, ISourceManipulation {
35 //extends IJavaElement, ISourceReference, IParent, IOpenable, IWorkingCopy, ISourceManipulation, ICodeAssist {
36
37         /**
38          * Constant indicating that a reconcile operation should not return an AST.
39          * @since 3.0
40          */
41         public static final int NO_AST = 0;
42
43         /**
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.
47          * <p>
48          * When switching to working copy mode, problems are reported to given 
49          * <code>IProblemRequestor</code>.
50          * </p>
51          * <p>
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.
55          * </p>
56          * <p>
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>.
61          * </p>
62          * 
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()
70          * @since 3.0
71          */
72         void becomeWorkingCopy(IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException;
73         /**
74          * Commits the contents of this working copy to its underlying resource.
75          *
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>
84          * </ul>
85          * <p>
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.
89          * </p>
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:
94          * <ul>
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)
98          * </ul>
99          * @since 3.0
100          */
101         void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws JavaModelException;
102         /**
103          * Changes this compilation unit in working copy mode back to its original mode.
104          * <p>
105          * This has no effect if this compilation unit was not in working copy mode.
106          * </p>
107          * <p>
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.
111          * </p>
112          * 
113          * @throws JavaModelException if this working copy could not return in its original mode.
114          * @see #becomeWorkingCopy(IProblemRequestor, IProgressMonitor)
115          * @since 3.0
116          */
117         void discardWorkingCopy() throws JavaModelException;
118 /**
119  * Creates and returns an import declaration in this compilation unit
120  * with the given name.
121  * <p>
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.
125  * <p>
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.
135  *
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)
142  *
143  * @exception JavaModelException if the element could not be created. Reasons include:
144  * <ul>
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)
149  * </ul>
150  */
151 //IImportDeclaration createImport(String name, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException;
152 /**
153  * Creates and returns a package declaration in this compilation unit
154  * with the given package name.
155  *
156  * <p>If the compilation unit already includes the specified package declaration,
157  * it is not generated (it does not generate duplicates).
158  *
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)
162  *
163  * @exception JavaModelException if the element could not be created. Reasons include:
164  * <ul>
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)
168  * </ul>
169  */
170 // IPackageDeclaration createPackageDeclaration(String name, IProgressMonitor monitor) throws JavaModelException;   
171 /**
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.
175  * <p>
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.
179  *
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>
185  * </ul>
186  *
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
193  *
194  * @exception JavaModelException if the element could not be created. Reasons include:
195  * <ul>
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)
201  * </ul>
202  */
203 //IType createType(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException;
204 /**
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).
209  *
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
213  */
214 IType[] getAllTypes() throws JavaModelException;
215 /**
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.
221  *
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
227  */
228 IJavaElement getElementAt(int position) throws JavaModelException;
229 /**
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
233  * import container.
234  *
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.
238  */
239 IImportDeclaration getImport(String name) ;
240 /**
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.
245  *
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
249  */
250 IImportDeclaration[] getImports() throws JavaModelException;
251 /**
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 
255  * imports.
256  * @return a handle onto the corresponding import container. The 
257  *              import contain may or may not exist.
258  */
259 IImportContainer getImportContainer();
260 /**
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.
265  *
266  * @exception JavaModelException if this element does not exist or if an
267  *              exception occurs while accessing its corresponding resource
268  */
269 //IImportDeclaration[] getImports() throws JavaModelException;
270 /**
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.
274  *
275  * @param name the name of the package declaration as defined by JLS2 7.4. (For example, <code>"java.lang"</code>)
276  */
277 IPackageDeclaration getPackageDeclaration(String name);
278 /**
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.
282  *
283  * @return an array of package declaration (normally of size one)
284  *
285  * @exception JavaModelException if this element does not exist or if an
286  *              exception occurs while accessing its corresponding resource
287  */
288 IPackageDeclaration[] getPackageDeclarations() throws JavaModelException;
289 /**
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
292  * compilation unit.
293  * <p>
294  * Note that the returned primary compilation unit can be in working copy mode.
295  * </p>
296  * 
297  * @return the primary compilation unit this working copy was created from,
298  * or this compilation unit if it is primary
299  * @since 3.0
300  */
301 ICompilationUnit getPrimary();
302 /**
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.
306  *
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)
310  */
311 IType getType(String name); 
312 /**
313  * Returns the top-level types declared in this compilation unit
314  * in the order in which they appear in the source.
315  *
316  * @exception JavaModelException if this element does not exist or if an
317  *              exception occurs while accessing its corresponding resource
318  */
319 IType[] getTypes() throws JavaModelException;
320
321 }