diff --git a/java/java-impl/src/com/intellij/refactoring/introduceParameter/OldReferenceResolver.java b/java/java-impl/src/com/intellij/refactoring/introduceParameter/OldReferenceResolver.java index efd4672e9f5e..1ca50aad8094 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceParameter/OldReferenceResolver.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceParameter/OldReferenceResolver.java @@ -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 || diff --git a/java/java-tests/testData/refactoring/introduceParameter/afterEnclosingWithParamDeletion.java b/java/java-tests/testData/refactoring/introduceParameter/afterEnclosingWithParamDeletion.java new file mode 100644 index 000000000000..1216026ab11f --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceParameter/afterEnclosingWithParamDeletion.java @@ -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(); + } +} diff --git a/java/java-tests/testData/refactoring/introduceParameter/beforeEnclosingWithParamDeletion.java b/java/java-tests/testData/refactoring/introduceParameter/beforeEnclosingWithParamDeletion.java new file mode 100644 index 000000000000..231920153098 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceParameter/beforeEnclosingWithParamDeletion.java @@ -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(s + ""); + } + }.bar(); + } +} diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java index c193eeae4d9e..30481aacf6ca 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java @@ -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 methods = IntroduceParameterHandler.getEnclosingMethods(method); + assertTrue(methods.size() > enclosingLevel); + method = methods.get(enclosingLevel); + final PsiMethod methodToSearchFor; if (searchForSuper) { methodToSearchFor = method.findDeepestSuperMethod();