use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class ExecutableReferenceTest method testCallMethodOfClassNotPresent.
@Test
public void testCallMethodOfClassNotPresent() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/resources/executable-reference", "--output-type", "nooutput", "--noclasspath" });
final List<CtExecutableReference<?>> references = Query.getReferences(launcher.getFactory(), new ReferenceTypeFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
@Override
public boolean matches(CtExecutableReference<?> reference) {
return !reference.isConstructor() && super.matches(reference);
}
});
final List<CtInvocation<?>> invocations = Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return !element.getExecutable().isConstructor() && super.matches(element);
}
});
assertEquals(4, references.size());
assertEquals(4, invocations.size());
// Executable reference with 0 parameter.
final CtExecutableReference<?> executableZeroParameter = references.get(0);
assertNotNull(executableZeroParameter.getDeclaringType());
assertNull(executableZeroParameter.getType());
assertEquals(0, executableZeroParameter.getParameters().size());
assertEquals("m()", executableZeroParameter.toString());
assertEquals("new Bar().m()", invocations.get(0).toString());
// Executable reference with 1 parameter and return type.
final CtExecutableReference<?> executableOneParameter = references.get(1);
assertNotNull(executableOneParameter.getDeclaringType());
assertNotNull(executableOneParameter.getType());
assertEquals(1, executableOneParameter.getParameters().size());
assertNotEquals(executableZeroParameter, executableOneParameter);
assertEquals("m(int)", executableOneParameter.toString());
assertEquals("bar.m(1)", invocations.get(1).toString());
// Executable reference with 2 parameters.
final CtExecutableReference<?> executableTwoParameters = references.get(2);
assertNotNull(executableTwoParameters.getDeclaringType());
assertNull(executableTwoParameters.getType());
assertEquals(2, executableTwoParameters.getParameters().size());
assertNotEquals(executableTwoParameters, executableZeroParameter);
assertNotEquals(executableTwoParameters, executableOneParameter);
assertEquals("m(int,java.lang.String)", executableTwoParameters.toString());
assertEquals("new Bar().m(1, \"5\")", invocations.get(2).toString());
// Static Executable reference.
final CtExecutableReference<?> staticExecutable = references.get(3);
assertNotNull(staticExecutable.getDeclaringType());
assertNull(staticExecutable.getType());
assertEquals(1, staticExecutable.getParameters().size());
assertNotEquals(staticExecutable, executableZeroParameter);
assertNotEquals(staticExecutable, executableOneParameter);
assertEquals("m(java.lang.String)", staticExecutable.toString());
assertEquals("Bar.m(\"42\")", invocations.get(3).toString());
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class ExecutableReferenceTest method testInvokeEnumMethod.
@Test
public void testInvokeEnumMethod() {
final spoon.Launcher launcher = new spoon.Launcher();
launcher.addInputResource("./src/test/java/spoon/test/reference/Enum.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setComplianceLevel(8);
launcher.buildModel();
CtInvocation invocation = launcher.getModel().getElements(new TypeFilter<CtInvocation>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation element) {
return super.matches(element) && element.getExecutable().getSimpleName().equals("valueOf");
}
}).get(0);
assertNotNull(invocation.getExecutable().getExecutableDeclaration());
}
use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.
the class TestMethodCallAdder method apply.
private CtMethod apply(CtMethod method, int invocation_index) {
CtMethod<?> cloned_method = AmplificationHelper.cloneTestMethodForAmp(method, "_add");
// add the cloned method in the same class as the original method
// get the lit_indexth literal of the cloned method
CtInvocation stmt = Query.getElements(cloned_method, new TypeFilter<>(CtInvocation.class)).get(invocation_index);
CtInvocation cloneStmt = stmt.clone();
final CtStatement parent = getParent(stmt);
parent.insertBefore(cloneStmt);
cloneStmt.setParent(parent.getParent(CtBlock.class));
Counter.updateInputOf(cloned_method, 1);
// DSpotUtils.addComment(cloneStmt, "MethodCallAdder", CtComment.CommentType.INLINE);
return cloned_method;
}
use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.
the class StatementAddTest method testOnClassWithJavaObjects.
@Test
public void testOnClassWithJavaObjects() throws Exception {
/*
Test that the StatementAdd amplifier is able to generate, and manage Collection and Map from java.util
*/
final String packageName = "fr.inria.statementadd";
InputProgram inputProgram = Utils.getInputProgram();
final Factory factory = inputProgram.getFactory();
inputProgram.setFactory(factory);
AmplificationHelper.setSeedRandom(32L);
StatementAdd amplifier = new StatementAdd(packageName);
amplifier.reset(factory.Class().get(packageName + ".ClassTarget"));
CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTarget"), "test");
List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
assertEquals(7, amplifiedMethods.size());
List<String> expectedCalledMethod = Arrays.asList("getList", "getSizeOf", "getSizeOfTypedCollection", "getSizeOfTypedMap");
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 StatementAddTest method testStatementAddOnArrayObjects.
@Test
public void testStatementAddOnArrayObjects() throws Exception {
final String packageName = "fr.inria.statementaddarray";
InputProgram inputProgram = Utils.getInputProgram();
final Factory factory = inputProgram.getFactory();
inputProgram.setFactory(factory);
AmplificationHelper.setSeedRandom(32L);
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(5, amplifiedMethods.size());
List<String> expectedCalledMethod = Arrays.asList("methodWithArrayParatemeter", "methodWithArrayParatemeterFromDomain", "methodWithDomainParameter", "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));
}
Aggregations