From 3251ee29da8687c1128267262cc6cbf6583f97c9 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 25 Feb 2015 19:02:20 +0100 Subject: [PATCH] extract method with last declaration statement fixed (EA-64510 - IOE: PsiJavaCodeReferenceElementImpl.cannotBindError) --- .../extractMethod/ExtractMethodProcessor.java | 12 ++++++-- .../extractMethod/NonPhysicalAssumptions.java | 26 ++++++++++++++++ .../NonPhysicalAssumptions_after.java | 30 +++++++++++++++++++ .../refactoring/ExtractMethodTest.java | 4 +++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions.java create mode 100644 java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions_after.java diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java index daa1950d94d2..82aa58e8535e 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java @@ -326,7 +326,8 @@ public class ExtractMethodProcessor implements MatchProvider { elements = ArrayUtil.append(myElements, ((PsiMethod)myCodeFragmentMember).getReturnTypeElement(), PsiElement.class); } } - myTypeParameterList = RefactoringUtil.createTypeParameterListWithUsedTypeParameters(((PsiMethod)container).getTypeParameterList(), elements); + myTypeParameterList = RefactoringUtil.createTypeParameterListWithUsedTypeParameters(((PsiMethod)container).getTypeParameterList(), + elements); } List exceptions = ExceptionUtil.getThrownCheckedExceptions(myElements); myThrownExceptions = exceptions.toArray(new PsiClassType[exceptions.size()]); @@ -633,12 +634,17 @@ public class ExtractMethodProcessor implements MatchProvider { private Nullness initNullness() { if (!PsiUtil.isLanguageLevel5OrHigher(myElements[0]) || PsiUtil.resolveClassInType(myReturnType) == null) return null; - final PsiMethod emptyMethod = (PsiMethod)myTargetClass.copy().add(generateEmptyMethod("name")); - prepareMethodBody(emptyMethod, false); final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject); final PsiClass nullableAnnotationClass = JavaPsiFacade.getInstance(myProject) .findClass(manager.getDefaultNullable(), myElements[0].getResolveScope()); if (nullableAnnotationClass != null) { + final PsiElement elementInCopy = myTargetClass.getContainingFile().copy().findElementAt(myTargetClass.getTextOffset()); + final PsiClass classCopy = PsiTreeUtil.getParentOfType(elementInCopy, PsiClass.class); + if (classCopy == null) { + return null; + } + final PsiMethod emptyMethod = (PsiMethod)classCopy.add(generateEmptyMethod("name")); + prepareMethodBody(emptyMethod, false); if (myNotNullConditionalCheck || myNullConditionalCheck) { return Nullness.NULLABLE; } diff --git a/java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions.java b/java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions.java new file mode 100644 index 000000000000..1ba26fc99846 --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions.java @@ -0,0 +1,26 @@ +class Test { + { + UrlClassLoader loader = UrlClassLoader.build(). + useCache(new UrlClassLoader.I() { + @Override + public void m() {} + }).get(); + + System.out.println(loader); + } + + static class UrlClassLoader { + static UrlClassLoader build() {return new UrlClassLoader();} + UrlClassLoader useCache(I i) { + return this; + } + + interface I { + void m(); + } + + UrlClassLoader get() { + return this; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions_after.java b/java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions_after.java new file mode 100644 index 000000000000..90f2fa01ca54 --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/NonPhysicalAssumptions_after.java @@ -0,0 +1,30 @@ +class Test { + { + UrlClassLoader loader = newMethod(); + + System.out.println(loader); + } + + private UrlClassLoader newMethod() { + return UrlClassLoader.build(). + useCache(new UrlClassLoader.I() { + @Override + public void m() {} + }).get(); + } + + static class UrlClassLoader { + static UrlClassLoader build() {return new UrlClassLoader();} + UrlClassLoader useCache(I i) { + return this; + } + + interface I { + void m(); + } + + UrlClassLoader get() { + return this; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java b/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java index cf82ecbf0818..1a2b9a6c9b1e 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java @@ -450,6 +450,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase { } } + public void testNonPhysicalAssumptions() throws Exception { + doTest(); + } + public void testNullableCheck() throws Exception { doTest(); }