1 11 package org.eclipse.jdt.internal.corext.codemanipulation; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.List ; 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 72 public final class AddCustomConstructorOperation implements IWorkspaceRunnable { 73 74 75 private boolean fApply= true; 76 77 78 private final IMethodBinding fBinding; 79 80 81 private final IVariableBinding[] fBindings; 82 83 84 private final List fCreated= new ArrayList (); 85 86 87 private TextEdit fEdit= null; 88 89 90 private final IJavaElement fInsert; 91 92 93 private boolean fOmitSuper= false; 94 95 96 private final boolean fSave; 97 98 99 private final CodeGenerationSettings fSettings; 100 101 102 private final IType fType; 103 104 105 private final CompilationUnit fUnit; 106 107 108 private int fVisibility= 0; 109 110 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 142 public final TextEdit getResultingEdit() { 143 return fEdit; 144 } 145 146 151 public final ISchedulingRule getSchedulingRule() { 152 return ResourcesPlugin.getWorkspace().getRoot(); 153 } 154 155 160 public final int getVisibility() { 161 return fVisibility; 162 } 163 164 169 public final boolean isOmitSuper() { 170 return fOmitSuper; 171 } 172 173 176 public final void run(IProgressMonitor monitor) throws CoreException { 177 if (monitor == null) 178 monitor= new NullProgressMonitor(); 179 try { 180 monitor.beginTask("", 1); 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 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 271 public final void setOmitSuper(final boolean omit) { 272 fOmitSuper= omit; 273 } 274 275 280 public final void setVisibility(final int visibility) { 281 fVisibility= visibility; 282 } 283 } 284 | Popular Tags |