Search in sources :

Example 71 with CtMethod

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

the class TestModifiers method testMethodWithVarargsDoesNotBecomeTransient.

@Test
public void testMethodWithVarargsDoesNotBecomeTransient() {
    // contract: method with varsargs should not become transient
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/MethodVarArgs.java");
    spoon.buildModel();
    CtType<?> myClass = spoon.getFactory().Type().get(MethodVarArgs.class);
    CtMethod methodVarargs = myClass.getMethodsByName("getInitValues").get(0);
    Set<ModifierKind> expectedModifiers = Collections.singleton(ModifierKind.PROTECTED);
    assertEquals(expectedModifiers, methodVarargs.getModifiers());
    spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/MethodVarArgs.java");
    spoon.getEnvironment().setShouldCompile(true);
    spoon.run();
}
Also used : ModifierKind(spoon.reflect.declaration.ModifierKind) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 72 with CtMethod

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

the class TestModifiers method testCtModifiableAddRemoveReturnCtModifiable.

@Test
public void testCtModifiableAddRemoveReturnCtModifiable() {
    // contract: CtModifiable#addModifier and CtModifiable#removeModifier should return CtModifiable
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/MethodVarArgs.java");
    spoon.buildModel();
    CtType<?> myClass = spoon.getFactory().Type().get(MethodVarArgs.class);
    CtMethod methodVarargs = myClass.getMethodsByName("getInitValues").get(0);
    Object o = methodVarargs.addModifier(ModifierKind.FINAL);
    assertEquals(methodVarargs, o);
    o = methodVarargs.removeModifier(ModifierKind.FINAL);
    assertEquals(methodVarargs, o);
}
Also used : Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 73 with CtMethod

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

the class TestModifiers method testSetVisibility.

@Test
public void testSetVisibility() {
    // contract: setVisibility should only work with public/private/protected modifiers
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/StaticMethod.java");
    spoon.buildModel();
    CtType<?> myClass = spoon.getFactory().Type().get(StaticMethod.class);
    CtMethod methodPublicStatic = myClass.getMethodsByName("maMethod").get(0);
    assertEquals(ModifierKind.PUBLIC, methodPublicStatic.getVisibility());
    methodPublicStatic.setVisibility(ModifierKind.PROTECTED);
    assertEquals(ModifierKind.PROTECTED, methodPublicStatic.getVisibility());
    try {
        methodPublicStatic.setVisibility(ModifierKind.FINAL);
        fail();
    } catch (SpoonException e) {
    }
    assertEquals(ModifierKind.PROTECTED, methodPublicStatic.getVisibility());
}
Also used : SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 74 with CtMethod

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

the class TestModifiers method testGetModifiersHelpers.

@Test
public void testGetModifiersHelpers() {
    // contract: the CtModifiable helpers like isPublic, isFinal etc returns right values
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/AbstractClass.java");
    spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/ConcreteClass.java");
    spoon.getEnvironment().setShouldCompile(true);
    spoon.run();
    CtType<?> abstractClass = spoon.getFactory().Type().get(AbstractClass.class);
    checkCtModifiableHelpersAssertion(abstractClass, true, false, false, true, false, false);
    assertEquals(4, abstractClass.getFields().size());
    for (CtField field : abstractClass.getFields()) {
        switch(field.getSimpleName()) {
            case "privateField":
                checkCtModifiableHelpersAssertion(field, false, false, true, false, false, false);
                break;
            case "protectedField":
                checkCtModifiableHelpersAssertion(field, false, true, false, false, false, false);
                break;
            case "privateStaticField":
                checkCtModifiableHelpersAssertion(field, false, false, true, false, false, true);
                break;
            case "publicFinalField":
                checkCtModifiableHelpersAssertion(field, true, false, false, false, true, false);
                break;
            default:
                fail("The field " + field.getSimpleName() + " should be take into account.");
        }
    }
    assertEquals(4, abstractClass.getMethods().size());
    for (CtMethod method : abstractClass.getMethods()) {
        switch(method.getSimpleName()) {
            case "method":
                checkCtModifiableHelpersAssertion(method, true, false, false, false, true, true);
                break;
            case "onlyStatic":
                checkCtModifiableHelpersAssertion(method, true, false, false, false, false, true);
                break;
            case "otherMethod":
                checkCtModifiableHelpersAssertion(method, false, true, false, true, false, false);
                break;
            case "anotherOne":
                checkCtModifiableHelpersAssertion(method, false, false, false, true, false, false);
                break;
            default:
                fail("The method " + method.getSimpleName() + " should be taken into account.");
        }
    }
    CtType<?> concreteClass = spoon.getFactory().Type().get("spoon.test.modifiers.testclasses.ConcreteClass");
    checkCtModifiableHelpersAssertion(concreteClass, false, false, false, false, true, false);
    assertEquals(2, concreteClass.getFields().size());
    for (CtField field : concreteClass.getFields()) {
        switch(field.getSimpleName()) {
            case "className":
                checkCtModifiableHelpersAssertion(field, true, false, false, false, true, true);
                break;
            case "test":
                checkCtModifiableHelpersAssertion(field, false, false, true, false, false, true);
                break;
            default:
                fail("The field " + field.getSimpleName() + " should be take into account.");
        }
    }
    assertEquals(2, concreteClass.getMethods().size());
    for (CtMethod method : concreteClass.getMethods()) {
        switch(method.getSimpleName()) {
            case "otherMethod":
                checkCtModifiableHelpersAssertion(method, false, true, false, false, false, false);
                break;
            case "anotherOne":
                checkCtModifiableHelpersAssertion(method, false, false, false, false, true, false);
                break;
            default:
                fail("The method " + method.getSimpleName() + " should be taken into account.");
        }
    }
}
Also used : CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 75 with CtMethod

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

the class ParentContractTest method testContract.

@Test
public void testContract() throws Throwable {
    int nSetterCalls = 0;
    int nAssertsOnParent = 0;
    int nAssertsOnParentInList = 0;
    // contract: all setters/adders must set the parent (not necessarily the direct parent, can be upper in the parent tree, for instance when injecting blocks
    Object o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
    for (CtMethod<?> setter : SpoonTestHelpers.getAllSetters(toTest)) {
        Object argument = createCompatibleObject(setter.getParameters().get(0).getType());
        try {
            // we create a fresh object
            CtElement receiver = ((CtElement) o).clone();
            // we invoke the setter
            Method actualMethod = setter.getReference().getActualMethod();
            actualMethod.invoke(receiver, new Object[] { argument });
            nSetterCalls++;
            nTotalSetterCalls++;
            // directly the element
            if (CtElement.class.isInstance(argument) && setter.getAnnotation(UnsettableProperty.class) == null) {
                nAssertsOnParent++;
                assertTrue(((CtElement) argument).hasParent(receiver));
            }
            // the element is in a list
            if (Collection.class.isInstance(argument) && setter.getAnnotation(UnsettableProperty.class) == null) {
                nAssertsOnParentInList++;
                assertTrue(((CtElement) ((Collection) argument).iterator().next()).hasParent(receiver));
            }
        } catch (AssertionError e) {
            Assert.fail("call setParent contract failed for " + setter.toString() + " " + e.toString());
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof UnsupportedOperationException) {
                // this is now documented by @UnsettableProperty
                throw e;
            } else if (e.getCause() instanceof RuntimeException) {
                throw e.getCause();
            } else {
                throw new SpoonException(e.getCause());
            }
        }
    }
    assertTrue(nSetterCalls > 0);
    assertTrue(nAssertsOnParent > 0 || nAssertsOnParentInList > 0);
}
Also used : SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) Collection(java.util.Collection) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16