KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
15
16 import org.eclipse.text.edits.MultiTextEdit;
17 import org.eclipse.text.edits.TextEdit;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.core.runtime.jobs.ISchedulingRule;
24
25 import org.eclipse.core.resources.IWorkspaceRunnable;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28 import org.eclipse.jdt.core.ICompilationUnit;
29 import org.eclipse.jdt.core.dom.AST;
30 import org.eclipse.jdt.core.dom.ASTNode;
31 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
32 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
33 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
34 import org.eclipse.jdt.core.dom.CompilationUnit;
35 import org.eclipse.jdt.core.dom.IMethodBinding;
36 import org.eclipse.jdt.core.dom.ITypeBinding;
37 import org.eclipse.jdt.core.dom.MethodDeclaration;
38 import org.eclipse.jdt.core.dom.Modifier;
39 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
40 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
41 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
42
43 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
44
45 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
46
47 /**
48  * Workspace runnable to add unimplemented constructors.
49  *
50  * @since 3.1
51  */

52 public final class AddUnimplementedConstructorsOperation implements IWorkspaceRunnable {
53
54     /** Should the resulting edit be applied? */
55     private final boolean fApply;
56
57     /** The qualified names of the generated imports */
58     private String JavaDoc[] fCreatedImports;
59
60     /** The method binding keys for which a constructor was generated */
61     private final List JavaDoc fCreatedMethods= new ArrayList JavaDoc();
62
63     /** Should the import edits be applied? */
64     private final boolean fImports;
65
66     /** The insertion point, or <code>-1</code> */
67     private final int fInsertPos;
68
69     /** The method bindings to implement */
70     private final IMethodBinding[] fConstructorsToImplement;
71
72     /** Should the call to the super constructor be omitted? */
73     private boolean fOmitSuper;
74
75     /** Should the compilation unit content be saved? */
76     private final boolean fSave;
77
78     /** Specified if comments should be created */
79     private boolean fCreateComments;
80
81     /** The type declaration to add the constructors to */
82     private final ITypeBinding fType;
83
84     /** The compilation unit AST node */
85     private final CompilationUnit fASTRoot;
86
87     /** The visibility flags of the new constructor */
88     private int fVisibility;
89
90     /**
91      * Creates a new add unimplemented constructors operation.
92      *
93      * @param astRoot the compilation unit AST node
94      * @param type the type to add the methods to
95      * @param constructorsToImplement the method binding keys to implement
96      * @param insertPos the insertion point, or <code>-1</code>
97      * @param imports <code>true</code> if the import edits should be applied, <code>false</code> otherwise
98      * @param apply <code>true</code> if the resulting edit should be applied, <code>false</code> otherwise
99      * @param save <code>true</code> if the changed compilation unit should be saved, <code>false</code> otherwise
100      */

101     public AddUnimplementedConstructorsOperation(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] constructorsToImplement, int insertPos, final boolean imports, final boolean apply, final boolean save) {
102         if (astRoot == null || !(astRoot.getJavaElement() instanceof ICompilationUnit)) {
103             throw new IllegalArgumentException JavaDoc("AST must not be null and has to be created from a ICompilationUnit"); //$NON-NLS-1$
104
}
105         if (type == null) {
106             throw new IllegalArgumentException JavaDoc("The type must not be null"); //$NON-NLS-1$
107
}
108         ASTNode node= astRoot.findDeclaringNode(type);
109         if (!(node instanceof AnonymousClassDeclaration || node instanceof AbstractTypeDeclaration)) {
110             throw new IllegalArgumentException JavaDoc("type has to map to a type declaration in the AST"); //$NON-NLS-1$
111
}
112         
113         fType= type;
114         fInsertPos= insertPos;
115         fASTRoot= astRoot;
116         fConstructorsToImplement= constructorsToImplement;
117         fSave= save;
118         fApply= apply;
119         fImports= imports;
120         
121         fCreateComments= StubUtility.doAddComments(astRoot.getJavaElement().getJavaProject());
122         fVisibility= Modifier.PUBLIC;
123         fOmitSuper= false;
124     }
125
126     /**
127      * Returns the method binding keys for which a constructor has been generated.
128      *
129      * @return the method binding keys
130      */

131     public String JavaDoc[] getCreatedConstructors() {
132         final String JavaDoc[] keys= new String JavaDoc[fCreatedMethods.size()];
133         fCreatedMethods.toArray(keys);
134         return keys;
135     }
136
137     /**
138      * Returns the qualified names of the generated imports.
139      *
140      * @return the generated imports
141      */

142     public String JavaDoc[] getCreatedImports() {
143         return fCreatedImports;
144     }
145
146     /**
147      * Returns the scheduling rule for this operation.
148      *
149      * @return the scheduling rule
150      */

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

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

169     public boolean isOmitSuper() {
170         return fOmitSuper;
171     }
172
173     /**
174      * Determines whether to create comments.
175      * @param comments <code>true</code> to create comments, <code>false</code> otherwise
176      */

177     public void setCreateComments(final boolean comments) {
178         fCreateComments= comments;
179     }
180
181     /*
182      * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
183      */

184     public void run(IProgressMonitor monitor) throws CoreException {
185         if (monitor == null)
186             monitor= new NullProgressMonitor();
187         try {
188             monitor.beginTask("", 2); //$NON-NLS-1$
189
monitor.setTaskName(CodeGenerationMessages.AddUnimplementedMethodsOperation_description);
190             fCreatedMethods.clear();
191             ICompilationUnit cu= (ICompilationUnit) fASTRoot.getJavaElement();
192             
193             AST ast= fASTRoot.getAST();
194             
195             ASTRewrite astRewrite= ASTRewrite.create(ast);
196             ImportRewrite importRewrite= StubUtility.createImportRewrite(fASTRoot, true);
197             
198             ITypeBinding currTypeBinding= fType;
199             ListRewrite memberRewriter= null;
200             
201             ASTNode node= fASTRoot.findDeclaringNode(currTypeBinding);
202             if (node instanceof AnonymousClassDeclaration) {
203                 memberRewriter= astRewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
204             } else if (node instanceof AbstractTypeDeclaration) {
205                 ChildListPropertyDescriptor property= ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty();
206                 memberRewriter= astRewrite.getListRewrite(node, property);
207             } else {
208                 throw new IllegalArgumentException JavaDoc();
209                 // not possible, we checked this in the constructor
210
}
211             
212             final CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(cu.getJavaProject());
213             settings.createComments= fCreateComments;
214
215             ASTNode insertion= getNodeToInsertBefore(memberRewriter);
216             
217             IMethodBinding[] toImplement= fConstructorsToImplement;
218             if (toImplement == null) {
219                 toImplement= StubUtility2.getVisibleConstructors(currTypeBinding, true, true);
220             }
221             
222             int deprecationCount= 0;
223             for (int i= 0; i < toImplement.length; i++) {
224                 if (toImplement[i].isDeprecated())
225                     deprecationCount++;
226             }
227             boolean createDeprecated= deprecationCount == toImplement.length;
228             for (int i= 0; i < toImplement.length; i++) {
229                 IMethodBinding curr= toImplement[i];
230                 if (!curr.isDeprecated() || createDeprecated) {
231                     MethodDeclaration stub= StubUtility2.createConstructorStub(cu, astRewrite, importRewrite, curr, currTypeBinding.getName(), fVisibility, fOmitSuper, true, settings);
232                     if (stub != null) {
233                         fCreatedMethods.add(curr.getKey());
234                         if (insertion != null)
235                             memberRewriter.insertBefore(stub, insertion, null);
236                         else
237                             memberRewriter.insertLast(stub, null);
238                     }
239                 }
240             }
241             MultiTextEdit edit= new MultiTextEdit();
242             
243             TextEdit importEdits= importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
244             fCreatedImports= importRewrite.getCreatedImports();
245             if (fImports) {
246                 edit.addChild(importEdits);
247             }
248             edit.addChild(astRewrite.rewriteAST());
249             
250             if (fApply) {
251                 JavaModelUtil.applyEdit(cu, edit, fSave, new SubProgressMonitor(monitor, 1));
252             }
253         } finally {
254             monitor.done();
255         }
256     }
257
258     /**
259      * Determines whether the super call should be omitted.
260      *
261      * @param omit <code>true</code> to omit the super call, <code>false</code> otherwise
262      */

263     public void setOmitSuper(final boolean omit) {
264         fOmitSuper= omit;
265     }
266
267     /**
268      * Determines the visibility of the constructors.
269      *
270      * @param visibility the visibility
271      */

272     public void setVisibility(final int visibility) {
273         fVisibility= visibility;
274     }
275     
276     private ASTNode getNodeToInsertBefore(ListRewrite rewriter) {
277         if (fInsertPos != -1) {
278             List JavaDoc members= rewriter.getOriginalList();
279             for (int i= 0; i < members.size(); i++) {
280                 ASTNode curr= (ASTNode) members.get(i);
281                 if (curr.getStartPosition() >= fInsertPos) {
282                     return curr;
283                 }
284             }
285         }
286         return null;
287     }
288 }
289
Popular Tags