1 11 package org.eclipse.jdt.internal.corext.codemanipulation; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 52 public final class AddUnimplementedConstructorsOperation implements IWorkspaceRunnable { 53 54 55 private final boolean fApply; 56 57 58 private String [] fCreatedImports; 59 60 61 private final List fCreatedMethods= new ArrayList (); 62 63 64 private final boolean fImports; 65 66 67 private final int fInsertPos; 68 69 70 private final IMethodBinding[] fConstructorsToImplement; 71 72 73 private boolean fOmitSuper; 74 75 76 private final boolean fSave; 77 78 79 private boolean fCreateComments; 80 81 82 private final ITypeBinding fType; 83 84 85 private final CompilationUnit fASTRoot; 86 87 88 private int fVisibility; 89 90 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 ("AST must not be null and has to be created from a ICompilationUnit"); } 105 if (type == null) { 106 throw new IllegalArgumentException ("The type must not be null"); } 108 ASTNode node= astRoot.findDeclaringNode(type); 109 if (!(node instanceof AnonymousClassDeclaration || node instanceof AbstractTypeDeclaration)) { 110 throw new IllegalArgumentException ("type has to map to a type declaration in the AST"); } 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 131 public String [] getCreatedConstructors() { 132 final String [] keys= new String [fCreatedMethods.size()]; 133 fCreatedMethods.toArray(keys); 134 return keys; 135 } 136 137 142 public String [] getCreatedImports() { 143 return fCreatedImports; 144 } 145 146 151 public ISchedulingRule getSchedulingRule() { 152 return ResourcesPlugin.getWorkspace().getRoot(); 153 } 154 155 160 public int getVisibility() { 161 return fVisibility; 162 } 163 164 169 public boolean isOmitSuper() { 170 return fOmitSuper; 171 } 172 173 177 public void setCreateComments(final boolean comments) { 178 fCreateComments= comments; 179 } 180 181 184 public void run(IProgressMonitor monitor) throws CoreException { 185 if (monitor == null) 186 monitor= new NullProgressMonitor(); 187 try { 188 monitor.beginTask("", 2); 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 (); 209 } 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 263 public void setOmitSuper(final boolean omit) { 264 fOmitSuper= omit; 265 } 266 267 272 public void setVisibility(final int visibility) { 273 fVisibility= visibility; 274 } 275 276 private ASTNode getNodeToInsertBefore(ListRewrite rewriter) { 277 if (fInsertPos != -1) { 278 List 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 |