use of spoon.reflect.code.CtLocalVariable in project spoon by INRIA.
the class ParentTest method testGetParentWithFilter.
@Test
public void testGetParentWithFilter() throws Exception {
// addType should set Parent
CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
CtMethod<Object> m = clazz.getMethod("m");
// get three = "" in one = two = three = "";
CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return element.getParent() instanceof CtStatementList && super.matches(element);
}
});
// the filter has to return one = two = three = ""
assertEquals(m.getBody().getStatement(3), ctStatement);
m = clazz.getMethod("internalClass");
CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return element instanceof CtLocalVariable && super.matches(element);
}
}).get(0);
// get the top class
ctStatement1.getParent(CtType.class);
CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {
@Override
public boolean matches(CtType element) {
return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
}
});
assertEquals(clazz, parent);
assertNotEquals(ctStatement1.getParent(CtType.class), parent);
// not present element
CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
assertEquals(null, ctWhile);
CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return true;
}
});
// getParent must not return the current element
assertNotEquals(statement, statementParent);
}
use of spoon.reflect.code.CtLocalVariable in project Ex2Amplifier by STAMP-project.
the class MainGenerator method removeNonStaticElement.
private static void removeNonStaticElement(final CtMethod<?> mainMethod, final CtType testClass) {
final CtBlock<?> body = mainMethod.getBody();
final Factory factory = mainMethod.getFactory();
// 1 create a local variable of the test class
final CtLocalVariable localVariableOfTestClass = factory.createLocalVariable(testClass.getReference(), Character.toLowerCase(testClass.getSimpleName().charAt(0)) + testClass.getSimpleName().substring(1), factory.createConstructorCall(testClass.getReference()));
// 2 invoke setUp(@Before) at the begin if present
final CtTry wrappedBefore = wrapInTryCatchMethodWithSpecificAnnotation(testClass, factory, localVariableOfTestClass, "org.junit.Before");
if (wrappedBefore != null) {
body.insertBegin(wrappedBefore);
}
// 3 invoke tearDown(@After) at the end of the block
final CtTry wrappedAfter = wrapInTryCatchMethodWithSpecificAnnotation(testClass, factory, localVariableOfTestClass, "org.junit.After");
if (wrappedAfter != null) {
body.insertEnd(wrappedAfter);
}
// 4 replaces all non-static accesses to accesses on the local variable created at the first step
body.getElements(new TypeFilter<CtTargetedExpression>(CtTargetedExpression.class) {
@Override
public boolean matches(CtTargetedExpression element) {
return element.getTarget() instanceof CtThisAccess;
}
}).stream().map(CtTargetedExpression::getTarget).forEach(target -> target.replace(factory.createVariableRead(localVariableOfTestClass.getReference(), false)));
body.insertBegin(localVariableOfTestClass);
}
use of spoon.reflect.code.CtLocalVariable in project dspot by STAMP-project.
the class TestValueCreator method testCreateRandomLocalVar.
@Test
public void testCreateRandomLocalVar() throws Exception {
/*
Test the value created randomly by the value creator.
- one primitive
- one array
- one object
*/
AmplificationHelper.setSeedRandom(23L);
ValueCreator.count = 0;
Factory factory = Utils.getFactory();
int count = 0;
CtLocalVariable randomLocalVar = ValueCreator.createRandomLocalVar(factory.Type().INTEGER_PRIMITIVE);
assertEquals("__DSPOT_vc_" + count, randomLocalVar.getSimpleName());
assertEquals(factory.Type().INTEGER_PRIMITIVE, randomLocalVar.getType());
assertEquals(-1150482841, ((CtLiteral) randomLocalVar.getDefaultExpression()).getValue());
randomLocalVar = ValueCreator.createRandomLocalVar(factory.Type().createArrayReference("int"));
count++;
assertEquals("__DSPOT_vc_" + count, randomLocalVar.getSimpleName());
assertEquals(factory.Type().createArrayReference("int"), randomLocalVar.getType());
randomLocalVar = ValueCreator.createRandomLocalVar(factory.Type().createReference("fr.inria.mutation.ClassUnderTest"));
count++;
assertEquals("__DSPOT_vc_" + count, randomLocalVar.getSimpleName());
assertEquals(factory.Type().createReference("fr.inria.mutation.ClassUnderTest"), randomLocalVar.getType());
}
use of spoon.reflect.code.CtLocalVariable in project dspot by STAMP-project.
the class TestValueCreator method testCreateNull.
@Test
public void testCreateNull() throws Exception {
/*
Test the value created randomly by the value creator.
- one primitive
- one array
- one object
*/
AmplificationHelper.setSeedRandom(23L);
ValueCreator.count = 0;
Factory factory = Utils.getFactory();
int count = 0;
CtLocalVariable randomLocalVar = ValueCreator.generateNullValue(factory.Type().INTEGER_PRIMITIVE);
assertEquals("vc_" + count, randomLocalVar.getSimpleName());
assertEquals(factory.Type().INTEGER_PRIMITIVE, randomLocalVar.getType());
assertEquals("((int) (null))", randomLocalVar.getDefaultExpression().toString());
randomLocalVar = ValueCreator.generateNullValue(factory.Type().createArrayReference("int"));
count++;
assertEquals("vc_" + count, randomLocalVar.getSimpleName());
assertEquals(factory.Type().createArrayReference("int"), randomLocalVar.getType());
assertEquals("((int[]) (null))", randomLocalVar.getDefaultExpression().toString());
randomLocalVar = ValueCreator.generateNullValue(factory.Type().createReference("mutation.ClassUnderTest"));
count++;
assertEquals("vc_" + count, randomLocalVar.getSimpleName());
assertEquals(factory.Type().createReference("mutation.ClassUnderTest"), randomLocalVar.getType());
assertEquals("((mutation.ClassUnderTest) (null))", randomLocalVar.getDefaultExpression().toString());
}
Aggregations