use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DeleteTest method testDeleteAStatementInAnonymousExecutable.
@Test
public void testDeleteAStatementInAnonymousExecutable() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final List<CtAnonymousExecutable> anonymousExecutables = adobada.getAnonymousExecutables();
final CtAnonymousExecutable instanceExec = anonymousExecutables.get(0);
assertEquals(2, instanceExec.getBody().getStatements().size());
final CtStatement statement = instanceExec.getBody().getStatement(1);
statement.delete();
assertEquals(1, instanceExec.getBody().getStatements().size());
assertFalse(instanceExec.getBody().getStatements().contains(statement));
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class SwitchCaseTest method insertBeforeStatementInSwitchCaseWithoutException.
@Test
public void insertBeforeStatementInSwitchCaseWithoutException() throws Exception {
String packageName = "spoon.test.ctCase";
String className = "ClassWithSwitchExample";
Factory factory = factoryFor(packageName, className);
List<CtCase> elements = elementsOfType(CtCase.class, factory);
assertEquals(3, elements.size());
CtCase firstCase = elements.get(0);
List<CtStatement> statements = firstCase.getStatements();
assertEquals(2, statements.size());
CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
statements.get(0).insertBefore(newStatement);
statements = firstCase.getStatements();
assertEquals(3, statements.size());
assertTrue(statements.get(0) == newStatement);
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ExecutableRefTest method getInvocationFromMethod.
private CtAbstractInvocation<?> getInvocationFromMethod(String methodName) throws Exception {
Factory factory = build(ExecutableRefTestSource.class, MyIntf.class);
CtClass<ExecutableRefTestSource> clazz = factory.Class().get(ExecutableRefTestSource.class);
Assert.assertNotNull(clazz);
List<CtMethod<?>> methods = clazz.getMethodsByName(methodName);
assertEquals(1, methods.size());
CtMethod<?> ctMethod = methods.get(0);
CtBlock<?> ctBody = (CtBlock<?>) ctMethod.getBody();
Assert.assertNotNull(ctBody);
List<CtStatement> ctStatements = ctBody.getStatements();
assertEquals(1, ctStatements.size());
CtStatement ctStatement = ctStatements.get(0);
Assert.assertTrue(ctStatement instanceof CtAbstractInvocation<?>);
return (CtAbstractInvocation<?>) ctStatement;
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ReplaceParametrizedTest method testContract.
@Test
public void testContract() throws Throwable {
List<String> problems = new ArrayList<>();
// contract: all elements are replaceable wherever they are in the model
// this test puts them at all possible locations
CtType<?> toTest = typeToTest.getModelInterface();
CtElement o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
for (MetamodelProperty mmField : typeToTest.getRoleToProperty().values()) {
Class<?> argType = mmField.getItemValueType().getActualClass();
if (!CtElement.class.isAssignableFrom(argType)) {
continue;
}
CtTypeReference<?> itemType = mmField.getItemValueType();
// special cases...
if (itemType.getQualifiedName().equals(CtStatement.class.getName())) {
// the children of CtLoop wraps CtStatement into an implicit CtBlock. So make a block directly to test plain get/set and not wrapping.
itemType = factory.createCtTypeReference(CtBlock.class);
}
if (o.getClass().getSimpleName().equals("CtAnnotationFieldAccessImpl") && mmField.getRole() == CtRole.VARIABLE) {
itemType = factory.createCtTypeReference(CtFieldReference.class);
} else if (CtFieldAccess.class.isAssignableFrom(o.getClass()) && mmField.getRole() == CtRole.VARIABLE) {
itemType = factory.createCtTypeReference(CtFieldReference.class);
}
CtElement argument = (CtElement) createCompatibleObject(itemType);
assertNotNull(argument);
// we create a fresh object
CtElement receiver = ((CtElement) o).clone();
RoleHandler rh = RoleHandlerHelper.getRoleHandler(o.getClass(), mmField.getRole());
if (mmField.isUnsettable()) {
try {
// we invoke the setter
invokeSetter(rh, receiver, argument);
} catch (SpoonException e) {
// ok this unsettable property has no setter at all
return;
}
// this unsettable property has setter, but it should do nothing
CtRole argumentsRoleInParent = argument.getRoleInParent();
if (argumentsRoleInParent == null) {
// OK - unsettable property set no value
continue;
}
if (argumentsRoleInParent == mmField.getRole()) {
problems.add("UnsettableProperty " + mmField + " sets the value");
} else {
if (mmField.isDerived()) {
// it is OK, that setting of value into derived unsettable field influences other field
// Example 1: CtCatchVariable.setType(x) influences result of getMultitype()
// Example 2: CtEnumValue.setAssignment(x) influences result of getDefaultExpression()
} else {
problems.add("UnsettableProperty " + mmField + " sets the value into different role " + argumentsRoleInParent);
}
}
continue;
}
// we invoke the setter
invokeSetter(rh, receiver, argument);
// contract: a property setter sets properties that are visitable by a scanner
CtElement finalArgument = argument;
class Scanner extends CtScanner {
boolean found = false;
@Override
public void scan(CtRole role, CtElement e) {
super.scan(role, e);
if (e == finalArgument) {
if (rh.getRole() == role || rh.getRole().getSuperRole() == role) {
found = true;
return;
}
// if (rh.getRole()==CtRole.TYPE && role==CtRole.MULTI_TYPE) {
// //CtCatchVaraible#type sets CtCatchVaraible#multiType - OK
// found = true;
// return;
// }
problems.add("Argument was set into " + rh.getRole() + " but was found in " + role);
}
}
}
;
Scanner s = new Scanner();
receiver.accept(s);
assertTrue("Settable field " + mmField.toString() + " should set value.\n" + getReport(problems), s.found);
// contract: a property getter on the same role can be used to get the value back
assertSame(argument, invokeGetter(rh, receiver));
final CtElement argument2 = argument.clone();
assertNotSame(argument, argument2);
// we do the replace
argument.replace(argument2);
// the new element is indeed now in this AST
assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new Filter<CtElement>() {
@Override
public boolean matches(CtElement element) {
return element == argument2;
}
}).size() == 1);
}
if (problems.size() > 0) {
fail(getReport(problems));
}
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ReplaceTest method testReplaceStatement.
@Test
public void testReplaceStatement() {
CtMethod<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo").getMethod("foo");
Assert.assertTrue(sample.getBody().getStatement(0) instanceof CtVariable);
CtStatement replacement = factory.Core().createInvocation();
sample.getBody().getStatement(0).replace(replacement);
Assert.assertTrue(sample.getBody().getStatement(0) instanceof CtInvocation);
}
Aggregations