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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.codemanipulation;
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;
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.text.edits.MultiTextEdit;
24 import org.eclipse.text.edits.TextEdit;
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;
41 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
44 * Workspace runnable to add custom constructors initializing fields.
48 public final class AddCustomConstructorOperation implements IWorkspaceRunnable {
50 /** Should the resulting edit be applied? */
51 private boolean fApply= true;
53 /** The super constructor method binding */
54 private final IMethodBinding fConstructorBinding;
56 /** The variable bindings to implement */
57 private final IVariableBinding[] fFieldBindings;
59 /** The resulting text edit */
60 private TextEdit fResultingEdit= null;
62 /** The insertion point, or <code>null</code> */
63 private final IJavaElement fInsert;
65 /** Should the call to the super constructor be omitted? */
66 private boolean fOmitSuper= false;
68 /** Should the compilation unit content be saved? */
69 private final boolean fSave;
71 /** The code generation settings to use */
72 private final CodeGenerationSettings fSettings;
74 /** The type declaration to add the constructors to */
75 private final ITypeBinding fParentType;
77 /** The compilation unit ast node */
78 private final CompilationUnit fASTRoot;
80 /** The visibility flags of the new constructor */
81 private int fVisibility= 0;
84 * Creates a new add custom constructor operation.
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>
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
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;
106 fFieldBindings= variables;
107 fConstructorBinding= constructor;
114 * Returns the resulting text edit.
116 * @return the resulting text edit
118 public final TextEdit getResultingEdit() {
119 return fResultingEdit;
123 * Returns the scheduling rule for this operation.
125 * @return the scheduling rule
127 public final ISchedulingRule getSchedulingRule() {
128 return ResourcesPlugin.getWorkspace().getRoot();
132 * Returns the visibility modifier of the generated constructors.
134 * @return the visibility modifier
136 public final int getVisibility() {
141 * Should the call to the super constructor be omitted?
143 * @return <code>true</code> to omit the call, <code>false</code> otherwise
145 public final boolean isOmitSuper() {
150 * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
152 public final void run(IProgressMonitor monitor) throws CoreException {
154 monitor= new NullProgressMonitor();
156 monitor.beginTask("", 2); //$NON-NLS-1$
157 monitor.setTaskName(CodeGenerationMessages.AddCustomConstructorOperation_description);
159 ICompilationUnit cu= (ICompilationUnit) fASTRoot.getTypeRoot();
161 ASTRewrite astRewrite= ASTRewrite.create(fASTRoot.getAST());
162 ImportRewrite importRewrite= StubUtility.createImportRewrite(fASTRoot, true);
164 ListRewrite listRewriter= null;
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);
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);
177 ASTNode insertion= StubUtility2.getNodeToInsertBefore(listRewriter, fInsert);
178 if (insertion != null && insertion.getParent() == typeDecl) {
179 listRewriter.insertBefore(stub, insertion, null);
181 listRewriter.insertLast(stub, null);
184 fResultingEdit= new MultiTextEdit();
185 fResultingEdit.addChild(astRewrite.rewriteAST());
186 fResultingEdit.addChild(importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1)));
189 JavaModelUtil.applyEdit(cu, fResultingEdit, fSave, new SubProgressMonitor(monitor, 1));
198 * Determines whether the call to the super constructor should be omitted.
200 * @param omit <code>true</code> to omit the call, <code>false</code> otherwise
202 public final void setOmitSuper(final boolean omit) {
207 * Sets the visibility modifier of the generated constructors.
209 * @param visibility the visibility modifier
211 public final void setVisibility(final int visibility) {
212 fVisibility= visibility;