KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > codemanipulation > AddCustomConstructorOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.codemanipulation;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.text.edits.TextEdit;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26 import org.eclipse.core.runtime.jobs.ISchedulingRule;
27
28 import org.eclipse.core.filebuffers.ITextFileBuffer;
29
30 import org.eclipse.core.resources.IWorkspaceRunnable;
31 import org.eclipse.core.resources.ResourcesPlugin;
32
33 import org.eclipse.jface.text.Document;
34 import org.eclipse.jface.text.IDocument;
35
36 import org.eclipse.ltk.core.refactoring.Change;
37
38 import org.eclipse.jdt.core.Flags;
39 import org.eclipse.jdt.core.ICompilationUnit;
40 import org.eclipse.jdt.core.IField;
41 import org.eclipse.jdt.core.IJavaElement;
42 import org.eclipse.jdt.core.IMember;
43 import org.eclipse.jdt.core.IMethod;
44 import org.eclipse.jdt.core.ISourceReference;
45 import org.eclipse.jdt.core.IType;
46 import org.eclipse.jdt.core.dom.ASTNode;
47 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
48 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
49 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
50 import org.eclipse.jdt.core.dom.CompilationUnit;
51 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
52 import org.eclipse.jdt.core.dom.IMethodBinding;
53 import org.eclipse.jdt.core.dom.ITypeBinding;
54 import org.eclipse.jdt.core.dom.IVariableBinding;
55 import org.eclipse.jdt.core.dom.MethodDeclaration;
56 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
57
58 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
59 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
60 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
61 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
62 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringFileBuffers;
63 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
64
65 import org.eclipse.jdt.internal.ui.JavaPlugin;
66
67 /**
68  * Workspace runnable to add custom constructors initializing fields.
69  *
70  * @since 3.1
71  */

72 public final class AddCustomConstructorOperation implements IWorkspaceRunnable {
73
74     /** Should the resulting edit be applied? */
75     private boolean fApply= true;
76
77     /** The super constructor method binding */
78     private final IMethodBinding fBinding;
79
80     /** The variable bindings to implement */
81     private final IVariableBinding[] fBindings;
82
83     /** The variable binding keys for which a constructor was generated */
84     private final List JavaDoc fCreated= new ArrayList JavaDoc();
85
86     /** The resulting text edit */
87     private TextEdit fEdit= null;
88
89     /** The insertion point, or <code>null</code> */
90     private final IJavaElement fInsert;
91
92     /** Should the call to the super constructor be omitted? */
93     private boolean fOmitSuper= false;
94
95     /** Should the compilation unit content be saved? */
96     private final boolean fSave;
97
98     /** The code generation settings to use */
99     private final CodeGenerationSettings fSettings;
100
101     /** The type declaration to add the constructors to */
102     private final IType fType;
103
104     /** The compilation unit ast node */
105     private final CompilationUnit fUnit;
106
107     /** The visibility flags of the new constructor */
108     private int fVisibility= 0;
109
110     /**
111      * Creates a new add custom constructor operation.
112      *
113      * @param type the type to add the methods to
114      * @param insert the insertion point, or <code>null</code>
115      * @param unit the compilation unit ast node
116      * @param bindings the variable bindings to use in the constructor
117      * @param binding the method binding of the super constructor
118      * @param settings the code generation settings to use
119      * @param apply <code>true</code> if the resulting edit should be applied, <code>false</code> otherwise
120      * @param save <code>true</code> if the changed compilation unit should be saved, <code>false</code> otherwise
121      */

122     public AddCustomConstructorOperation(final IType type, final IJavaElement insert, final CompilationUnit unit, final IVariableBinding[] bindings, final IMethodBinding binding, final CodeGenerationSettings settings, final boolean apply, final boolean save) {
123         Assert.isNotNull(type);
124         Assert.isNotNull(unit);
125         Assert.isNotNull(bindings);
126         Assert.isNotNull(settings);
127         fType= type;
128         fInsert= insert;
129         fUnit= unit;
130         fBindings= bindings;
131         fBinding= binding;
132         fSettings= settings;
133         fSave= save;
134         fApply= apply;
135     }
136
137     /**
138      * Returns the resulting text edit.
139      *
140      * @return the resulting text edit
141      */

142     public final TextEdit getResultingEdit() {
143         return fEdit;
144     }
145
146     /**
147      * Returns the scheduling rule for this operation.
148      *
149      * @return the scheduling rule
150      */

151     public final ISchedulingRule getSchedulingRule() {
152         return ResourcesPlugin.getWorkspace().getRoot();
153     }
154
155     /**
156      * Returns the visibility modifier of the generated constructors.
157      *
158      * @return the visibility modifier
159      */

160     public final int getVisibility() {
161         return fVisibility;
162     }
163
164     /**
165      * Should the call to the super constructor be omitted?
166      *
167      * @return <code>true</code> to omit the call, <code>false</code> otherwise
168      */

169     public final boolean isOmitSuper() {
170         return fOmitSuper;
171     }
172
173     /*
174      * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
175      */

176     public final void run(IProgressMonitor monitor) throws CoreException {
177         if (monitor == null)
178             monitor= new NullProgressMonitor();
179         try {
180             monitor.beginTask("", 1); //$NON-NLS-1$
181
monitor.setTaskName(CodeGenerationMessages.AddCustomConstructorOperation_description);
182             fCreated.clear();
183             final ICompilationUnit unit= fType.getCompilationUnit();
184             final CompilationUnitRewrite rewrite= new CompilationUnitRewrite(unit, fUnit);
185             ITypeBinding binding= null;
186             ListRewrite rewriter= null;
187             if (fType.isAnonymous()) {
188                 final IJavaElement parent= fType.getParent();
189                 if (parent instanceof IField && Flags.isEnum(((IMember) parent).getFlags())) {
190                     final EnumConstantDeclaration constant= (EnumConstantDeclaration) NodeFinder.perform(rewrite.getRoot(), ((ISourceReference) parent).getSourceRange());
191                     if (constant != null) {
192                         final AnonymousClassDeclaration declaration= constant.getAnonymousClassDeclaration();
193                         if (declaration != null) {
194                             binding= declaration.resolveBinding();
195                             if (binding != null)
196                                 rewriter= rewrite.getASTRewrite().getListRewrite(declaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
197                         }
198                     }
199                 } else {
200                     final ClassInstanceCreation creation= (ClassInstanceCreation) ASTNodes.getParent(NodeFinder.perform(rewrite.getRoot(), fType.getNameRange()), ClassInstanceCreation.class);
201                     if (creation != null) {
202                         binding= creation.resolveTypeBinding();
203                         final AnonymousClassDeclaration declaration= creation.getAnonymousClassDeclaration();
204                         if (declaration != null)
205                             rewriter= rewrite.getASTRewrite().getListRewrite(declaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
206                     }
207                 }
208             } else {
209                 final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(NodeFinder.perform(rewrite.getRoot(), fType.getNameRange()), AbstractTypeDeclaration.class);
210                 if (declaration != null) {
211                     binding= declaration.resolveBinding();
212                     rewriter= rewrite.getASTRewrite().getListRewrite(declaration, declaration.getBodyDeclarationsProperty());
213                 }
214             }
215             if (binding != null && rewriter != null) {
216                 ITextFileBuffer buffer= null;
217                 IDocument document= null;
218                 try {
219                     if (!JavaModelUtil.isPrimary(unit))
220                         document= new Document(unit.getBuffer().getContents());
221                     else {
222                         buffer= RefactoringFileBuffers.acquire(unit);
223                         document= buffer.getDocument();
224                     }
225                     ASTNode insertion= null;
226                     if (fInsert instanceof IMethod)
227                         insertion= ASTNodes.getParent(NodeFinder.perform(rewrite.getRoot(), ((IMethod) fInsert).getNameRange()), MethodDeclaration.class);
228                     MethodDeclaration stub= StubUtility2.createConstructorStub(rewrite.getCu(), rewrite.getASTRewrite(), rewrite.getImportRewrite(), binding, rewrite.getAST(), fOmitSuper ? null : fBinding, fBindings, fVisibility, fSettings);
229                     if (stub != null) {
230                         fCreated.addAll(Arrays.asList(fBindings));
231                         if (insertion != null)
232                             rewriter.insertBefore(stub, insertion, null);
233                         else
234                             rewriter.insertLast(stub, null);
235                     }
236                     final Change result= rewrite.createChange();
237                     if (result instanceof CompilationUnitChange) {
238                         final CompilationUnitChange change= (CompilationUnitChange) result;
239                         final TextEdit edit= change.getEdit();
240                         if (edit != null) {
241                             try {
242                                 fEdit= edit;
243                                 if (fApply)
244                                     edit.apply(document, TextEdit.UPDATE_REGIONS);
245                                 if (fSave) {
246                                     if (buffer != null)
247                                         buffer.commit(new SubProgressMonitor(monitor, 1), true);
248                                     else
249                                         unit.getBuffer().setContents(document.get());
250                                 }
251                             } catch (Exception JavaDoc exception) {
252                                 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, exception.getLocalizedMessage(), exception));
253                             }
254                         }
255                     }
256                 } finally {
257                     if (buffer != null)
258                         RefactoringFileBuffers.release(unit);
259                 }
260             }
261         } finally {
262             monitor.done();
263         }
264     }
265
266     /**
267      * Determines whether the call to the super constructor should be omitted.
268      *
269      * @param omit <code>true</code> to omit the call, <code>false</code> otherwise
270      */

271     public final void setOmitSuper(final boolean omit) {
272         fOmitSuper= omit;
273     }
274
275     /**
276      * Sets the visibility modifier of the generated constructors.
277      *
278      * @param visibility the visibility modifier
279      */

280     public final void setVisibility(final int visibility) {
281         fVisibility= visibility;
282     }
283 }
284
Popular Tags