diff --git a/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java b/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java index b9b5f7fd9d9..29d83c8e17b 100644 --- a/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java +++ b/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java @@ -9,6 +9,9 @@ import org.junit.Test; import spoon.Launcher; +import spoon.OutputType; +import spoon.refactoring.CtParameterRemoveRefactoring; +import spoon.refactoring.RefactoringException; import spoon.reflect.code.CtComment; import spoon.reflect.code.CtLambda; import spoon.reflect.declaration.CtClass; @@ -59,6 +62,7 @@ public void testSubInheritanceHierarchyFunction() { "spoon.test.refactoring.parameter.testclasses.TypeB", "spoon.test.refactoring.parameter.testclasses.TypeB$1", "spoon.test.refactoring.parameter.testclasses.TypeB$1Local", + "spoon.test.refactoring.parameter.testclasses.TypeB$2", "spoon.test.refactoring.parameter.testclasses.TypeC", "spoon.test.refactoring.parameter.testclasses.IFaceL", "spoon.test.refactoring.parameter.testclasses.TypeL", @@ -202,4 +206,56 @@ private void forEachMethodOfHierarchy(Factory factory, final String hierarchyNam return false; }).forEach(consumer); } + + @Test + public void testCtParameterRemoveRefactoring() { + String testPackagePath = "spoon/test/refactoring/parameter/testclasses"; + Factory factory = ModelUtils.build(new File("./src/test/java/"+testPackagePath)); + Launcher launcher = ModelUtils.getLastLauncher(); + + CtType typeA = factory.Class().get(TypeA.class); + + CtMethod methodTypeA_method1 = typeA.getMethodsByName("method1").get(0); + CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring().setTarget(methodTypeA_method1.getParameters().get(0)); + refactor.setTarget(methodTypeA_method1.getParameters().get(0)); + //check that expected methods are targets of refactoring + List> execs = refactor.getTargetExecutables(); + execs.forEach(exec->{ + //check that each to be modified method has one parameter + assertEquals(1, exec.getParameters().size()); + }); + refactor.refactor(); + execs.forEach(exec->{ + //check that each to be modified method has no parameter after refactoring + assertEquals(0, exec.getParameters().size()); + }); + launcher.setSourceOutputDirectory(new File("./target/spooned/")); + launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES); + ModelUtils.canBeBuilt("./target/spooned/"+testPackagePath, 8); + } + @Test + public void testCtParameterRemoveRefactoringValidationCheck() { + String testPackagePath = "spoon/test/refactoring/parameter/testclasses"; + Factory factory = ModelUtils.build(new File("./src/test/java/"+testPackagePath)); + Launcher launcher = ModelUtils.getLastLauncher(); + + CtType typeR = factory.Class().get(TypeR.class); + + CtMethod methodTypeR_method1 = typeR.getMethodsByName("method1").get(0); + CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring().setTarget(methodTypeR_method1.getParameters().get(0)); + refactor.setTarget(methodTypeR_method1.getParameters().get(0)); + //check that each to be refactored method has one parameter + List> execs = refactor.getTargetExecutables(); + execs.forEach(exec->{ + //check that each to be modified method has one parameter + assertEquals(1, exec.getParameters().size()); + }); + //try refactor + try { + refactor.refactor(); + fail(); + } catch (RefactoringException e) { + this.getClass(); + } + } } \ No newline at end of file diff --git a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java index 6e42874d80b..beb35f693ce 100644 --- a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java +++ b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java @@ -33,4 +33,13 @@ public void method1(Double p1) { } } } + private void anMethodWithNullParameterValue() { + IFaceB ifaceB = new IFaceB() { + @Override + @TestHierarchy("A_method1") + public void method1(String p1) { + } + }; + ifaceB.method1(null); + } } diff --git a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java index a1daa30c40f..7b08139fb7d 100644 --- a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java +++ b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java @@ -15,4 +15,13 @@ private void methodWithLambdaOf_R() { IFaceT ifaceT = /*R_method1*/ p->{}; ifaceT.method1(1.0); } + + private void methodWithComplexExpression_R() { + IFaceT ifaceT = /*R_method1*/ p->{}; + /* + * the refactoring should by default report calling of method as problem, because it cannot know, + * whether that method call has side effects. Such method call remove might cause malfunction. + */ + ifaceT.method1(Math.abs(1.0)); + } }