use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class VisibilityTest method testFullyQualifiedNameOfTypeReferenceWithGeneric.
@Test
public void testFullyQualifiedNameOfTypeReferenceWithGeneric() throws Exception {
// contract: Generics are written when there are specified in the return type of a method.
final String target = "./target/spooned/spoon/test/visibility_generics/testclasses/";
final SpoonAPI launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A.java");
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A2.java");
launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/Foo.java");
launcher.setSourceOutputDirectory(target);
launcher.run();
final CtClass<A> aClass = launcher.getFactory().Class().get(A.class);
CtType<?> nestedB = aClass.getNestedType("B");
List<CtFieldAccess> elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(1, elements.size());
assertEquals("(spoon.test.visibility.testclasses.A.B.i)", elements.get(0).toString());
CtMethod<?> instanceOf = aClass.getMethodsByName("instanceOf").get(0);
List<CtBinaryOperator> elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
assertEquals(1, elements1.size());
assertEquals("spoon.test.visibility.testclasses.A.B", elements1.get(0).getRightHandOperand().toString());
CtMethod<?> returnType = aClass.getMethodsByName("returnType").get(0);
assertEquals("spoon.test.visibility.testclasses.A<T>.C<T>", returnType.getType().toString());
final CtClass<A2> secondClass = launcher.getFactory().Class().get(A2.class);
nestedB = secondClass.getNestedType("B");
elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(1, elements.size());
assertEquals("(spoon.test.visibility.testclasses.A2.B.i)", elements.get(0).toString());
instanceOf = secondClass.getMethodsByName("instanceOf").get(0);
elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
assertEquals(1, elements1.size());
assertEquals("spoon.test.visibility.testclasses.A2.B", elements1.get(0).getRightHandOperand().toString());
returnType = secondClass.getMethodsByName("returnType").get(0);
assertEquals("spoon.test.visibility.testclasses.A2.C<java.lang.String>", returnType.getType().toString());
returnType = secondClass.getMethodsByName("returnType2").get(0);
assertEquals("spoon.test.visibility.testclasses.Foo<java.lang.String>.Bar<java.lang.String>", returnType.getType().toString());
canBeBuilt(target, 8);
}
use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class MethodsAssertGenerator method buildTestWithAssert.
/**
* Adds new assertions to a test from observation points.
*
* @param test Test method
* @param observations Observation points of the test suite
* @return Test with new assertions
*/
@SuppressWarnings("unchecked")
private CtMethod<?> buildTestWithAssert(CtMethod test, Map<String, Observation> observations) {
CtMethod testWithAssert = AmplificationHelper.cloneTestMethodForAmp(test, "");
int numberOfAddedAssertion = 0;
List<CtStatement> statements = Query.getElements(testWithAssert, new TypeFilter(CtStatement.class));
for (String id : observations.keySet()) {
if (!id.split("__")[0].equals(testWithAssert.getSimpleName())) {
continue;
}
final List<CtStatement> assertStatements = AssertBuilder.buildAssert(factory, observations.get(id).getNotDeterministValues(), observations.get(id).getObservationValues(), Double.parseDouble(configuration.getProperties().getProperty("delta", "0.1")));
if (assertStatements.stream().map(Object::toString).map("// AssertGenerator add assertion\n"::concat).anyMatch(testWithAssert.getBody().getLastStatement().toString()::equals)) {
continue;
}
int line = Integer.parseInt(id.split("__")[1]);
CtStatement lastStmt = null;
for (CtStatement statement : assertStatements) {
DSpotUtils.addComment(statement, "AssertGenerator add assertion", CtComment.CommentType.INLINE);
try {
CtStatement stmt = statements.get(line);
if (lastStmt == null) {
lastStmt = stmt;
}
if (stmt instanceof CtBlock) {
break;
}
if (stmt instanceof CtInvocation && !AssertGeneratorHelper.isVoidReturn((CtInvocation) stmt) && stmt.getParent() instanceof CtBlock) {
CtInvocation invocationToBeReplaced = (CtInvocation) stmt.clone();
final CtLocalVariable localVariable = factory.createLocalVariable(invocationToBeReplaced.getType(), "o_" + id.split("___")[0], invocationToBeReplaced);
stmt.replace(localVariable);
DSpotUtils.addComment(localVariable, "AssertGenerator create local variable with return value of invocation", CtComment.CommentType.INLINE);
localVariable.setParent(stmt.getParent());
if (id.endsWith("end")) {
testWithAssert.getBody().insertEnd(statement);
} else {
localVariable.insertAfter(statement);
}
statements.remove(line);
statements.add(line, localVariable);
} else {
if (id.endsWith("end")) {
stmt.getParent(CtBlock.class).insertEnd(statement);
} else {
lastStmt.insertAfter(statement);
}
}
lastStmt = statement;
numberOfAddedAssertion++;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Counter.updateAssertionOf(testWithAssert, numberOfAddedAssertion);
if (!testWithAssert.equals(test)) {
return testWithAssert;
} else {
AmplificationHelper.removeAmpTestParent(testWithAssert);
return null;
}
}
use of spoon.reflect.visitor.filter.TypeFilter 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.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class TestMethodCallRemover method apply.
public List<CtMethod> apply(CtMethod method) {
List<CtMethod> methods = new ArrayList<>();
if (method.getDeclaringType() != null) {
// get the list of method calls
List<CtInvocation> invocations = Query.getElements(method, new TypeFilter(CtInvocation.class));
// this index serves to replace ith literal is replaced by zero in the ith clone of the method
int invocation_index = 0;
for (CtInvocation invocation : invocations) {
try {
if (toRemove(invocation) && !AmplificationChecker.isAssert(invocation) && !inWhileLoop(invocation) && !containsIteratorNext(invocation)) {
methods.add(apply(method, invocation_index));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
invocation_index++;
}
}
return methods;
}
use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.
the class AssertGeneratorHelper method createTestWithLog.
static CtMethod<?> createTestWithLog(CtMethod test, final String filter) {
CtMethod clone = AmplificationHelper.cloneTestMethodNoAmp(test);
clone.setSimpleName(test.getSimpleName() + "_withlog");
final List<CtStatement> allStatement = clone.getElements(new TypeFilter<>(CtStatement.class));
allStatement.stream().filter(statement -> isStmtToLog(filter, statement)).forEach(statement -> addLogStmt(statement, test.getSimpleName() + "__" + indexOfByRef(allStatement, statement)));
return clone;
}
Aggregations