use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class ParentTest method testParentOfCtExecutableReference.
@Test
public void testParentOfCtExecutableReference() throws Exception {
// contract: parent of a executable reference is the element which call getExecutable().
final Factory factory = build(Tacos.class);
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
final CtExecutableReference oldExecutable = inv.getExecutable();
assertNotNull(oldExecutable.getParent());
assertEquals(inv, oldExecutable.getParent());
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class QualifiedThisRefTest method testPrintCtFieldAccessWorkEvenWhenParentNotInitialized.
@Test
public void testPrintCtFieldAccessWorkEvenWhenParentNotInitialized() throws Exception {
CtClass zeclass = factory.Class().get(QualifiedThisRef.class);
List<CtMethod> methods = zeclass.getMethodsByName("bla");
assertEquals(1, methods.size());
CtStatement invocation = methods.get(0).getBody().getStatement(0);
assertTrue(invocation instanceof CtInvocation);
CtInvocation<?> arg0 = (CtInvocation) invocation;
CtExpression param = arg0.getArguments().get(0);
CtExecutableReference execref = factory.Core().createExecutableReference();
execref.setDeclaringType(factory.Type().createReference("java.util.Map"));
execref.setSimpleName("exorcise");
execref.setStatic(true);
CtTypeReference tmp = param.getType();
CtExpression arg = null;
CtFieldReference ctfe = factory.createFieldReference();
ctfe.setSimpleName("class");
ctfe.setDeclaringType(tmp.box());
arg = factory.Core().createFieldRead();
((CtFieldAccessImpl) arg).setVariable(ctfe);
CtLiteral location = factory.Core().createLiteral();
location.setType(factory.Type().createReference(String.class));
CtTypeReference tmpref = factory.Core().clone(tmp);
CtInvocation invoc = factory.Core().createInvocation();
invoc.setExecutable(execref);
invoc.setArguments(Arrays.asList(new CtExpression[] { param, arg, location }));
execref.setActualTypeArguments(Arrays.asList(new CtTypeReference<?>[] { tmpref }));
// succeeds
arg0.getArguments().set(0, invoc);
DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(factory.getEnvironment());
printer.visitCtClass(zeclass);
assertFalse(printer.getResult().isEmpty());
}
use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.
the class StatementAddTest method testStatementAdd.
@Test
public void testStatementAdd() throws Exception {
/*
Test the StatementAdd amplifier. It reuse existing object to add method call of accessible method.
It can reuse return value to add method call. It results here with 7 new test cases.
*/
final String packageName = "fr.inria.statementadd";
InputProgram inputProgram = Utils.getInputProgram();
final Factory factory = inputProgram.getFactory();
inputProgram.setFactory(factory);
AmplificationHelper.setSeedRandom(42L);
StatementAdd amplifier = new StatementAdd(packageName);
amplifier.reset(factory.Class().get(packageName + ".ClassTargetAmplify"));
CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
System.out.println(amplifiedMethods);
assertEquals(6, amplifiedMethods.size());
List<String> expectedCalledMethod = Arrays.asList("method", "methodWithDomainParameter", "methodWithPrimitifParameters", "methodWithPrimitifParameters", "methodWithReturn", "method1");
assertTrue(amplifiedMethods.stream().allMatch(amplifiedMethod -> amplifiedMethod.filterChildren(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return expectedCalledMethod.contains(element.getExecutable().getSimpleName());
}
}).first() != null));
}
use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.
the class ChangeMinimizerTest method test.
@SuppressWarnings("unchecked")
@Test
public void test() throws Exception {
/*
ChangeMinimizer keeps only the assertions that trigger the failure on the second version
*/
final CtClass testClass = Utils.findClass("example.TestSuiteExample");
final String configurationPath = Utils.getInputConfiguration().getProperty("configPath");
final String pathToFolder = Utils.getInputConfiguration().getProperty("folderPath");
InputConfiguration inputConfiguration = new InputConfiguration(configurationPath);
InputProgram inputProgram = InputConfiguration.initInputProgram(inputConfiguration);
inputConfiguration.setInputProgram(inputProgram);
String pathToChangedVersionOfProgram = pathToFolder + DSpotUtils.shouldAddSeparator.apply(pathToFolder) + (inputConfiguration.getProperty("targetModule") != null ? inputConfiguration.getProperty("targetModule") + DSpotUtils.shouldAddSeparator.apply(pathToFolder) : "");
inputProgram.setProgramDir(pathToChangedVersionOfProgram);
Initializer.initialize(Utils.getInputConfiguration(), inputProgram);
final HashMap<CtMethod<?>, Failure> failurePerAmplifiedTest = new HashMap<>();
final CtMethod<?> test2 = Utils.findMethod(testClass, "test2");
failurePerAmplifiedTest.put(test2, new Failure(Description.EMPTY, new StringIndexOutOfBoundsException(-1)));
final ChangeMinimizer changeMinimizer = new ChangeMinimizer(testClass, inputConfiguration, inputProgram, pathToChangedVersionOfProgram, failurePerAmplifiedTest);
final CtInvocation assertion = test2.getElements(new TypeFilter<CtInvocation>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation element) {
return AmplificationChecker.isAssert(element);
}
}).get(0);
final CtInvocation clone = assertion.clone();
clone.getElements(new TypeFilter<CtLiteral>(CtLiteral.class) {
@Override
public boolean matches(CtLiteral element) {
return element.getValue() instanceof Integer && super.matches(element);
}
}).get(0).setValue(-1);
test2.getBody().insertEnd(clone);
assertEquals(3, test2.getBody().getStatements().size());
final CtMethod<?> minimize = changeMinimizer.minimize(test2);
assertEquals(2, minimize.getBody().getStatements().size());
}
Aggregations