diff --git a/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java b/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java index a8b8f227839..1fa8e19526b 100644 --- a/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java +++ b/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java @@ -12,9 +12,12 @@ import org.junit.Test; +import spoon.Launcher; +import spoon.OutputType; +import spoon.refactoring.CtParameterRemoveRefactoring; +import spoon.refactoring.RefactoringException; import spoon.reflect.code.CtLambda; import spoon.reflect.code.CtStatement; -import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtConstructor; import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtExecutable; @@ -24,7 +27,6 @@ import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction; import spoon.reflect.visitor.filter.ExecutableReferenceFilter; -import spoon.reflect.visitor.filter.NameFilter; import spoon.reflect.visitor.filter.SubInheritanceHierarchyFunction; import spoon.reflect.visitor.filter.TypeFilter; import spoon.test.refactoring.parameter.testclasses.IFaceB; @@ -34,9 +36,7 @@ import spoon.test.refactoring.parameter.testclasses.TypeA; import spoon.test.refactoring.parameter.testclasses.TypeB; import spoon.test.refactoring.parameter.testclasses.TypeC; -import spoon.test.refactoring.parameter.testclasses.TypeL; import spoon.test.refactoring.parameter.testclasses.TypeR; -import spoon.test.refactoring.parameter.testclasses.TypeS; import spoon.testing.utils.ModelUtils; public class MethodsRefactoringTest { @@ -64,6 +64,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", @@ -300,4 +301,56 @@ private boolean removeSame(Collection list, Object item) { } return false; } -} \ No newline at end of file + + @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(); + } + } +} 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 c6650cc779a..9ce22799e09 100644 --- a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java +++ b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java @@ -39,4 +39,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 d0bd3321156..ab225c9c900 100644 --- a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java +++ b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java @@ -21,4 +21,16 @@ private void methodWithLambdaOf_R() { }; ifaceT.method1(1.0); } + + private void methodWithComplexExpression_R() { + IFaceT ifaceT = p->{ + @TestHierarchy("R_method1") + int x; + }; + /* + * 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)); + } }