|
Link Here
|
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
| 10 |
* Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056 |
10 |
* Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056 |
| 11 |
* Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607 |
11 |
* Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607 |
|
|
12 |
* Nikolay Metchev <nikolaymetchev@gmail.com> - [extract method] Compilation error on extracting to outer type |
| 12 |
*******************************************************************************/ |
13 |
*******************************************************************************/ |
| 13 |
package org.eclipse.jdt.internal.corext.refactoring.code; |
14 |
package org.eclipse.jdt.internal.corext.refactoring.code; |
| 14 |
|
15 |
|
| 15 |
import java.util.ArrayList; |
16 |
import java.util.ArrayList; |
| 16 |
import java.util.HashSet; |
17 |
import java.util.HashSet; |
|
Link Here
|
| 50 |
import org.eclipse.jdt.core.dom.IVariableBinding; |
51 |
import org.eclipse.jdt.core.dom.IVariableBinding; |
| 51 |
import org.eclipse.jdt.core.dom.Initializer; |
52 |
import org.eclipse.jdt.core.dom.Initializer; |
| 52 |
import org.eclipse.jdt.core.dom.LabeledStatement; |
53 |
import org.eclipse.jdt.core.dom.LabeledStatement; |
| 53 |
import org.eclipse.jdt.core.dom.Message; |
54 |
import org.eclipse.jdt.core.dom.Message; |
| 54 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
55 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
|
|
56 |
import org.eclipse.jdt.core.dom.MethodInvocation; |
| 57 |
import org.eclipse.jdt.core.dom.Modifier; |
| 55 |
import org.eclipse.jdt.core.dom.Name; |
58 |
import org.eclipse.jdt.core.dom.Name; |
| 56 |
import org.eclipse.jdt.core.dom.PrimitiveType; |
59 |
import org.eclipse.jdt.core.dom.PrimitiveType; |
| 57 |
import org.eclipse.jdt.core.dom.QualifiedName; |
60 |
import org.eclipse.jdt.core.dom.QualifiedName; |
| 58 |
import org.eclipse.jdt.core.dom.SimpleName; |
61 |
import org.eclipse.jdt.core.dom.SimpleName; |
| 59 |
import org.eclipse.jdt.core.dom.Statement; |
62 |
import org.eclipse.jdt.core.dom.Statement; |
|
Link Here
|
| 62 |
import org.eclipse.jdt.core.dom.SwitchCase; |
65 |
import org.eclipse.jdt.core.dom.SwitchCase; |
| 63 |
import org.eclipse.jdt.core.dom.SwitchStatement; |
66 |
import org.eclipse.jdt.core.dom.SwitchStatement; |
| 64 |
import org.eclipse.jdt.core.dom.ThisExpression; |
67 |
import org.eclipse.jdt.core.dom.ThisExpression; |
| 65 |
import org.eclipse.jdt.core.dom.TryStatement; |
68 |
import org.eclipse.jdt.core.dom.TryStatement; |
| 66 |
import org.eclipse.jdt.core.dom.Type; |
69 |
import org.eclipse.jdt.core.dom.Type; |
|
|
70 |
import org.eclipse.jdt.core.dom.TypeDeclaration; |
| 67 |
import org.eclipse.jdt.core.dom.VariableDeclaration; |
71 |
import org.eclipse.jdt.core.dom.VariableDeclaration; |
| 68 |
import org.eclipse.jdt.core.dom.VariableDeclarationExpression; |
72 |
import org.eclipse.jdt.core.dom.VariableDeclarationExpression; |
| 69 |
import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
73 |
import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
| 70 |
import org.eclipse.jdt.core.dom.VariableDeclarationStatement; |
74 |
import org.eclipse.jdt.core.dom.VariableDeclarationStatement; |
| 71 |
import org.eclipse.jdt.core.dom.WhileStatement; |
75 |
import org.eclipse.jdt.core.dom.WhileStatement; |
|
Link Here
|
| 92 |
|
96 |
|
| 93 |
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; |
97 |
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; |
| 94 |
import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; |
98 |
import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; |
| 95 |
|
99 |
|
| 96 |
/* package */ class ExtractMethodAnalyzer extends CodeAnalyzer { |
100 |
/* package */ class ExtractMethodAnalyzer extends CodeAnalyzer { |
|
|
101 |
|
| 102 |
private static final class AccesibilityVisitor extends ASTVisitor { |
| 103 |
private static final class ClassFinder extends ASTVisitor { |
| 104 |
private final ITypeBinding fDeclaringClass; |
| 105 |
|
| 106 |
private boolean fFound= false; |
| 107 |
|
| 108 |
private ClassFinder(ITypeBinding declaringClass) { |
| 109 |
fDeclaringClass= declaringClass; |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public boolean visit(ClassInstanceCreation node) { |
| 114 |
if (node.resolveTypeBinding().isSubTypeCompatible(fDeclaringClass)) { |
| 115 |
fFound = true; |
| 116 |
return false; |
| 117 |
} |
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private ASTNode fDestination; |
| 123 |
private RefactoringStatus fStatus= new RefactoringStatus(); |
| 124 |
|
| 125 |
private ASTNode[] fSelectedNodes; |
| 126 |
|
| 127 |
public AccesibilityVisitor(ASTNode destination, ASTNode[] selectedNodes) { |
| 128 |
fDestination= destination; |
| 129 |
fSelectedNodes= selectedNodes; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public boolean visit(MethodInvocation node) { |
| 134 |
IMethodBinding resolvedMethodBinding= node.resolveMethodBinding(); |
| 135 |
if (!Modifier.isStatic(resolvedMethodBinding.getModifiers())) { |
| 136 |
Expression expression= node.getExpression(); |
| 137 |
if (expression != null) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
final ITypeBinding declaringClass= resolvedMethodBinding.getDeclaringClass(); |
| 141 |
ClassFinder classFinder= new ClassFinder(declaringClass); |
| 142 |
for (ASTNode selectedNode : fSelectedNodes) { |
| 143 |
selectedNode.accept(classFinder); |
| 144 |
if (classFinder.fFound) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
} |
| 148 |
if (fDestination.getNodeType() == ASTNode.TYPE_DECLARATION) { |
| 149 |
TypeDeclaration destination= (TypeDeclaration) fDestination; |
| 150 |
ITypeBinding binding= destination.resolveBinding(); |
| 151 |
if (binding != null && !declaringClass.isSubTypeCompatible(binding)) { |
| 152 |
fStatus.addFatalError( |
| 153 |
Messages.format( |
| 154 |
RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_accesibility, new Object[] { resolvedMethodBinding.getName(), binding.getName() })); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
public RefactoringStatus getStatus() { |
| 162 |
return fStatus; |
| 163 |
} |
| 164 |
} |
| 97 |
|
165 |
|
| 98 |
public static final int ERROR= -2; |
166 |
public static final int ERROR= -2; |
| 99 |
public static final int UNDEFINED= -1; |
167 |
public static final int UNDEFINED= -1; |
| 100 |
public static final int NO= 0; |
168 |
public static final int NO= 0; |
| 101 |
public static final int EXPRESSION= 1; |
169 |
public static final int EXPRESSION= 1; |
|
Link Here
|
| 319 |
ITypeBinding type= ASTNodes.getEnclosingType(destination); |
387 |
ITypeBinding type= ASTNodes.getEnclosingType(destination); |
| 320 |
status.merge(Checks.checkMethodInType(type, methodName, arguments)); |
388 |
status.merge(Checks.checkMethodInType(type, methodName, arguments)); |
| 321 |
status.merge(Checks.checkMethodInHierarchy(type.getSuperclass(), methodName, null, arguments)); |
389 |
status.merge(Checks.checkMethodInHierarchy(type.getSuperclass(), methodName, null, arguments)); |
| 322 |
} |
390 |
} |
| 323 |
|
391 |
|
|
|
392 |
public void checkAccesibility(RefactoringStatus result, ASTNode[] selectedNodes, ASTNode destination) { |
| 393 |
AccesibilityVisitor visitor= new AccesibilityVisitor(destination, selectedNodes); |
| 394 |
for (ASTNode node : selectedNodes) { |
| 395 |
node.accept(visitor); |
| 396 |
} |
| 397 |
result.merge(visitor.getStatus()); |
| 398 |
} |
| 399 |
|
| 324 |
private ITypeBinding[] getArgumentTypes() { |
400 |
private ITypeBinding[] getArgumentTypes() { |
| 325 |
ITypeBinding[] result= new ITypeBinding[fArguments.length]; |
401 |
ITypeBinding[] result= new ITypeBinding[fArguments.length]; |
| 326 |
for (int i= 0; i < fArguments.length; i++) { |
402 |
for (int i= 0; i < fArguments.length; i++) { |
| 327 |
result[i]= fArguments[i].getType(); |
403 |
result[i]= fArguments[i].getType(); |
| 328 |
} |
404 |
} |