From 4cf5d1beb7fbcb781ce9bf04c0864f1785f5b684 Mon Sep 17 00:00:00 2001 From: Eugene Vigdorchik Date: Thu, 17 Feb 2005 16:12:12 +0300 Subject: [PATCH] (no message) --- .../ExceptionSimple.java | 12 ++ .../ExceptionSimple.java.after | 16 +++ .../ExceptionWithOverriding.java | 18 +++ .../ExceptionWithOverriding.java.after | 22 ++++ .../ParamSimple.java | 12 ++ .../ParamSimple.java.after | 12 ++ .../ParamWithOverriding.java | 16 +++ .../ParamWithOverriding.java.after | 20 ++++ .../ChangeSignaturePropagationTest.java | 103 ++++++++++++++++++ 9 files changed, 231 insertions(+) create mode 100644 testData/refactoring/changeSignaturePropagation/ExceptionSimple.java create mode 100644 testData/refactoring/changeSignaturePropagation/ExceptionSimple.java.after create mode 100644 testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java create mode 100644 testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java.after create mode 100644 testData/refactoring/changeSignaturePropagation/ParamSimple.java create mode 100644 testData/refactoring/changeSignaturePropagation/ParamSimple.java.after create mode 100644 testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java create mode 100644 testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java.after create mode 100644 testSource/com/intellij/refactoring/ChangeSignaturePropagationTest.java diff --git a/testData/refactoring/changeSignaturePropagation/ExceptionSimple.java b/testData/refactoring/changeSignaturePropagation/ExceptionSimple.java new file mode 100644 index 000000000000..6ac7602f178a --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ExceptionSimple.java @@ -0,0 +1,12 @@ +class ChangeSignatureTest { + void foo() { + } + + void bar() { + foo(); + } + + { + bar(); + } +} \ No newline at end of file diff --git a/testData/refactoring/changeSignaturePropagation/ExceptionSimple.java.after b/testData/refactoring/changeSignaturePropagation/ExceptionSimple.java.after new file mode 100644 index 000000000000..ac867389f0f2 --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ExceptionSimple.java.after @@ -0,0 +1,16 @@ +class ChangeSignatureTest { + void foo() throws Exception { + } + + void bar() throws Exception { + foo(); + } + + { + try { + bar(); + } catch (Exception e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } +} \ No newline at end of file diff --git a/testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java b/testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java new file mode 100644 index 000000000000..bf3fb2f52e4c --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java @@ -0,0 +1,18 @@ +class ChangeSignatureTest { + void foo() { + } + + void bar() { + foo(); + } + + { + bar(); + } +} + +class Derived extends ChangeSignatureTest { + void bar () { + super.bar(); + } +} \ No newline at end of file diff --git a/testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java.after b/testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java.after new file mode 100644 index 000000000000..2b33e2495065 --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ExceptionWithOverriding.java.after @@ -0,0 +1,22 @@ +class ChangeSignatureTest { + void foo() throws Exception { + } + + void bar() throws Exception { + foo(); + } + + { + try { + bar(); + } catch (Exception e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } +} + +class Derived extends ChangeSignatureTest { + void bar () throws Exception { + super.bar(); + } +} \ No newline at end of file diff --git a/testData/refactoring/changeSignaturePropagation/ParamSimple.java b/testData/refactoring/changeSignaturePropagation/ParamSimple.java new file mode 100644 index 000000000000..c5f0fcda0c6d --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ParamSimple.java @@ -0,0 +1,12 @@ +class ChangeSignatureTest { + public ChangeSignatureTest() { + } + + void foo() { + new ChangeSignatureTest(); + } + + { + foo(); + } +} diff --git a/testData/refactoring/changeSignaturePropagation/ParamSimple.java.after b/testData/refactoring/changeSignaturePropagation/ParamSimple.java.after new file mode 100644 index 000000000000..f56c94564d37 --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ParamSimple.java.after @@ -0,0 +1,12 @@ +class ChangeSignatureTest { + public ChangeSignatureTest(Class clazz) { + } + + void foo(Class clazz) { + new ChangeSignatureTest(clazz); + } + + { + foo(null); + } +} diff --git a/testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java b/testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java new file mode 100644 index 000000000000..bea89d7968e3 --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java @@ -0,0 +1,16 @@ +class ChangeSignatureTest { + public ChangeSignatureTest() { + } + + void foo() { + new ChangeSignatureTest(); + } + + { + foo(); + } +} + +class Derived extends ChangeSignatureTest { + void foo () {} +} \ No newline at end of file diff --git a/testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java.after b/testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java.after new file mode 100644 index 000000000000..087eb3ac1e96 --- /dev/null +++ b/testData/refactoring/changeSignaturePropagation/ParamWithOverriding.java.after @@ -0,0 +1,20 @@ +class ChangeSignatureTest { + public ChangeSignatureTest(Class clazz) { + } + + void foo(Class clazz) { + new ChangeSignatureTest(clazz); + } + + { + foo(null); + } +} + +class Derived extends ChangeSignatureTest { + Derived() { + super(null); + } + + void foo(Class clazz) {} +} \ No newline at end of file diff --git a/testSource/com/intellij/refactoring/ChangeSignaturePropagationTest.java b/testSource/com/intellij/refactoring/ChangeSignaturePropagationTest.java new file mode 100644 index 000000000000..c15681daa6d4 --- /dev/null +++ b/testSource/com/intellij/refactoring/ChangeSignaturePropagationTest.java @@ -0,0 +1,103 @@ +package com.intellij.refactoring; + +import com.intellij.codeInsight.CodeInsightTestCase; +import com.intellij.codeInsight.TargetElementUtil; +import com.intellij.psi.*; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.refactoring.changeSignature.ChangeSignatureProcessor; +import com.intellij.refactoring.changeSignature.ParameterInfo; +import com.intellij.refactoring.changeSignature.ThrownExceptionInfo; +import com.intellij.refactoring.util.CanonicalTypes; +import com.intellij.util.containers.HashSet; + +import java.util.Set; +import java.util.Arrays; + +/** + * @author ven + */ +public class ChangeSignaturePropagationTest extends CodeInsightTestCase { + public void testParamSimple() throws Exception { + parameterPropagationTest(); + } + + public void testParamWithOverriding() throws Exception { + parameterPropagationTest(); + } + + public void testExceptionSimple() throws Exception { + exceptionPropagationTest(); + } + + public void testExceptionWithOverriding() throws Exception { + exceptionPropagationTest(); + } + + private void parameterPropagationTest() throws Exception { + PsiMethod method = getPrimaryMethod(); + PsiClass aClass = method.getContainingClass(); + PsiType newParamType = myPsiManager.getElementFactory().createTypeByFQClassName("java.lang.Class", GlobalSearchScope.allScope(myProject)); + final ParameterInfo[] newParameters = new ParameterInfo[]{new ParameterInfo(-1, "clazz", newParamType, "null")}; + final Set methodsToPropagateParameters = new HashSet(Arrays.asList(aClass.getMethods())); + doTest(newParameters, new ThrownExceptionInfo[0], methodsToPropagateParameters, null, method); + } + + private void exceptionPropagationTest() throws Exception { + PsiMethod method = getPrimaryMethod(); + PsiClass aClass = method.getContainingClass(); + PsiClassType newExceptionType = myPsiManager.getElementFactory().createTypeByFQClassName("java.lang.Exception", GlobalSearchScope.allScope(myProject)); + final ThrownExceptionInfo[] newExceptions = new ThrownExceptionInfo[]{new ThrownExceptionInfo(-1, newExceptionType)}; + final Set methodsToPropagateExceptions = new HashSet(Arrays.asList(aClass.getMethods())); + doTest(new ParameterInfo[0], newExceptions, null, methodsToPropagateExceptions, method); + } + + private void doTest(ParameterInfo[] newParameters, + final ThrownExceptionInfo[] newExceptions, + Set methodsToPropagateParameterChanges, + Set methodsToPropagateExceptionChanges, + PsiMethod primaryMethod) throws Exception { + final String filePath = getBasePath() + getTestName(false) + ".java"; + final PsiType returnType = primaryMethod.getReturnType(); + final CanonicalTypes.Type type = returnType == null ? null : CanonicalTypes.createTypeWrapper(returnType); + new ChangeSignatureProcessor(myProject, primaryMethod, false, null, + primaryMethod.getName(), + type, + generateParameterInfos(primaryMethod, newParameters), + generateExceptionInfos(primaryMethod, newExceptions), + methodsToPropagateParameterChanges, + methodsToPropagateExceptionChanges).testRun(); + checkResultByFile(filePath + ".after"); + } + + private PsiMethod getPrimaryMethod() throws Exception { + final String filePath = getBasePath() + getTestName(false) + ".java"; + configureByFile(filePath); + final PsiElement targetElement = TargetElementUtil.findTargetElement(myEditor, TargetElementUtil.ELEMENT_NAME_ACCEPTED); + assertTrue(" is not on method name", targetElement instanceof PsiMethod); + return (PsiMethod) targetElement; + } + + private String getBasePath() { + return "/refactoring/changeSignaturePropagation/"; + } + + private ParameterInfo[] generateParameterInfos (PsiMethod method, ParameterInfo[] newParameters) { + final PsiParameter[] parameters = method.getParameterList().getParameters(); + ParameterInfo[] result = new ParameterInfo[parameters.length + newParameters.length]; + for (int i = 0; i < parameters.length; i++) { + result[i] = new ParameterInfo(i); + } + System.arraycopy(newParameters, 0, result, parameters.length, newParameters.length); + return result; + } + + private ThrownExceptionInfo[] generateExceptionInfos (PsiMethod method, ThrownExceptionInfo[] newExceptions) { + final PsiClassType[] exceptions = method.getThrowsList().getReferencedTypes(); + ThrownExceptionInfo[] result = new ThrownExceptionInfo[exceptions.length + newExceptions.length]; + for (int i = 0; i < exceptions.length; i++) { + result[i] = new ThrownExceptionInfo(i); + } + System.arraycopy(newExceptions, 0, result, exceptions.length, newExceptions.length); + return result; + } +}