introduce parameter: correct introduce parameter and remove unused from the enclosing method

This commit is contained in:
anna
2011-03-09 12:07:37 +01:00
parent f3f7fbb576
commit da1aab6e43
4 changed files with 83 additions and 4 deletions
@@ -110,7 +110,7 @@ public class OldReferenceResolver {
final JavaResolveResult adv = oldRef.advancedResolve(false);
final PsiElement scope = getClassContainingResolve(adv);
final PsiClass clss = PsiTreeUtil.getParentOfType(oldExpr, PsiClass.class);
if (clss != null && scope != null && PsiTreeUtil.isAncestor(clss, scope, false)) {
if (clss != null && scope != null ) {
final PsiElement subj = adv.getElement();
@@ -118,7 +118,6 @@ public class OldReferenceResolver {
// Parameters
if (subj instanceof PsiParameter) {
PsiParameterList parameterList = myMethodToReplaceIn.getParameterList();
PsiParameter[] parameters = parameterList.getParameters();
if (subj.getParent() != parameterList) return;
int index = parameterList.getParameterIndex((PsiParameter)subj);
@@ -133,7 +132,7 @@ public class OldReferenceResolver {
}
}
// "naked" field and methods (should become qualified)
else if ((subj instanceof PsiField || subj instanceof PsiMethod) && oldRef.getQualifierExpression() == null) {
else if ((subj instanceof PsiField || subj instanceof PsiMethod) && oldRef.getQualifierExpression() == null && PsiTreeUtil.isAncestor(clss, scope, false)) {
boolean isStatic = subj instanceof PsiField && ((PsiField)subj).hasModifierProperty(PsiModifier.STATIC) ||
subj instanceof PsiMethod && ((PsiMethod)subj).hasModifierProperty(PsiModifier.STATIC);
@@ -151,7 +150,7 @@ public class OldReferenceResolver {
}
}
if (subj instanceof PsiField) {
if (subj instanceof PsiField && PsiTreeUtil.isAncestor(clss, scope, false)) {
// probably replacing field with a getter
if (myReplaceFieldsWithGetters != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE) {
if (myReplaceFieldsWithGetters == IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL ||
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class AAAA {
void foo() {
sampleEnclosing("A" + "");
}
private void sampleEnclosing(final String anObject) {
new AAAA() {
void bar() {
System.out.println(anObject);
}
}.bar();
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class AAAA {
void foo() {
sampleEnclosing("A");
}
private void sampleEnclosing(final String s) {
new AAAA() {
void bar() {
System.out.println(<selection>s + ""</selection>);
}
}.bar();
}
}
@@ -28,6 +28,8 @@ import gnu.trove.TIntArrayList;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@TestDataPath("$CONTENT_ROOT/testData")
public class IntroduceParameterTest extends LightCodeInsightTestCase {
@Override
@@ -270,6 +272,12 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
}
public void testEnclosingWithParamDeletion() throws Exception {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
perform(true, 0, "anObject", false, true, true, false, 1);
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
}
private static boolean perform(boolean replaceAllOccurences,
int replaceFieldsWithGetters,
@NonNls String parameterName,
@@ -277,6 +285,18 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
boolean declareFinal,
final boolean removeUnusedParameters,
final boolean generateDelegate) {
return perform(replaceAllOccurences, replaceFieldsWithGetters, parameterName, searchForSuper, declareFinal, removeUnusedParameters,
generateDelegate, 0);
}
private static boolean perform(boolean replaceAllOccurences,
int replaceFieldsWithGetters,
@NonNls String parameterName,
boolean searchForSuper,
boolean declareFinal,
final boolean removeUnusedParameters,
final boolean generateDelegate,
int enclosingLevel) {
int startOffset = myEditor.getSelectionModel().getSelectionStart();
int endOffset = myEditor.getSelectionModel().getSelectionEnd();
@@ -294,6 +314,10 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
PsiMethod method = Util.getContainingMethod(context);
if (method == null) return false;
final List<PsiMethod> methods = IntroduceParameterHandler.getEnclosingMethods(method);
assertTrue(methods.size() > enclosingLevel);
method = methods.get(enclosingLevel);
final PsiMethod methodToSearchFor;
if (searchForSuper) {
methodToSearchFor = method.findDeepestSuperMethod();