]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddCustomConstructorOperation.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core extension / org / eclipse / jdt / internal / corext / codemanipulation / AddCustomConstructorOperation.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2010 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 org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.core.runtime.SubProgressMonitor;
18 import org.eclipse.core.runtime.jobs.ISchedulingRule;
19
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21 import org.eclipse.core.resources.ResourcesPlugin;
22
23 import org.eclipse.text.edits.MultiTextEdit;
24 import org.eclipse.text.edits.TextEdit;
25
26 import org.eclipse.jdt.core.ICompilationUnit;
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.dom.ASTNode;
29 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
30 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
31 import org.eclipse.jdt.core.dom.CompilationUnit;
32 import org.eclipse.jdt.core.dom.IMethodBinding;
33 import org.eclipse.jdt.core.dom.ITypeBinding;
34 import org.eclipse.jdt.core.dom.IVariableBinding;
35 import org.eclipse.jdt.core.dom.MethodDeclaration;
36 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
37 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
38 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
39 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
40
41 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
42
43 /**
44  * Workspace runnable to add custom constructors initializing fields.
45  *
46  * @since 3.1
47  */
48 public final class AddCustomConstructorOperation implements IWorkspaceRunnable {
49
50         /** Should the resulting edit be applied? */
51         private boolean fApply= true;
52
53         /** The super constructor method binding */
54         private final IMethodBinding fConstructorBinding;
55
56         /** The variable bindings to implement */
57         private final IVariableBinding[] fFieldBindings;
58
59         /** The resulting text edit */
60         private TextEdit fResultingEdit= null;
61
62         /** The insertion point, or <code>null</code> */
63         private final IJavaElement fInsert;
64
65         /** Should the call to the super constructor be omitted? */
66         private boolean fOmitSuper= false;
67
68         /** Should the compilation unit content be saved? */
69         private final boolean fSave;
70
71         /** The code generation settings to use */
72         private final CodeGenerationSettings fSettings;
73
74         /** The type declaration to add the constructors to */
75         private final ITypeBinding fParentType;
76
77         /** The compilation unit ast node */
78         private final CompilationUnit fASTRoot;
79
80         /** The visibility flags of the new constructor */
81         private int fVisibility= 0;
82
83         /**
84          * Creates a new add custom constructor operation.
85          *
86          * @param astRoot the compilation unit ast node
87          * @param parentType the type to add the methods to
88          *      @param variables the variable bindings to use in the constructor
89          * @param constructor the method binding of the super constructor
90          * @param insert the insertion point, or <code>null</code>
91
92
93          * @param settings the code generation settings to use
94          * @param apply <code>true</code> if the resulting edit should be applied, <code>false</code> otherwise
95          * @param save <code>true</code> if the changed compilation unit should be saved, <code>false</code> otherwise
96          */
97         public AddCustomConstructorOperation(CompilationUnit astRoot, ITypeBinding parentType, IVariableBinding[] variables, IMethodBinding constructor, IJavaElement insert, CodeGenerationSettings settings, boolean apply, boolean save) {
98                 Assert.isTrue(astRoot != null && astRoot.getTypeRoot() instanceof ICompilationUnit);
99                 Assert.isNotNull(parentType);
100                 Assert.isNotNull(variables);
101                 Assert.isNotNull(constructor);
102                 Assert.isNotNull(settings);
103                 fParentType= parentType;
104                 fInsert= insert;
105                 fASTRoot= astRoot;
106                 fFieldBindings= variables;
107                 fConstructorBinding= constructor;
108                 fSettings= settings;
109                 fSave= save;
110                 fApply= apply;
111         }
112
113         /**
114          * Returns the resulting text edit.
115          *
116          * @return the resulting text edit
117          */
118         public final TextEdit getResultingEdit() {
119                 return fResultingEdit;
120         }
121
122         /**
123          * Returns the scheduling rule for this operation.
124          *
125          * @return the scheduling rule
126          */
127         public final ISchedulingRule getSchedulingRule() {
128                 return ResourcesPlugin.getWorkspace().getRoot();
129         }
130
131         /**
132          * Returns the visibility modifier of the generated constructors.
133          *
134          * @return the visibility modifier
135          */
136         public final int getVisibility() {
137                 return fVisibility;
138         }
139
140         /**
141          * Should the call to the super constructor be omitted?
142          *
143          * @return <code>true</code> to omit the call, <code>false</code> otherwise
144          */
145         public final boolean isOmitSuper() {
146                 return fOmitSuper;
147         }
148
149         /*
150          * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
151          */
152         public final void run(IProgressMonitor monitor) throws CoreException {
153                 if (monitor == null)
154                         monitor= new NullProgressMonitor();
155                 try {
156                         monitor.beginTask("", 2); //$NON-NLS-1$
157                         monitor.setTaskName(CodeGenerationMessages.AddCustomConstructorOperation_description);
158
159                         ICompilationUnit cu= (ICompilationUnit) fASTRoot.getTypeRoot();
160
161                         ASTRewrite astRewrite= ASTRewrite.create(fASTRoot.getAST());
162                         ImportRewrite importRewrite= StubUtility.createImportRewrite(fASTRoot, true);
163
164                         ListRewrite listRewriter= null;
165
166                         ASTNode typeDecl= fASTRoot.findDeclaringNode(fParentType);
167                         if (typeDecl instanceof AbstractTypeDeclaration) {
168                                 listRewriter= astRewrite.getListRewrite(typeDecl, ((AbstractTypeDeclaration) typeDecl).getBodyDeclarationsProperty());
169                         } else if (typeDecl instanceof AnonymousClassDeclaration) {
170                                 listRewriter= astRewrite.getListRewrite(typeDecl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
171                         }
172
173                         if (listRewriter != null) {
174                                 ImportRewriteContext context= new ContextSensitiveImportRewriteContext(typeDecl, importRewrite);
175                                 MethodDeclaration stub= StubUtility2.createConstructorStub(cu, astRewrite, importRewrite, context, fParentType, fOmitSuper ? null : fConstructorBinding, fFieldBindings, fVisibility, fSettings);
176                                 if (stub != null) {
177                                         ASTNode insertion= StubUtility2.getNodeToInsertBefore(listRewriter, fInsert);
178                                         if (insertion != null && insertion.getParent() == typeDecl) {
179                                                 listRewriter.insertBefore(stub, insertion, null);
180                                         } else {
181                                                 listRewriter.insertLast(stub, null);
182                                         }
183                                 }
184                                 fResultingEdit= new MultiTextEdit();
185                                 fResultingEdit.addChild(astRewrite.rewriteAST());
186                                 fResultingEdit.addChild(importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1)));
187
188                                 if (fApply) {
189                                         JavaModelUtil.applyEdit(cu, fResultingEdit, fSave, new SubProgressMonitor(monitor, 1));
190                                 }
191                         }
192                 } finally {
193                         monitor.done();
194                 }
195         }
196
197         /**
198          * Determines whether the call to the super constructor should be omitted.
199          *
200          * @param omit <code>true</code> to omit the call, <code>false</code> otherwise
201          */
202         public final void setOmitSuper(final boolean omit) {
203                 fOmitSuper= omit;
204         }
205
206         /**
207          * Sets the visibility modifier of the generated constructors.
208          *
209          * @param visibility the visibility modifier
210          */
211         public final void setVisibility(final int visibility) {
212                 fVisibility= visibility;
213         }
214 }