From 6bf6d119d55653c19e491f4d11e4cbbcbedcc588 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 11 Mar 2014 15:30:25 +0100 Subject: [PATCH 1/2] Cleanup (pointless exceptions; typos; formatting) --- .../IntroduceParameterProcessor.java | 33 +- .../refactoring/IntroduceParameterTest.java | 697 +++++++++--------- 2 files changed, 372 insertions(+), 358 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java index 076734e90a7f..dc1f1668d744 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -13,15 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * Created by IntelliJ IDEA. - * User: dsl - * Date: 07.05.2002 - * Time: 11:17:31 - * To change template for new class use - * Code Style | Class Templates options (Tools | IDE Options). - */ package com.intellij.refactoring.introduceParameter; import com.intellij.codeInsight.ChangeContextUtil; @@ -62,6 +53,10 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Set; +/** + * @author dsl + * @since 07.05.2002 + */ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implements IntroduceParameterData { private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor"); @@ -72,7 +67,7 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem private final PsiLocalVariable myLocalVariable; private final boolean myRemoveLocalVariable; private final String myParameterName; - private final boolean myReplaceAllOccurences; + private final boolean myReplaceAllOccurrences; private int myReplaceFieldsWithGetters; private final boolean myDeclareFinal; @@ -94,7 +89,7 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem PsiLocalVariable localVariable, boolean removeLocalVariable, String parameterName, - boolean replaceAllOccurences, + boolean replaceAllOccurrences, int replaceFieldsWithGetters, boolean declareFinal, boolean generateDelegate, @@ -110,7 +105,7 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem myLocalVariable = localVariable; myRemoveLocalVariable = removeLocalVariable; myParameterName = parameterName; - myReplaceAllOccurences = replaceAllOccurences; + myReplaceAllOccurrences = replaceAllOccurrences; myReplaceFieldsWithGetters = replaceFieldsWithGetters; myDeclareFinal = declareFinal; myGenerateDelegate = generateDelegate; @@ -177,7 +172,7 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem } } - if (myReplaceAllOccurences) { + if (myReplaceAllOccurrences) { for (PsiElement expr : getOccurrences()) { result.add(new InternalUsageInfo(expr)); } @@ -245,8 +240,9 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem for (UsageInfo usageInfo : usagesIn) { if (!(usageInfo.getElement() instanceof PsiMethod) && !(usageInfo instanceof InternalUsageInfo)) { if (!PsiTreeUtil.isAncestor(myMethodToReplaceIn.getContainingClass(), usageInfo.getElement(), false)) { - conflicts.putValue(myParameterInitializer, RefactoringBundle.message("parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class", - CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER))); + String message = RefactoringBundle.message("parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class", + CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER)); + conflicts.putValue(myParameterInitializer, message); break; } } @@ -272,7 +268,8 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem if (usageInfo instanceof ExternalUsageInfo && IntroduceParameterUtil.isMethodUsage(usageInfo)) { final PsiElement place = usageInfo.getElement(); for (PsiElement element : result) { - if (element instanceof PsiField && myReplaceFieldsWithGetters != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE) { //check getter access instead + if (element instanceof PsiField && myReplaceFieldsWithGetters != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE) { + //check getter access instead final PsiClass psiClass = ((PsiField)element).getContainingClass(); LOG.assertTrue(psiClass != null); final PsiMethod method = psiClass.findMethodBySignature(PropertyUtil.generateGetterPrototype((PsiField)element), true); @@ -370,7 +367,7 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem myInitializerWrapper = new JavaExpressionWrapper(myParameterInitializer); - // Changing external occurences (the tricky part) + // Changing external occurrences (the tricky part) IntroduceParameterUtil.processUsages(usages, this); diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java index 8444c2b547c6..887b5908318a 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -13,15 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * Created by IntelliJ IDEA. - * User: dsl - * Date: 07.05.2002 - * Time: 13:59:01 - * To change template for new class use - * Code Style | Class Templates options (Tools | IDE Options). - */ package com.intellij.refactoring; import com.intellij.JavaTestUtil; @@ -38,6 +29,7 @@ import com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor; import com.intellij.refactoring.introduceParameter.Util; import com.intellij.refactoring.util.occurrences.ExpressionOccurrenceManager; import com.intellij.testFramework.TestDataPath; +import com.intellij.util.ObjectUtils; import gnu.trove.TIntArrayList; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -45,6 +37,10 @@ import org.jetbrains.annotations.Nullable; import java.util.List; +/** + * @author dsl + * @since 07.05.2002 + */ @TestDataPath("$CONTENT_ROOT/testData") public class IntroduceParameterTest extends LightRefactoringTestCase { @NotNull @@ -53,13 +49,313 @@ public class IntroduceParameterTest extends LightRefactoringTestCase { return JavaTestUtil.getJavaTestDataPath(); } - private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper, boolean declareFinal, - final boolean generateDelegate) throws Exception { + @Override + protected LanguageLevel getLanguageLevel() { + return LanguageLevel.JDK_1_7; + } + + public void testNoUsages() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testSimpleUsage() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testMethodWithoutParams() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testParameterSubstitution() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testThisSubstitution() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testThisSubstitutionInQualifier() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false, + "field Test.i is not accessible from method XTest.n(). " + + "Value for introduced parameter in that method call will be incorrect."); + } + + public void testFieldAccess() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testMethodAccess() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testStaticFieldAccess() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testFieldWithGetterReplacement() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, false, false, false, false); + } + + public void testFieldWithInaccessibleGetterReplacement() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testWeirdQualifier() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testSuperInExpression() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false, + "Parameter initializer contains super, but not all calls to method are in its class"); + } + + public void testNull() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testWeirdQualifierAndParameter() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testImplicitSuperCall() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testImplicitDefaultConstructor() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testLocalVarDeclaration() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testInternalSideEffect() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testQualifiedNew() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testAnonymousClass() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testSuperWithSideEffect() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false, + "Parameter initializer contains super, but not all calls to method are in its class"); + } + + public void testConflictingField() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, true, false, false); + } + + public void testParameterInFor() { + configureByFile("/refactoring/introduceParameter/beforeParameterInFor.java"); + performForLocal(true, true, true, false, false); + checkResultByFile("/refactoring/introduceParameter/afterParameterInFor.java"); + } + + public void testParameterJavaDoc1() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); + } + + public void testParameterJavaDoc2() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); + } + + public void testParameterJavaDoc3() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); + } + + public void testParameterJavaDocBeforeVararg() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); + } + + public void testIncompleteVararg() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, true, false, true, false, + "Incomplete call(method()): 2 parameters expected but only 0 found\n" + + "Incomplete call(method()): expected to delete the 0 parameter but only 0 parameters found"); + } + + public void testIncorrectScope() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, true, true, false); + } + + public void testExpectedType() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); + } + + public void testRemoveParameter() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); + } + + public void testRemoveParameterAfterVariable() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); + } + + public void testRemoveParameterInHierarchy() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); + } + + public void testRemoveParameterWithJavadoc() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); + } + + public void testVarargs() { // IDEADEV-16828 + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testVarargs1() { // IDEADEV-33555 + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); + } + + public void testUseInInnerClass() { + doTestThroughHandler(); + } + + public void testLocalVarSelection() { + doTestThroughHandler(); + } + + public void testGenerateDelegate() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, true); + } + + public void testGenerateDelegateRemoveParameter() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, true); + } + + public void testGenerateDelegateNoArgs() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, true); + } + + public void testEnums() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testMethodUsageInThisMethodInheritor() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + + public void testGetterQualifier() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); + } + + public void testArrayInitializer() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, true, false, false, false); + } + + public void testIncompleteEnumDefinition() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false, + "Incomplete call(Root()): 2 parameters expected but only 0 found\n" + + "Incomplete call(Root()): expected to delete the 1 parameter but only 0 parameters found"); + } + + public void testStaticFieldWithGetterReplacement() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testGenerateDelegateInSuperClass() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, true, true, true); + } + + public void testGenerateDelegateInSuperInterface() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, true, true, true); + } + + public void testReplaceAllAndDeleteUnused() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testDiamond2Raw() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testDiamondOccurrences() { + final LanguageLevel oldLevel = getLanguageLevel(); + try { + setLanguageLevel(LanguageLevel.JDK_1_7); + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + finally { + setLanguageLevel(oldLevel); + } + } + + public void testPreserveDiamondOccurrences() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testSubstituteTypeParams() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testDelegateWithVarargs() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, true); + } + + public void testSelfReference() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testSelfReferenceVarargs() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testSelfReferenceVarargs1() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testSimplifiedResultedType() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + public void testPackageReferenceShouldBeIgnored() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); + } + + private void doTestThroughHandler() { + configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java"); + boolean enabled = true; + try { + configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java"); + enabled = myEditor.getSettings().isVariableInplaceRenameEnabled(); + myEditor.getSettings().setVariableInplaceRenameEnabled(false); + new IntroduceParameterHandler().invoke(getProject(), myEditor, myFile, new DataContext() { + @Override + @Nullable + public Object getData(@NonNls final String dataId) { + return null; + } + }); + checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java"); + } + finally { + myEditor.getSettings().setVariableInplaceRenameEnabled(enabled); + } + } + + public void testEnclosingWithParamDeletion() { + configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java"); + perform(true, 0, "anObject", false, true, true, false, 1); + checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java"); + } + + private void doTest(int replaceFieldsWithGetters, + boolean removeUnusedParameters, + boolean searchForSuper, + boolean declareFinal, + boolean generateDelegate) { doTest(replaceFieldsWithGetters, removeUnusedParameters, searchForSuper, declareFinal, generateDelegate, null); } - private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper, boolean declareFinal, final boolean generateDelegate, - String conflict) throws Exception { + private void doTest(int replaceFieldsWithGetters, + boolean removeUnusedParameters, + boolean searchForSuper, + boolean declareFinal, + boolean generateDelegate, + String conflict) { boolean enabled = true; try { configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java"); @@ -81,322 +377,42 @@ public class IntroduceParameterTest extends LightRefactoringTestCase { } } - public void testNoUsages() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testSimpleUsage() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testMethodWithoutParams() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testParameterSubstitution() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testThisSubstitution() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testThisSubstitutionInQualifier() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false, "field Test.i is not accessible from method XTest.n(). Value for introduced parameter in that method call will be incorrect."); - } - - public void testFieldAccess() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testMethodAccess() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testStaticFieldAccess() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testFieldWithGetterReplacement() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, false, false, false, false); - } - - public void testFieldWithInaccessibleGetterReplacement() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testWeirdQualifier() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testSuperInExpression() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false, "Parameter initializer contains super, but not all calls to method are in its class"); - } - - public void testNull() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testWeirdQualifierAndParameter() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testImplicitSuperCall() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testImplicitDefaultConstructor() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testLocalVarDeclaration() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testInternalSideEffect() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testQualifiedNew() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testAnonymousClass() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testSuperWithSideEffect() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false, "Parameter initializer contains super, but not all calls to method are in its class"); - } - - public void testConflictingField() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, true, false, false); - } - - public void testParameterInFor() throws Exception { - configureByFile("/refactoring/introduceParameter/beforeParameterInFor.java"); - performForLocal(true, true, true, false, false); - checkResultByFile("/refactoring/introduceParameter/afterParameterInFor.java"); - } - - public void testParameterJavaDoc1() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); - } - - public void testParameterJavaDoc2() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); - } - - public void testParameterJavaDoc3() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); - } - - public void testParameterJavaDocBeforeVararg() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); - } - - public void testIncompleteVararg() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, true, false, true, false, "Incomplete call(method()): 2 parameters expected but only 0 found\n" + - "Incomplete call(method()): expected to delete the 0 parameter but only 0 parameters found"); - } - - public void testIncorrectScope() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, true, true, false); - } - - public void testExpectedType() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true, false); - } - - public void testRemoveParameter() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); - } - - public void testRemoveParameterAfterVariable() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); - } - - public void testRemoveParameterInHierarchy() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); - } - - public void testRemoveParameterWithJavadoc() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); - } - - public void testVarargs() throws Exception { // IDEADEV-16828 - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testVarargs1() throws Exception { // IDEADEV-33555 - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false); - } - - public void testUseInInnerClass() throws Exception { - doTestThroughHandler(); - } - - public void testLocalVarSelection() throws Exception { - doTestThroughHandler(); - } - - public void testGenerateDelegate() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, true); - } - - public void testGenerateDelegateRemoveParameter() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, true); - } - - public void testGenerateDelegateNoArgs() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, true); - } - - public void testEnums() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testMethodUsageInThisMethodInheritor() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); - } - - public void testGetterQualifier() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false); - } - - public void testArrayInitializer() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, true, false, false, false); - } - - public void testIncompleteEnumDefinition() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false, "Incomplete call(Root()): 2 parameters expected but only 0 found\n" + - "Incomplete call(Root()): expected to delete the 1 parameter but only 0 parameters found"); - } - - public void testStaticFieldWithGetterReplacement() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testGenerateDelegateInSuperClass() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, true, true, true); - } - - public void testGenerateDelegateInSuperInterface() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, true, true, true); - } - - public void testReplaceAllAndDeleteUnused() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testDiamond2Raw() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testDiamondOccurrences() throws Exception { - final LanguageLevel oldLevel = getLanguageLevel(); - try { - setLanguageLevel(LanguageLevel.JDK_1_7); - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - finally { - setLanguageLevel(oldLevel); - } - } - - public void testPreserveDiamondOccurrences() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testSubstituteTypeParams() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testDelegateWithVarargs() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, true); - } - - public void testSelfReference() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testSelfReferenceVarargs() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testSelfReferenceVarargs1() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testSimplifiedResultedType() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - public void testPackageReferenceShouldBeIgnored() throws Exception { - doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false); - } - - private void doTestThroughHandler() throws Exception { - configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java"); - boolean enabled = true; - try { - configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java"); - enabled = myEditor.getSettings().isVariableInplaceRenameEnabled(); - myEditor.getSettings().setVariableInplaceRenameEnabled(false); - new IntroduceParameterHandler().invoke(getProject(), myEditor, myFile, new DataContext() { - @Override - @Nullable - public Object getData(@NonNls final String dataId) { - return null; - } - }); - checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java"); - } - finally { - myEditor.getSettings().setVariableInplaceRenameEnabled(enabled); - } - } - - 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, + private static boolean perform(boolean replaceAllOccurrences, int replaceFieldsWithGetters, @NonNls String parameterName, boolean searchForSuper, boolean declareFinal, - final boolean removeUnusedParameters, - final boolean generateDelegate) { - return perform(replaceAllOccurences, replaceFieldsWithGetters, parameterName, searchForSuper, declareFinal, removeUnusedParameters, - generateDelegate, 0); + boolean removeUnusedParameters, + boolean generateDelegate) { + return perform( + replaceAllOccurrences, replaceFieldsWithGetters, parameterName, searchForSuper, declareFinal, + removeUnusedParameters, generateDelegate, 0 + ); } - private static boolean perform(boolean replaceAllOccurences, + private static boolean perform(boolean replaceAllOccurrences, int replaceFieldsWithGetters, @NonNls String parameterName, boolean searchForSuper, boolean declareFinal, - final boolean removeUnusedParameters, - final boolean generateDelegate, + boolean removeUnusedParameters, + boolean generateDelegate, int enclosingLevel) { final ElementToWorkOn[] elementToWorkOn = new ElementToWorkOn[1]; - ElementToWorkOn - .processElementToWorkOn(myEditor, myFile, "INtr param", HelpID.INTRODUCE_PARAMETER, getProject(), new ElementToWorkOn.ElementsProcessor() { - @Override - public boolean accept(ElementToWorkOn el) { - return true; - } + ElementToWorkOn.processElementToWorkOn(myEditor, myFile, "INtr param", HelpID.INTRODUCE_PARAMETER, getProject(), + new ElementToWorkOn.ElementsProcessor() { + @Override + public boolean accept(ElementToWorkOn el) { + return true; + } - @Override - public void pass(final ElementToWorkOn e) { - if (e == null) return; - - elementToWorkOn[0] = e; - } - }); + @Override + public void pass(final ElementToWorkOn e) { + if (e != null) { + elementToWorkOn[0] = e; + } + } + }); final PsiExpression expr = elementToWorkOn[0].getExpression(); final PsiLocalVariable localVar = elementToWorkOn[0].getLocalVariable(); @@ -411,41 +427,46 @@ public class IntroduceParameterTest extends LightRefactoringTestCase { final PsiMethod methodToSearchFor; if (searchForSuper) { - methodToSearchFor = method.findDeepestSuperMethod(); + methodToSearchFor = method.findDeepestSuperMethods()[0]; } else { methodToSearchFor = method; } - PsiExpression[] occurences; + PsiExpression[] occurrences; PsiExpression initializer; if (expr == null) { initializer = localVar.getInitializer(); - occurences = CodeInsightUtil.findReferenceExpressions(method, localVar); + assertNotNull(initializer); + occurrences = CodeInsightUtil.findReferenceExpressions(method, localVar); } else { initializer = expr; - occurences = new ExpressionOccurrenceManager(expr, method, null).findExpressionOccurrences(); + occurrences = new ExpressionOccurrenceManager(expr, method, null).findExpressionOccurrences(); } - TIntArrayList parametersToRemove = removeUnusedParameters ? Util.findParametersToRemove(method, initializer, occurences) : new TIntArrayList(); + TIntArrayList parametersToRemove = removeUnusedParameters ? Util.findParametersToRemove(method, initializer, occurrences) + : new TIntArrayList(); new IntroduceParameterProcessor( - getProject(), method, methodToSearchFor, initializer, expr, localVar, true, parameterName, replaceAllOccurences, - replaceFieldsWithGetters, - declareFinal, generateDelegate, null, parametersToRemove).run(); + getProject(), method, methodToSearchFor, initializer, expr, localVar, true, parameterName, replaceAllOccurrences, + replaceFieldsWithGetters, declareFinal, generateDelegate, null, parametersToRemove + ).run(); myEditor.getSelectionModel().removeSelection(); return true; } - private static void performForLocal(boolean searchForSuper, boolean removeLocalVariable, boolean replaceAllOccurrences, boolean declareFinal, + private static void performForLocal(boolean searchForSuper, + boolean removeLocalVariable, + boolean replaceAllOccurrences, + boolean declareFinal, final boolean removeUnusedParameters) { final int offset = myEditor.getCaretModel().getOffset(); - final PsiElement element = myFile.findElementAt(offset).getParent(); + final PsiElement element = ObjectUtils.assertNotNull(myFile.findElementAt(offset)).getParent(); assertTrue(element instanceof PsiLocalVariable); PsiMethod method = Util.getContainingMethod(element); final PsiMethod methodToSearchFor; if (searchForSuper) { - final PsiMethod deepestSuperMethod = method.findDeepestSuperMethod(); - methodToSearchFor = deepestSuperMethod != null ? deepestSuperMethod : method; + PsiMethod[] deepestSuperMethods = method.findDeepestSuperMethods(); + methodToSearchFor = deepestSuperMethods.length > 0 ? deepestSuperMethods[0] : method; } else { methodToSearchFor = method; @@ -454,18 +475,14 @@ public class IntroduceParameterTest extends LightRefactoringTestCase { assertNotNull(methodToSearchFor); final PsiLocalVariable localVariable = (PsiLocalVariable)element; final PsiExpression parameterInitializer = localVariable.getInitializer(); - TIntArrayList parametersToRemove = removeUnusedParameters ? Util.findParametersToRemove(method, parameterInitializer, null) : new TIntArrayList(); + assertNotNull(parameterInitializer); + TIntArrayList parametersToRemove = removeUnusedParameters ? Util.findParametersToRemove(method, parameterInitializer, null) + : new TIntArrayList(); new IntroduceParameterProcessor( - getProject(), method, methodToSearchFor, - parameterInitializer, null, localVariable, removeLocalVariable, - localVariable.getName(), replaceAllOccurrences, - IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, - declareFinal, false, null, parametersToRemove).run(); - } - - @Override - protected LanguageLevel getLanguageLevel() { - return LanguageLevel.JDK_1_7; + getProject(), method, methodToSearchFor, parameterInitializer, null, localVariable, removeLocalVariable, + localVariable.getName(), replaceAllOccurrences, IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, + declareFinal, false, null, parametersToRemove + ).run(); } } From f05869508f0eddf9708351e35425d30cdd9b788e Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 11 Mar 2014 17:25:13 +0100 Subject: [PATCH 2/2] IDEA-54387 (type annotations in introduce parameter) --- .../intellij/psi/impl/PsiElementFactoryImpl.java | 14 +++++++------- .../introduceParameter/afterTypeAnnotation.java | 14 ++++++++++++++ .../introduceParameter/beforeTypeAnnotation.java | 15 +++++++++++++++ .../refactoring/IntroduceParameterTest.java | 4 ++++ 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 java/java-tests/testData/refactoring/introduceParameter/afterTypeAnnotation.java create mode 100644 java/java-tests/testData/refactoring/introduceParameter/beforeTypeAnnotation.java diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java index 68c29ec537e0..0515b4983ce2 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java @@ -165,7 +165,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps final PsiClassType.ClassResolveResult resolveResult = type.resolveGenerics(); final PsiClass refClass = resolveResult.getElement(); assert refClass != null : type; - return new LightClassReference(myManager, type.getCanonicalText(), refClass, resolveResult.getSubstitutor()); + return new LightClassReference(myManager, type.getCanonicalText(true), refClass, resolveResult.getSubstitutor()); } @NotNull @@ -186,7 +186,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps builder.append(" extends "); for (PsiClassType type : superTypes) { if (type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) continue; - builder.append(type.getCanonicalText()).append('&'); + builder.append(type.getCanonicalText(true)).append('&'); } builder.delete(builder.length() - 1, builder.length()); @@ -208,7 +208,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps throw new IncorrectOperationException("Cannot create field with type \"null\"."); } - @NonNls final String text = "class _Dummy_ { private " + type.getCanonicalText() + " " + name + "; }"; + @NonNls final String text = "class _Dummy_ { private " + type.getCanonicalText(true) + " " + name + "; }"; final PsiJavaFile aFile = createDummyJavaFile(text); final PsiClass[] classes = aFile.getClasses(); if (classes.length < 1) { @@ -232,7 +232,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps throw new IncorrectOperationException("Cannot create method with type \"null\"."); } - final String canonicalText = returnType.getCanonicalText(); + final String canonicalText = returnType.getCanonicalText(true); final PsiJavaFile aFile = createDummyJavaFile("class _Dummy_ { public " + canonicalText + " " + name + "() {} }"); final PsiClass[] classes = aFile.getClasses(); if (classes.length < 1) { @@ -282,7 +282,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps throw new IncorrectOperationException("Cannot create parameter with type \"null\"."); } - final String text = type.getCanonicalText() + " " + name; + final String text = type.getCanonicalText(true) + " " + name; PsiParameter parameter = createParameterFromText(text, null); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject()); PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, @@ -480,7 +480,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps builder.append("void method("); for (int i = 0; i < names.length; i++) { if (i > 0) builder.append(", "); - builder.append(types[i].getCanonicalText()).append(' ').append(names[i]); + builder.append(types[i].getCanonicalText(true)).append(' ').append(names[i]); } builder.append(");"); return createMethodFromText(builder.toString(), null).getParameterList(); @@ -742,7 +742,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps throw new IncorrectOperationException("Unexpected type:" + exceptionType); } - @NonNls final String text = "catch (" + exceptionType.getCanonicalText() + " " + exceptionName + ") {}"; + @NonNls final String text = "catch (" + exceptionType.getCanonicalText(true) + " " + exceptionName + ") {}"; final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, CATCH_SECTION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiCatchSection)) { diff --git a/java/java-tests/testData/refactoring/introduceParameter/afterTypeAnnotation.java b/java/java-tests/testData/refactoring/introduceParameter/afterTypeAnnotation.java new file mode 100644 index 000000000000..a14b989bbc89 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceParameter/afterTypeAnnotation.java @@ -0,0 +1,14 @@ +import java.lang.annotation.*; + +@Target(value = ElementType.TYPE_USE) +public @interface TA { } + +class Test { + void m(@TA String anObject) { + System.out.println(anObject); + } + + void use() { + m("smth"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceParameter/beforeTypeAnnotation.java b/java/java-tests/testData/refactoring/introduceParameter/beforeTypeAnnotation.java new file mode 100644 index 000000000000..949419221d40 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceParameter/beforeTypeAnnotation.java @@ -0,0 +1,15 @@ +import java.lang.annotation.*; + +@Target(value = ElementType.TYPE_USE) +public @interface TA { } + +class Test { + void m() { + @TA String v = "smth"; + System.out.println(v); + } + + void use() { + m(); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java index 887b5908318a..0423d0939242 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java @@ -342,6 +342,10 @@ public class IntroduceParameterTest extends LightRefactoringTestCase { checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java"); } + public void testTypeAnnotation() { + doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false); + } + private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper,