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();
}
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);
}
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());
}
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.");
}
}
}
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);
}
Aggregations