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.rewrite.ASTRewrite; 39 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 40 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 41 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; 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 AddUnimplementedMethodsOperation 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[] fMethodsToImplement; 71 72 73 private final boolean fSave; 74 75 76 private boolean fDoCreateComments; 77 78 79 private final ITypeBinding fType; 80 81 82 private final CompilationUnit fASTRoot; 83 84 95 public AddUnimplementedMethodsOperation(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] methodsToImplement, int insertPos, final boolean imports, final boolean apply, final boolean save) { 96 if (astRoot == null || !(astRoot.getJavaElement() instanceof ICompilationUnit)) { 97 throw new IllegalArgumentException ("AST must not be null and has to be created from a ICompilationUnit"); } 99 if (type == null) { 100 throw new IllegalArgumentException ("The type must not be null"); } 102 ASTNode node= astRoot.findDeclaringNode(type); 103 if (!(node instanceof AnonymousClassDeclaration || node instanceof AbstractTypeDeclaration)) { 104 throw new IllegalArgumentException ("type has to map to a type declaration in the AST"); } 106 107 fType= type; 108 fInsertPos= insertPos; 109 fASTRoot= astRoot; 110 fMethodsToImplement= methodsToImplement; 111 fSave= save; 112 fApply= apply; 113 fImports= imports; 114 115 fDoCreateComments= StubUtility.doAddComments(astRoot.getJavaElement().getJavaProject()); 116 } 117 118 public void setCreateComments(boolean createComments) { 119 fDoCreateComments= createComments; 120 } 121 122 123 128 public final String [] getCreatedImports() { 129 if (fCreatedImports != null) { 130 return fCreatedImports; 131 } 132 return new String [0]; 133 } 134 135 140 public final String [] getCreatedMethods() { 141 final String [] keys= new String [fCreatedMethods.size()]; 142 fCreatedMethods.toArray(keys); 143 return keys; 144 } 145 146 151 public final ISchedulingRule getSchedulingRule() { 152 return ResourcesPlugin.getWorkspace().getRoot(); 153 } 154 155 158 public final void run(IProgressMonitor monitor) throws CoreException { 159 if (monitor == null) 160 monitor= new NullProgressMonitor(); 161 try { 162 monitor.beginTask("", 2); monitor.setTaskName(CodeGenerationMessages.AddUnimplementedMethodsOperation_description); 164 fCreatedMethods.clear(); 165 ICompilationUnit cu= (ICompilationUnit) fASTRoot.getJavaElement(); 166 167 AST ast= fASTRoot.getAST(); 168 169 ASTRewrite astRewrite= ASTRewrite.create(ast); 170 ImportRewrite importRewrite= StubUtility.createImportRewrite(fASTRoot, true); 171 172 ITypeBinding currTypeBinding= fType; 173 ListRewrite memberRewriter= null; 174 175 ASTNode node= fASTRoot.findDeclaringNode(currTypeBinding); 176 if (node instanceof AnonymousClassDeclaration) { 177 memberRewriter= astRewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 178 } else if (node instanceof AbstractTypeDeclaration) { 179 ChildListPropertyDescriptor property= ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty(); 180 memberRewriter= astRewrite.getListRewrite(node, property); 181 } else { 182 throw new IllegalArgumentException (); 183 } 185 186 final CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(cu.getJavaProject()); 187 settings.createComments= fDoCreateComments; 188 189 ASTNode insertion= getNodeToInsertBefore(memberRewriter); 190 191 IMethodBinding[] methodsToImplement= fMethodsToImplement; 192 if (methodsToImplement == null) { 193 methodsToImplement= StubUtility2.getUnimplementedMethods(currTypeBinding); 194 } 195 196 ImportRewriteContext context= null; 197 int insertionPosition= fInsertPos; 198 if (insertionPosition == -1 && fASTRoot.types().size() > 0) { 199 AbstractTypeDeclaration firstTypeDecl= (AbstractTypeDeclaration)fASTRoot.types().get(0); 200 insertionPosition= firstTypeDecl.getStartPosition(); 201 if (insertionPosition != -1) { 202 context= new ContextSensitiveImportRewriteContext(fASTRoot, insertionPosition, importRewrite); 203 } 204 } 205 206 for (int i= 0; i < methodsToImplement.length; i++) { 207 IMethodBinding curr= methodsToImplement[i]; 208 MethodDeclaration stub= StubUtility2.createImplementationStub(cu, astRewrite, importRewrite, ast, curr, currTypeBinding.getName(), settings, currTypeBinding.isInterface(), context); 209 if (stub != null) { 210 fCreatedMethods.add(curr.getKey()); 211 if (insertion != null) 212 memberRewriter.insertBefore(stub, insertion, null); 213 else 214 memberRewriter.insertLast(stub, null); 215 } 216 } 217 MultiTextEdit edit= new MultiTextEdit(); 218 219 TextEdit importEdits= importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1)); 220 fCreatedImports= importRewrite.getCreatedImports(); 221 if (fImports) { 222 edit.addChild(importEdits); 223 } 224 edit.addChild(astRewrite.rewriteAST()); 225 226 if (fApply) { 227 JavaModelUtil.applyEdit(cu, edit, fSave, new SubProgressMonitor(monitor, 1)); 228 } 229 } finally { 230 monitor.done(); 231 } 232 } 233 234 private ASTNode getNodeToInsertBefore(ListRewrite rewriter) { 235 if (fInsertPos != -1) { 236 List members= rewriter.getOriginalList(); 237 for (int i= 0; i < members.size(); i++) { 238 ASTNode curr= (ASTNode) members.get(i); 239 if (curr.getStartPosition() >= fInsertPos) { 240 return curr; 241 } 242 } 243 } 244 return null; 245 } 246 247 } 248 | Popular Tags |