use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.
the class AssertionRemover method removeAssertion.
/**
* Replaces an invocation with its arguments.
*
* @param invocation Invocation
*/
public void removeAssertion(CtInvocation<?> invocation) {
final Factory factory = invocation.getFactory();
invocation.getArguments().forEach(argument -> {
CtExpression clone = ((CtExpression) argument).clone();
if (clone instanceof CtUnaryOperator) {
clone = ((CtUnaryOperator) clone).getOperand();
}
if (clone instanceof CtStatement) {
clone.getTypeCasts().clear();
invocation.insertBefore((CtStatement) clone);
} else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
CtTypeReference<?> typeOfParameter = clone.getType();
if (clone.getType().equals(factory.Type().NULL_TYPE)) {
typeOfParameter = factory.Type().createReference(Object.class);
}
final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
invocation.insertBefore(localVariable);
}
});
CtElement currentParent = invocation;
while (!(currentParent.getParent() instanceof CtStatementList)) {
currentParent = currentParent.getParent();
}
((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.
the class MethodsAssertGenerator method makeFailureTest.
/**
* Adds surrounding try/catch/fail in a failing test.
*
* @param test Failing test method to amplify
* @param failure Test's failure description
* @return New amplified test
*/
protected CtMethod<?> makeFailureTest(CtMethod<?> test, Failure failure) {
CtMethod cloneMethodTest = AmplificationHelper.cloneTestMethodForAmp(test, "");
cloneMethodTest.setSimpleName(test.getSimpleName());
Factory factory = cloneMethodTest.getFactory();
Throwable exception = failure.getException();
if (// TestTimedOutException means infinite loop
exception instanceof TestTimedOutException || exception instanceof AssertionError) {
// AssertionError means that some assertion remained in the test: TODO
return null;
}
Class exceptionClass;
if (exception == null) {
exceptionClass = Exception.class;
} else {
exceptionClass = exception.getClass();
}
CtTry tryBlock = factory.Core().createTry();
tryBlock.setBody(cloneMethodTest.getBody());
String snippet = "org.junit.Assert.fail(\"" + test.getSimpleName() + " should have thrown " + exceptionClass.getSimpleName() + "\")";
tryBlock.getBody().addStatement(factory.Code().createCodeSnippetStatement(snippet));
DSpotUtils.addComment(tryBlock, "AssertGenerator generate try/catch block with fail statement", CtComment.CommentType.INLINE);
CtCatch ctCatch = factory.Core().createCatch();
CtTypeReference exceptionType = factory.Type().createReference(exceptionClass);
ctCatch.setParameter(factory.Code().createCatchVariable(exceptionType, "eee"));
ctCatch.setBody(factory.Core().createBlock());
List<CtCatch> catchers = new ArrayList<>(1);
catchers.add(ctCatch);
tryBlock.setCatchers(catchers);
CtBlock body = factory.Core().createBlock();
body.addStatement(tryBlock);
cloneMethodTest.setBody(body);
cloneMethodTest.setSimpleName(cloneMethodTest.getSimpleName() + "_failAssert" + (numberOfFail++));
Counter.updateAssertionOf(cloneMethodTest, 1);
return cloneMethodTest;
}
use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.
the class CollectionCreator method generateEmptyCollection.
static CtExpression<?> generateEmptyCollection(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) {
final Factory factory = type.getFactory();
final CtType<?> collectionsType = factory.Type().get(Collections.class);
final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference());
final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0);
final CtExecutableReference<?> executableReference = factory.Core().createExecutableReference();
executableReference.setStatic(true);
executableReference.setSimpleName(singletonListMethod.getSimpleName());
executableReference.setDeclaringType(collectionsType.getReference());
executableReference.setType(factory.createCtTypeReference(typeOfCollection));
if (type.getActualTypeArguments().isEmpty()) {
// supporting Collections.<type>emptyList()
executableReference.addActualTypeArgument(type);
} else if (type.getActualTypeArguments().stream().noneMatch(reference -> reference instanceof CtWildcardReference)) {
// in case type is a list, we copy the Actual arguments
executableReference.setActualTypeArguments(type.getActualTypeArguments());
}
return factory.createInvocation(accessToCollections, executableReference);
}
use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.
the class ValueCreator method generateArray.
private static CtExpression generateArray(CtTypeReference type) {
CtArrayTypeReference arrayType = (CtArrayTypeReference) type;
CtTypeReference typeComponent = arrayType.getComponentType();
CtNewArray<?> newArray = type.getFactory().createNewArray();
final int size = AmplificationHelper.getRandom().nextInt(MAX_ARRAY_SIZE);
newArray.setType(arrayType);
if (size == 0) {
newArray.setDimensionExpressions(Collections.singletonList(type.getFactory().createLiteral(size)));
} else if (ValueCreatorHelper.canGenerateAValueForType(typeComponent)) {
IntStream.range(0, size).mapToObj(i -> generateRandomValue(typeComponent)).forEach(newArray::addElement);
}
return newArray;
}
use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.
the class AssertGeneratorHelper method isStmtToLog.
private static boolean isStmtToLog(String filter, CtStatement statement) {
if (!(statement.getParent() instanceof CtBlock)) {
return false;
}
// contract: for now, we do not log values inside loop
if (statement.getParent(CtLoop.class) != null) {
return false;
}
if (statement instanceof CtInvocation) {
CtInvocation invocation = (CtInvocation) statement;
// type tested by the test class
String targetType = "";
if (invocation.getTarget() != null && invocation.getTarget().getType() != null) {
targetType = invocation.getTarget().getType().getQualifiedName();
}
if (targetType.startsWith(filter)) {
return (!isVoidReturn(invocation) && !isGetter(invocation));
} else {
return !isVoidReturn(invocation);
}
}
if (statement instanceof CtLocalVariable || statement instanceof CtAssignment || statement instanceof CtVariableWrite) {
if (statement instanceof CtNamedElement) {
if (((CtNamedElement) statement).getSimpleName().startsWith("__DSPOT_")) {
return false;
}
}
final CtTypeReference type = ((CtTypedElement) statement).getType();
if (type.getQualifiedName().startsWith(filter)) {
return true;
} else {
try {
return type.getActualClass() == String.class;
} catch (SpoonClassNotFoundException e) {
return false;
}
}
} else {
return false;
}
}
Aggregations