Search in sources :

Example 16 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class MethodsRefactoringTest method testCtParameterRemoveRefactoringValidationCheck.

@Test
public void testCtParameterRemoveRefactoringValidationCheck() throws FileNotFoundException {
    String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    SpoonModelBuilder comp = launcher.createCompiler();
    comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/" + testPackagePath)));
    comp.build();
    Factory factory = comp.getFactory();
    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<CtExecutable<?>> 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();
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtParameterRemoveRefactoring(spoon.refactoring.CtParameterRemoveRefactoring) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) RefactoringException(spoon.refactoring.RefactoringException) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Example 17 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class MethodsRefactoringTest method checkExecutableReferenceFilter.

private int checkExecutableReferenceFilter(Factory factory, List<CtExecutable<?>> executables) {
    assertTrue(executables.size() > 0);
    ExecutableReferenceFilter execRefFilter = new ExecutableReferenceFilter();
    executables.forEach((CtExecutable<?> e) -> execRefFilter.addExecutable(e));
    final List<CtExecutableReference<?>> refs = new ArrayList<>(factory.getModel().filterChildren(execRefFilter).list());
    int nrExecRefs = refs.size();
    // use different (slower, but straight forward) algorithm to search for all executable references to check if ExecutableReferenceFilter returns correct results
    factory.getModel().filterChildren((CtExecutableReference er) -> {
        return containsSame(executables, er.getDeclaration());
    }).forEach((CtExecutableReference er) -> {
        // check that each expected reference was found by ExecutableReferenceFilter and remove it from that list
        assertTrue("Executable reference: " + er + " not found.", refs.remove(er));
    });
    // check that no other reference was found by ExecutableReferenceFilter
    assertSame(0, refs.size());
    return nrExecRefs;
}
Also used : ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 18 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class MethodsRefactoringTest method testCtParameterRemoveRefactoring.

@Test
public void testCtParameterRemoveRefactoring() throws FileNotFoundException {
    String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    SpoonModelBuilder comp = launcher.createCompiler();
    comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/" + testPackagePath)));
    comp.build();
    Factory factory = comp.getFactory();
    CtType<?> typeA = factory.Class().get(TypeA.class);
    CtMethod<?> methodTypeA_method1 = typeA.getMethodsByName("method1").get(0);
    CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring();
    refactor.setTarget(methodTypeA_method1.getParameters().get(0));
    // check that expected methods are targets of refactoring
    List<CtExecutable<?>> 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);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtParameterRemoveRefactoring(spoon.refactoring.CtParameterRemoveRefactoring) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Example 19 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class MethodsRefactoringTest method checkMethodHierarchy.

private void checkMethodHierarchy(List<CtExecutable<?>> expectedExecutables, CtExecutable startExecutable) {
    // contract: check that by default it does not includes self
    // contract: check that by default it returns lambdas
    {
        final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction()).list();
        assertFalse("Unexpected start executable " + startExecutable, containsSame(executables, startExecutable));
        // check that some method was found
        assertTrue(executables.size() > 0);
        // check that expected methods were found and remove them
        expectedExecutables.forEach(m -> {
            boolean found = removeSame(executables, m);
            if (startExecutable == m) {
                // it is start method. It should not be there
                assertFalse("The signature " + getQSignature(m) + " was returned too", found);
            } else {
                assertTrue("The signature " + getQSignature(m) + " not found", found);
            }
        });
        // check that there is no unexpected executable
        assertTrue("Unexpected executables: " + executables, executables.isEmpty());
    }
    // contract: check that includingSelf(true) returns startMethod too
    // contract: check that by default it still returns lambdas
    {
        final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true)).list();
        assertTrue("Missing start executable " + startExecutable, containsSame(executables, startExecutable));
        // check that some method was found
        assertTrue(executables.size() > 0);
        // check that expected methods were found and remove them
        expectedExecutables.forEach(m -> {
            assertTrue("The signature " + getQSignature(m) + " not found", removeSame(executables, m));
        });
        // check that there is no unexpected executable
        assertTrue("Unexpected executables: " + executables, executables.isEmpty());
    }
    // contract: check that includingLambdas(false) returns no lambda expressions
    {
        final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true).includingLambdas(false)).list();
        if (startExecutable instanceof CtLambda) {
            // lambda must not be returned even if it is first
            assertFalse("Unexpected start executable " + startExecutable, containsSame(executables, startExecutable));
        } else {
            assertTrue("Missing start executable " + startExecutable, containsSame(executables, startExecutable));
        }
        // check that some method was found
        assertTrue(executables.size() > 0);
        // check that expected methods were found and remove them
        expectedExecutables.forEach(m -> {
            if (m instanceof CtLambda) {
                // the lambdas are not expected. Do not ask for them
                return;
            }
            assertTrue("The signature " + getQSignature(m) + " not found", removeSame(executables, m));
        });
        // check that there is no unexpected executable or lambda
        assertTrue("Unexepcted executables " + executables, executables.isEmpty());
    }
    // contract: check early termination
    // contract: check that first returned element is the startExecutable itself if includingSelf == true
    CtExecutable<?> exec = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true)).first();
    assertSame(startExecutable, exec);
    // contract: check that first returned element is not the startExecutable itself if includingSelf == false, but some other executable from the expected
    exec = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(false)).first();
    assertNotSame(startExecutable, exec);
    assertTrue(containsSame(expectedExecutables, exec));
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) AllMethodsSameSignatureFunction(spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction) IFaceL(spoon.test.refactoring.parameter.testclasses.IFaceL) IFaceK(spoon.test.refactoring.parameter.testclasses.IFaceK) CtStatement(spoon.reflect.code.CtStatement) ArrayList(java.util.ArrayList) SpoonModelBuilder(spoon.SpoonModelBuilder) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) IFaceB(spoon.test.refactoring.parameter.testclasses.IFaceB) TypeA(spoon.test.refactoring.parameter.testclasses.TypeA) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) TypeC(spoon.test.refactoring.parameter.testclasses.TypeC) TypeB(spoon.test.refactoring.parameter.testclasses.TypeB) CtExecutable(spoon.reflect.declaration.CtExecutable) CtLambda(spoon.reflect.code.CtLambda) CtConstructor(spoon.reflect.declaration.CtConstructor) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Iterator(java.util.Iterator) RefactoringException(spoon.refactoring.RefactoringException) SpoonResourceHelper(spoon.compiler.SpoonResourceHelper) TypeR(spoon.test.refactoring.parameter.testclasses.TypeR) Collection(java.util.Collection) OutputType(spoon.OutputType) Test(org.junit.Test) CtParameterRemoveRefactoring(spoon.refactoring.CtParameterRemoveRefactoring) Factory(spoon.reflect.factory.Factory) ModelUtils(spoon.testing.utils.ModelUtils) TestHierarchy(spoon.test.refactoring.parameter.testclasses.TestHierarchy) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) SubInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SubInheritanceHierarchyFunction) Assert(org.junit.Assert) Collections(java.util.Collections) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter) CtMethod(spoon.reflect.declaration.CtMethod) CtLambda(spoon.reflect.code.CtLambda) ArrayList(java.util.ArrayList) List(java.util.List) AllMethodsSameSignatureFunction(spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction)

Example 20 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class ExecutableReferenceGenericTest method testMultiReferenceBetweenMethodsWithGenericInSameClass.

@Test
public void testMultiReferenceBetweenMethodsWithGenericInSameClass() throws Exception {
    final CtClass<?> clazz = getCtClassByName("MyClass");
    CtMethod<?> method2 = getCtMethodByNameFromCtClass(clazz, "method2");
    List<CtExecutableReference<?>> refsMethod2 = getReferencesOfAMethod(method2);
    CtMethod<?> expectedMethod1 = getCtMethodByNameFromCtClass(clazz, "method1");
    CtMethod<?> expectedMethod5 = getCtMethodByNameFromCtClass(clazz, "method5");
    assertEquals(3, refsMethod2.size());
    CtExecutable execRefsMethods2 = refsMethod2.get(0).getDeclaration();
    // T has more information in the invocation than its declaration because of the argument type
    // assertEquals(expectedMethod1, refsMethod2.get(0).getDeclaration());
    assertEquals("method1(T extends java.lang.String)", execRefsMethods2.getSignature());
    assertEquals(expectedMethod1, refsMethod2.get(1).getDeclaration());
    assertEquals(expectedMethod5, refsMethod2.get(2).getDeclaration());
}
Also used : CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Aggregations

CtExecutable (spoon.reflect.declaration.CtExecutable)26 Test (org.junit.Test)10 CtType (spoon.reflect.declaration.CtType)9 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)7 Launcher (spoon.Launcher)6 CtElement (spoon.reflect.declaration.CtElement)6 Factory (spoon.reflect.factory.Factory)6 File (java.io.File)5 CtConstructor (spoon.reflect.declaration.CtConstructor)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SpoonException (spoon.SpoonException)4 CtField (spoon.reflect.declaration.CtField)4 CtMethod (spoon.reflect.declaration.CtMethod)4 SpoonModelBuilder (spoon.SpoonModelBuilder)3 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLambda (spoon.reflect.code.CtLambda)3 CtClass (spoon.reflect.declaration.CtClass)3 CtParameter (spoon.reflect.declaration.CtParameter)3