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.TextEdit; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.NullProgressMonitor; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.core.runtime.SubProgressMonitor; 25 import org.eclipse.core.runtime.jobs.ISchedulingRule; 26 27 import org.eclipse.core.filebuffers.ITextFileBuffer; 28 29 import org.eclipse.core.resources.IWorkspaceRunnable; 30 import org.eclipse.core.resources.ResourcesPlugin; 31 32 import org.eclipse.jface.text.Document; 33 import org.eclipse.jface.text.IDocument; 34 35 import org.eclipse.ltk.core.refactoring.Change; 36 37 import org.eclipse.jdt.core.Flags; 38 import org.eclipse.jdt.core.ICompilationUnit; 39 import org.eclipse.jdt.core.IField; 40 import org.eclipse.jdt.core.IJavaElement; 41 import org.eclipse.jdt.core.IMember; 42 import org.eclipse.jdt.core.IMethod; 43 import org.eclipse.jdt.core.ISourceReference; 44 import org.eclipse.jdt.core.IType; 45 import org.eclipse.jdt.core.dom.ASTNode; 46 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 47 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; 48 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 49 import org.eclipse.jdt.core.dom.CompilationUnit; 50 import org.eclipse.jdt.core.dom.EnumConstantDeclaration; 51 import org.eclipse.jdt.core.dom.IBinding; 52 import org.eclipse.jdt.core.dom.ITypeBinding; 53 import org.eclipse.jdt.core.dom.MethodDeclaration; 54 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 55 56 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 57 import org.eclipse.jdt.internal.corext.dom.NodeFinder; 58 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 59 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; 60 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringFileBuffers; 61 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 62 63 import org.eclipse.jdt.internal.ui.JavaPlugin; 64 65 70 public final class AddDelegateMethodsOperation implements IWorkspaceRunnable { 71 72 73 private boolean fApply= true; 74 75 76 private final List fCreated= new ArrayList (); 77 78 79 private TextEdit fEdit= null; 80 81 82 private final IJavaElement fInsert; 83 84 85 private final String [] fMethodKeys; 86 87 88 private final boolean fSave; 89 90 91 private final CodeGenerationSettings fSettings; 92 93 94 private final IType fType; 95 96 97 private final CompilationUnit fUnit; 98 99 100 private final String [] fVariableKeys; 101 102 113 public AddDelegateMethodsOperation(final IType type, final IJavaElement insert, final CompilationUnit unit, final String [] variableKeys, final String [] methodKeys, final CodeGenerationSettings settings, final boolean apply, final boolean save) { 114 Assert.isNotNull(type); 115 Assert.isNotNull(unit); 116 Assert.isNotNull(variableKeys); 117 Assert.isNotNull(methodKeys); 118 Assert.isNotNull(settings); 119 Assert.isTrue(variableKeys.length == methodKeys.length); 120 fType= type; 121 fInsert= insert; 122 fUnit= unit; 123 fVariableKeys= variableKeys; 124 fMethodKeys= methodKeys; 125 fSettings= settings; 126 fSave= save; 127 fApply= apply; 128 } 129 130 135 public final String [] getCreatedMethods() { 136 final String [] keys= new String [fCreated.size()]; 137 fCreated.toArray(keys); 138 return keys; 139 } 140 141 146 public final TextEdit getResultingEdit() { 147 return fEdit; 148 } 149 150 155 public final ISchedulingRule getSchedulingRule() { 156 return ResourcesPlugin.getWorkspace().getRoot(); 157 } 158 159 162 public final void run(IProgressMonitor monitor) throws CoreException { 163 if (monitor == null) 164 monitor= new NullProgressMonitor(); 165 try { 166 monitor.beginTask("", 1); monitor.setTaskName(CodeGenerationMessages.AddDelegateMethodsOperation_monitor_message); 168 fCreated.clear(); 169 final ICompilationUnit unit= fType.getCompilationUnit(); 170 final CompilationUnitRewrite rewrite= new CompilationUnitRewrite(unit, fUnit); 171 ITypeBinding binding= null; 172 ListRewrite rewriter= null; 173 if (fType.isAnonymous()) { 174 final IJavaElement parent= fType.getParent(); 175 if (parent instanceof IField && Flags.isEnum(((IMember) parent).getFlags())) { 176 final EnumConstantDeclaration constant= (EnumConstantDeclaration) NodeFinder.perform(rewrite.getRoot(), ((ISourceReference) parent).getSourceRange()); 177 if (constant != null) { 178 final AnonymousClassDeclaration declaration= constant.getAnonymousClassDeclaration(); 179 if (declaration != null) { 180 binding= declaration.resolveBinding(); 181 if (binding != null) 182 rewriter= rewrite.getASTRewrite().getListRewrite(declaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 183 } 184 } 185 } else { 186 final ClassInstanceCreation creation= (ClassInstanceCreation) ASTNodes.getParent(NodeFinder.perform(rewrite.getRoot(), fType.getNameRange()), ClassInstanceCreation.class); 187 if (creation != null) { 188 binding= creation.resolveTypeBinding(); 189 final AnonymousClassDeclaration declaration= creation.getAnonymousClassDeclaration(); 190 if (declaration != null) 191 rewriter= rewrite.getASTRewrite().getListRewrite(declaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); 192 } 193 } 194 } else { 195 final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(NodeFinder.perform(rewrite.getRoot(), fType.getNameRange()), AbstractTypeDeclaration.class); 196 if (declaration != null) { 197 binding= declaration.resolveBinding(); 198 rewriter= rewrite.getASTRewrite().getListRewrite(declaration, declaration.getBodyDeclarationsProperty()); 199 } 200 } 201 if (binding != null && rewriter != null) { 202 final IBinding[][] bindings= StubUtility2.getDelegatableMethods(rewrite.getAST(), binding); 203 if (bindings != null && bindings.length > 0) { 204 ITextFileBuffer buffer= null; 205 IDocument document= null; 206 try { 207 if (!JavaModelUtil.isPrimary(unit)) 208 document= new Document(unit.getBuffer().getContents()); 209 else { 210 buffer= RefactoringFileBuffers.acquire(unit); 211 document= buffer.getDocument(); 212 } 213 ASTNode insertion= null; 214 if (fInsert instanceof IMethod) 215 insertion= ASTNodes.getParent(NodeFinder.perform(rewrite.getRoot(), ((IMethod) fInsert).getNameRange()), MethodDeclaration.class); 216 String variableKey= null; 217 String methodKey= null; 218 MethodDeclaration stub= null; 219 for (int index= 0; index < fMethodKeys.length; index++) { 220 methodKey= fMethodKeys[index]; 221 variableKey= fVariableKeys[index]; 222 if (monitor.isCanceled()) 223 break; 224 for (int offset= 0; offset < bindings.length; offset++) { 225 if (bindings[offset][0].getKey().equals(variableKey) && bindings[offset][1].getKey().equals(methodKey)) { 226 stub= StubUtility2.createDelegationStub(rewrite.getCu(), rewrite.getASTRewrite(), rewrite.getImportRewrite(), rewrite.getAST(), bindings[offset], fSettings); 227 if (stub != null) { 228 fCreated.add(methodKey); 229 if (insertion != null) 230 rewriter.insertBefore(stub, insertion, null); 231 else 232 rewriter.insertLast(stub, null); 233 } 234 break; 235 } 236 } 237 } 238 final Change result= rewrite.createChange(); 239 if (result instanceof CompilationUnitChange) { 240 final CompilationUnitChange change= (CompilationUnitChange) result; 241 final TextEdit edit= change.getEdit(); 242 if (edit != null) { 243 try { 244 fEdit= edit; 245 if (fApply) 246 edit.apply(document, TextEdit.UPDATE_REGIONS); 247 if (fSave) { 248 if (buffer != null) 249 buffer.commit(new SubProgressMonitor(monitor, 1), true); 250 else 251 unit.getBuffer().setContents(document.get()); 252 } 253 } catch (Exception exception) { 254 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, exception.getLocalizedMessage(), exception)); 255 } 256 } 257 } 258 } finally { 259 if (buffer != null) 260 RefactoringFileBuffers.release(unit); 261 } 262 } 263 } 264 } finally { 265 monitor.done(); 266 } 267 } 268 } 269 | Popular Tags |