KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > ReplaceInvocationsRefactoring


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.code;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.text.edits.MalformedTreeException;
19 import org.eclipse.text.edits.MultiTextEdit;
20 import org.eclipse.text.edits.TextEdit;
21 import org.eclipse.text.edits.TextEditGroup;
22
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.OperationCanceledException;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IResource;
31
32 import org.eclipse.jface.text.BadLocationException;
33 import org.eclipse.jface.text.Document;
34 import org.eclipse.jface.text.IDocument;
35
36 import org.eclipse.ltk.core.refactoring.Change;
37 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
38 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
39 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
40 import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;
41
42 import org.eclipse.jdt.core.Flags;
43 import org.eclipse.jdt.core.IClassFile;
44 import org.eclipse.jdt.core.ICompilationUnit;
45 import org.eclipse.jdt.core.IJavaElement;
46 import org.eclipse.jdt.core.IJavaProject;
47 import org.eclipse.jdt.core.IMethod;
48 import org.eclipse.jdt.core.IType;
49 import org.eclipse.jdt.core.ITypeHierarchy;
50 import org.eclipse.jdt.core.ITypeRoot;
51 import org.eclipse.jdt.core.JavaModelException;
52 import org.eclipse.jdt.core.dom.AST;
53 import org.eclipse.jdt.core.dom.ASTNode;
54 import org.eclipse.jdt.core.dom.ASTParser;
55 import org.eclipse.jdt.core.dom.Block;
56 import org.eclipse.jdt.core.dom.BodyDeclaration;
57 import org.eclipse.jdt.core.dom.CompilationUnit;
58 import org.eclipse.jdt.core.dom.ExpressionStatement;
59 import org.eclipse.jdt.core.dom.IBinding;
60 import org.eclipse.jdt.core.dom.IMethodBinding;
61 import org.eclipse.jdt.core.dom.MethodDeclaration;
62 import org.eclipse.jdt.core.dom.MethodInvocation;
63 import org.eclipse.jdt.core.dom.Modifier;
64 import org.eclipse.jdt.core.dom.SimpleName;
65 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
66 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
67 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
68 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
69
70 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
71 import org.eclipse.jdt.internal.corext.refactoring.Checks;
72 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptor;
73 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
74 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
75 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
76 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
77 import org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator;
78 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
79 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange;
80 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
81 import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager;
82 import org.eclipse.jdt.internal.corext.util.Messages;
83
84 import org.eclipse.jdt.ui.JavaElementLabels;
85
86 import org.eclipse.jdt.internal.ui.JavaPlugin;
87 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
88
89 public class ReplaceInvocationsRefactoring extends ScriptableRefactoring {
90
91     private static final String JavaDoc ID_REPLACE_INVOCATIONS= "org.eclipse.jdt.ui.replace.invocations"; //$NON-NLS-1$
92
private static final String JavaDoc ATTRIBUTE_MODE= "mode"; //$NON-NLS-1$
93

94     public static class Mode {
95         private Mode() {
96         }
97         public static final Mode REPLACE_ALL= new Mode();
98         public static final Mode REPLACE_SINGLE= new Mode();
99     }
100
101     private final ITypeRoot fSelectionTypeRoot;
102     /**
103      * only set if initial mode is REPLACE_SINGLE
104      */

105     private final int fSelectionStart;
106     /**
107      * only set if initial mode is REPLACE_SINGLE
108      */

109     private final int fSelectionLength;
110     
111     private ASTNode fSelectionNode;
112     /**
113      * only set after checkInitialConditions
114      */

115     private IMethod fMethod;
116     
117     private String JavaDoc fBody;
118     private String JavaDoc[] fParameterNames;
119     
120     private SourceProvider fSourceProvider; // resolved once in checkInitialConditions
121
private TargetProvider fTargetProvider; // flexible
122

123     private TextChangeManager fChangeManager;
124     private IMethodBinding fMethodBinding;
125     
126     public ReplaceInvocationsRefactoring(ITypeRoot typeRoot, int offset, int length) {
127         fSelectionTypeRoot= typeRoot;
128         fSelectionStart= offset;
129         fSelectionLength= length;
130     }
131
132     public ReplaceInvocationsRefactoring(IMethod method) {
133         fMethod= method;
134         fSelectionTypeRoot= method.getTypeRoot();
135         fSelectionStart= -1;
136         fSelectionLength= -1;
137     }
138     
139     public String JavaDoc getName() {
140         return RefactoringCoreMessages.ReplaceInvocationsRefactoring_name;
141     }
142     
143     /**
144      * Only to be called after {@link #checkInitialConditions(IProgressMonitor)}
145      */

146     public boolean canReplaceSingle() {
147         return fSelectionNode instanceof MethodInvocation;
148     }
149     
150     /**
151      * Only to be called after {@link #checkInitialConditions(IProgressMonitor)}
152      */

153     public RefactoringStatus setCurrentMode(Mode mode) throws JavaModelException {
154         if (fTargetProvider.isSingle() == (mode == Mode.REPLACE_SINGLE))
155             return new RefactoringStatus();
156         Assert.isTrue(canReplaceSingle());
157         if (mode == Mode.REPLACE_SINGLE) {
158             fTargetProvider= TargetProvider.create((ICompilationUnit) fSelectionTypeRoot, (MethodInvocation) fSelectionNode);
159         } else {
160             fTargetProvider= TargetProvider.create(fSourceProvider.getDeclaration());
161         }
162         return fTargetProvider.checkActivation();
163     }
164     
165     public void setBody(String JavaDoc body, String JavaDoc[] parameterNames) {
166         //TODO: validate parameter name count and body
167
fBody= body;
168         fParameterNames= parameterNames;
169     }
170     
171     public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
172         // TargetProvider must get an untampered AST with original invocation node
173
// SourceProvider must get a tweaked AST with method body / parameter names replaced
174

175         RefactoringStatus result= new RefactoringStatus();
176         
177         if (fMethod == null) {
178             if (! (fSelectionTypeRoot instanceof ICompilationUnit))
179                 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_replace_in_binary);
180             
181             ICompilationUnit cu= (ICompilationUnit) fSelectionTypeRoot;
182             CompilationUnit root= new RefactoringASTParser(AST.JLS3).parse(cu, true);
183             fSelectionNode= getTargetNode(cu, root, fSelectionStart, fSelectionLength);
184             if (fSelectionNode == null)
185                 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReplaceInvocationsRefactoring_select_method_to_apply);
186             
187             if (fSelectionNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
188                 MethodDeclaration methodDeclaration= (MethodDeclaration) fSelectionNode;
189                 fTargetProvider= TargetProvider.create(methodDeclaration);
190                 fMethodBinding= methodDeclaration.resolveBinding();
191             } else {
192                 MethodInvocation methodInvocation= (MethodInvocation) fSelectionNode;
193                 fTargetProvider= TargetProvider.create(cu, methodInvocation);
194                 fMethodBinding= methodInvocation.resolveMethodBinding();
195             }
196             if (fMethodBinding == null)
197                 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
198             fMethod= (IMethod) fMethodBinding.getJavaElement();
199             
200         } else {
201             ASTParser parser= ASTParser.newParser(AST.JLS3);
202             parser.setProject(fMethod.getJavaProject());
203             IBinding[] bindings= parser.createBindings(new IJavaElement[] { fMethod }, null);
204             fMethodBinding= (IMethodBinding) bindings[0];
205             if (fMethodBinding == null)
206                 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
207             
208             fTargetProvider= TargetProvider.create(fMethodBinding);
209         }
210         
211         result.merge(fTargetProvider.checkActivation());
212         return result;
213     }
214     
215     private SourceProvider resolveSourceProvider(IMethodBinding methodBinding, RefactoringStatus status) throws JavaModelException {
216         final IMethod method= (IMethod) methodBinding.getJavaElement();
217         
218         ITypeRoot typeRoot;
219         IDocument source;
220         CompilationUnit methodDeclarationAstRoot;
221         
222         ICompilationUnit methodCu= (method).getCompilationUnit();
223         if (methodCu != null) {
224             typeRoot= methodCu;
225             ASTParser parser= ASTParser.newParser(AST.JLS3);
226             parser.setSource(methodCu);
227             parser.setFocalPosition(method.getNameRange().getOffset());
228             CompilationUnit compilationUnit= (CompilationUnit) parser.createAST(null);
229             MethodDeclaration methodDecl= (MethodDeclaration) NodeFinder.perform(compilationUnit, method.getNameRange()).getParent();
230             AST ast= compilationUnit.getAST();
231             ASTRewrite rewrite= ASTRewrite.create(ast);
232             Block newBody= ast.newBlock();
233             newBody.statements().add(rewrite.createStringPlaceholder(fBody, ASTNode.EMPTY_STATEMENT));
234             rewrite.replace(methodDecl.getBody(), newBody, null);
235             List JavaDoc parameters= methodDecl.parameters();
236             for (int i= 0; i < parameters.size(); i++) {
237                 SingleVariableDeclaration parameter= (SingleVariableDeclaration) parameters.get(i);
238                 rewrite.set(parameter.getName(), SimpleName.IDENTIFIER_PROPERTY, fParameterNames[i], null);
239             }
240             TextEdit textEdit= rewrite.rewriteAST();
241             Document document= new Document(methodCu.getBuffer().getContents());
242             try {
243                 textEdit.apply(document);
244             } catch (MalformedTreeException e) {
245                 JavaPlugin.log(e);
246             } catch (BadLocationException e) {
247                 JavaPlugin.log(e);
248             }
249             source= document;
250             
251             methodDeclarationAstRoot= new RefactoringASTParser(AST.JLS3).parse(source.get(), methodCu, true, true, null);
252             
253         } else {
254             IClassFile classFile= method.getClassFile();
255             //TODO: use source if available?
256
StubCreator stubCreator= new StubCreator(true) {
257                 protected void appendMethodBody(IMethod currentMethod) throws JavaModelException {
258                     if (currentMethod.equals(method)) {
259                         fBuffer.append(fBody);
260                     } else {
261                         super.appendMethodBody(currentMethod);
262                     }
263                 }
264                 /*
265                  * @see org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator#appendMethodParameterName(org.eclipse.jdt.core.IMethod, int)
266                  */

267                 protected void appendMethodParameterName(IMethod currentMethod, int index) {
268                     if (currentMethod.equals(method)) {
269                         fBuffer.append(fParameterNames[index]);
270                     } else {
271                         super.appendMethodParameterName(currentMethod, index);
272                     }
273                 }
274             };
275             
276             String JavaDoc stub= stubCreator.createStub(classFile.getType(), null);
277             source= new Document(stub);
278             methodDeclarationAstRoot= new RefactoringASTParser(AST.JLS3).parse(stub, classFile, true, true, null);
279             typeRoot= classFile;
280         }
281         ASTNode node= methodDeclarationAstRoot.findDeclaringNode(methodBinding.getKey());
282         if (node instanceof MethodDeclaration) {
283             return new SourceProvider(typeRoot, source, (MethodDeclaration) node);
284         } else {
285             status.addFatalError(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_find_method_declaration);
286             return null;
287         }
288     }
289     
290     /**
291      * @return an invocation or declaration node
292      */

293     private static ASTNode getTargetNode(ICompilationUnit unit, CompilationUnit root, int offset, int length) {
294         ASTNode node= null;
295         try {
296             node= checkNode(NodeFinder.perform(root, offset, length, unit), unit);
297         } catch(JavaModelException e) {
298             // Do nothing
299
}
300         if (node != null)
301             return node;
302         return checkNode(NodeFinder.perform(root, offset, length), unit);
303     }
304
305     private static ASTNode checkNode(ASTNode node, ICompilationUnit unit) {
306         if (node == null)
307             return null;
308         if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
309             node= node.getParent();
310         } else if (node.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
311             node= ((ExpressionStatement)node).getExpression();
312         }
313         switch(node.getNodeType()) {
314             case ASTNode.METHOD_DECLARATION:
315             case ASTNode.METHOD_INVOCATION:
316 // not yet...
317
// case ASTNode.SUPER_METHOD_INVOCATION:
318
// case ASTNode.CONSTRUCTOR_INVOCATION:
319
return node;
320         }
321         return null;
322     }
323     
324     public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
325         pm.beginTask("", 20); //$NON-NLS-1$
326
fChangeManager= new TextChangeManager();
327         RefactoringStatus result= new RefactoringStatus();
328         
329         fSourceProvider= resolveSourceProvider(fMethodBinding, result);
330         if (result.hasFatalError())
331             return result;
332         
333         result.merge(fSourceProvider.checkActivation());
334         if (result.hasFatalError())
335             return result;
336         
337         fSourceProvider.initialize();
338         fTargetProvider.initialize();
339         pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
340         RefactoringStatus searchStatus= new RefactoringStatus();
341         ICompilationUnit[] units= fTargetProvider.getAffectedCompilationUnits(searchStatus, new SubProgressMonitor(pm, 1));
342         if (searchStatus.hasFatalError()) {
343             result.merge(searchStatus);
344             return result;
345         }
346         IFile[] filesToBeModified= getFilesToBeModified(units);
347         result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
348         if (result.hasFatalError())
349             return result;
350         result.merge(ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1)));
351         checkOverridden(result, new SubProgressMonitor(pm, 4));
352         IProgressMonitor sub= new SubProgressMonitor(pm, 15);
353         sub.beginTask("", units.length * 3); //$NON-NLS-1$
354
for (int c= 0; c < units.length; c++) {
355             ICompilationUnit unit= units[c];
356             sub.subTask(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_processing, unit.getElementName()));
357             CallInliner inliner= null;
358             try {
359                 boolean added= false;
360                 MultiTextEdit root= new MultiTextEdit();
361                 CompilationUnitChange change= (CompilationUnitChange)fChangeManager.get(unit);
362                 change.setEdit(root);
363                 BodyDeclaration[] bodies= fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
364                 if (bodies.length == 0)
365                     continue;
366                 inliner= new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
367                 for (int b= 0; b < bodies.length; b++) {
368                     BodyDeclaration body= bodies[b];
369                     inliner.initialize(body);
370                     RefactoringStatus nestedInvocations= new RefactoringStatus();
371                     ASTNode[] invocations= removeNestedCalls(nestedInvocations, unit,
372                         fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
373                     for (int i= 0; i < invocations.length; i++) {
374                         ASTNode invocation= invocations[i];
375                         result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
376                         if (result.hasFatalError())
377                             break;
378                         if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
379                             added= true;
380                             TextEditGroup group= new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
381                             change.addTextEditGroup(group);
382                             result.merge(inliner.perform(group));
383                         }
384                     }
385                     // do this after we have inlined the method calls. We still want
386
// to generate the modifications.
387
result.merge(nestedInvocations);
388                 }
389                 if (!added) {
390                     fChangeManager.remove(unit);
391                 } else {
392                     root.addChild(inliner.getModifications());
393                     ImportRewrite rewrite= inliner.getImportEdit();
394                     if (rewrite.hasRecordedChanges()) {
395                         TextEdit edit= rewrite.rewriteImports(null);
396                         if (edit instanceof MultiTextEdit ? ((MultiTextEdit)edit).getChildrenSize() > 0 : true) {
397                             root.addChild(edit);
398                             change.addTextEditGroup(
399                                 new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_import, new TextEdit[] {edit}));
400                         }
401                     }
402                 }
403             } finally {
404                 if (inliner != null)
405                     inliner.dispose();
406             }
407             sub.worked(1);
408             if (sub.isCanceled())
409                 throw new OperationCanceledException();
410         }
411         result.merge(searchStatus);
412         sub.done();
413         pm.done();
414         return result;
415     }
416
417     public Change createChange(IProgressMonitor pm) throws CoreException {
418         // TODO: update for fSelectionStart == -1
419
final Map JavaDoc arguments= new HashMap JavaDoc();
420         String JavaDoc project= null;
421         IJavaProject javaProject= fSelectionTypeRoot.getJavaProject();
422         if (javaProject != null)
423             project= javaProject.getElementName();
424         final IMethodBinding binding= fSourceProvider.getDeclaration().resolveBinding();
425         int flags= RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
426         if (!Modifier.isPrivate(binding.getModifiers()))
427             flags|= RefactoringDescriptor.MULTI_CHANGE;
428         final String JavaDoc description= Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description_short, binding.getName());
429         final String JavaDoc header= Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description, new String JavaDoc[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(binding.getDeclaringClass(), JavaElementLabels.ALL_FULLY_QUALIFIED)});
430         final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
431         comment.addSetting(Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
432         if (!fTargetProvider.isSingle())
433             comment.addSetting(RefactoringCoreMessages.ReplaceInvocationsRefactoring_replace_references);
434         final JDTRefactoringDescriptor descriptor= new JDTRefactoringDescriptor(ID_REPLACE_INVOCATIONS, project, description, comment.asString(), arguments, flags);
435         arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_INPUT, descriptor.elementToHandle(fSelectionTypeRoot));
436         arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_SELECTION, new Integer JavaDoc(fSelectionStart).toString() + " " + new Integer JavaDoc(fSelectionLength).toString()); //$NON-NLS-1$
437
arguments.put(ATTRIBUTE_MODE, new Integer JavaDoc(fTargetProvider.isSingle() ? 0 : 1).toString());
438         return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.ReplaceInvocationsRefactoring_change_name, fChangeManager.getAllChanges());
439     }
440     
441     private IFile[] getFilesToBeModified(ICompilationUnit[] units) {
442         List JavaDoc result= new ArrayList JavaDoc(units.length + 1);
443         IFile file;
444         for (int i= 0; i < units.length; i++) {
445             file= getFile(units[i]);
446             if (file != null)
447                 result.add(file);
448         }
449         return (IFile[])result.toArray(new IFile[result.size()]);
450     }
451     
452     private IFile getFile(ICompilationUnit unit) {
453         unit= unit.getPrimary();
454         IResource resource= unit.getResource();
455         if (resource != null && resource.getType() == IResource.FILE)
456             return (IFile)resource;
457         return null;
458     }
459     
460     private void checkOverridden(RefactoringStatus status, IProgressMonitor pm) throws JavaModelException {
461         pm.beginTask("", 9); //$NON-NLS-1$
462
pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden);
463         MethodDeclaration decl= fSourceProvider.getDeclaration();
464         IMethod method= (IMethod) decl.resolveBinding().getJavaElement();
465         if (method == null || Flags.isPrivate(method.getFlags())) {
466             pm.worked(8);
467             return;
468         }
469         IType type= method.getDeclaringType();
470         ITypeHierarchy hierarchy= type.newTypeHierarchy(new SubProgressMonitor(pm, 6));
471         checkSubTypes(status, method, hierarchy.getAllSubtypes(type), new SubProgressMonitor(pm, 1));
472         checkSuperClasses(status, method, hierarchy.getAllSuperclasses(type), new SubProgressMonitor(pm, 1));
473         checkSuperInterfaces(status, method, hierarchy.getAllSuperInterfaces(type), new SubProgressMonitor(pm, 1));
474         pm.setTaskName(""); //$NON-NLS-1$
475
}
476
477     private void checkSubTypes(RefactoringStatus result, IMethod method, IType[] types, IProgressMonitor pm) {
478         checkTypes(
479             result, method, types,
480             RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden_error,
481             pm);
482     }
483     
484     private void checkSuperClasses(RefactoringStatus result, IMethod method, IType[] types, IProgressMonitor pm) {
485         checkTypes(
486             result, method, types,
487             RefactoringCoreMessages.InlineMethodRefactoring_checking_overrides_error,
488             pm);
489     }
490
491     private void checkSuperInterfaces(RefactoringStatus result, IMethod method, IType[] types, IProgressMonitor pm) {
492         checkTypes(
493             result, method, types,
494             RefactoringCoreMessages.InlineMethodRefactoring_checking_implements_error,
495             pm);
496     }
497     private void checkTypes(RefactoringStatus result, IMethod method, IType[] types, String JavaDoc key, IProgressMonitor pm) {
498         pm.beginTask("", types.length); //$NON-NLS-1$
499
for (int i= 0; i < types.length; i++) {
500             pm.worked(1);
501             IMethod[] overridden= types[i].findMethods(method);
502             if (overridden != null && overridden.length > 0) {
503                 result.addError(
504                     Messages.format(key, types[i].getElementName()),
505                     JavaStatusContext.create(overridden[0]));
506             }
507         }
508     }
509     
510     private ASTNode[] removeNestedCalls(RefactoringStatus status, ICompilationUnit unit, ASTNode[] invocations) {
511         if (invocations.length <= 1)
512             return invocations;
513         ASTNode[] parents= new ASTNode[invocations.length];
514         for (int i= 0; i < invocations.length; i++) {
515             parents[i]= invocations[i].getParent();
516         }
517         for (int i= 0; i < invocations.length; i++) {
518             removeNestedCalls(status, unit, parents, invocations, i);
519         }
520         List JavaDoc result= new ArrayList JavaDoc();
521         for (int i= 0; i < invocations.length; i++) {
522             if (invocations[i] != null)
523                 result.add(invocations[i]);
524         }
525         return (ASTNode[])result.toArray(new ASTNode[result.size()]);
526     }
527     
528     private void removeNestedCalls(RefactoringStatus status, ICompilationUnit unit, ASTNode[] parents, ASTNode[] invocations, int index) {
529         ASTNode invocation= invocations[index];
530         for (int i= 0; i < parents.length; i++) {
531             ASTNode parent= parents[i];
532             while (parent != null) {
533                 if (parent == invocation) {
534                     status.addError(RefactoringCoreMessages.InlineMethodRefactoring_nestedInvocation,
535                         JavaStatusContext.create(unit, parent));
536                     invocations[index]= null;
537                 }
538                 parent= parent.getParent();
539             }
540         }
541     }
542
543     public RefactoringStatus initialize(final RefactoringArguments arguments) {
544         if (arguments instanceof JavaRefactoringArguments) {
545             final JavaRefactoringArguments generic= (JavaRefactoringArguments) arguments;
546             final String JavaDoc value= generic.getAttribute(ATTRIBUTE_MODE);
547             if (value != null && !"".equals(value)) {//$NON-NLS-1$
548
int mode= 0;
549                 try {
550                     mode= Integer.parseInt(value);
551                 } catch (NumberFormatException JavaDoc exception) {
552                     return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_MODE));
553                 }
554                 try {
555                     setCurrentMode(mode == 1 ? Mode.REPLACE_ALL : Mode.REPLACE_SINGLE);
556                 } catch (JavaModelException exception) {
557                     return RefactoringStatus.createFatalErrorStatus(exception.getLocalizedMessage());
558                 }
559             }
560         } else
561             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
562         return new RefactoringStatus();
563     }
564
565     public IMethod getMethod() {
566         return fMethod;
567     }
568
569 }
570
Popular Tags