use of fr.inria.diversify.dspot.assertGenerator.AssertionRemover in project Ex2Amplifier by STAMP-project.
the class MainGenerator method generateMainMethodFromTestMethod.
public static CtMethod<?> generateMainMethodFromTestMethod(CtMethod<?> testMethod, CtType<?> testClass) {
final Factory factory = testMethod.getFactory();
final CtBlock<?> blockMain = new AssertionRemover().removeAssertion(testMethod).getBody().clone();
final List<CtLiteral<?>> originalLiterals = blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER);
final int[] count = new int[] { 1 };
final List<CtLocalVariable<?>> localVariables = originalLiterals.stream().map(literal -> factory.createLocalVariable(Utils.getRealTypeOfLiteral(literal), "lit" + count[0]++, factory.createCodeSnippetExpression(createMakeRead(factory, literal)))).collect(Collectors.toList());
final CtMethod<?> mainMethod = initMainMethod(factory);
Collections.reverse(localVariables);
localVariables.forEach(blockMain::insertBegin);
Collections.reverse(localVariables);
final Iterator<CtLocalVariable<?>> iterator = localVariables.iterator();
blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).forEach(literal -> literal.replace(factory.createVariableRead(iterator.next().getReference(), false)));
if (!testMethod.getThrownTypes().isEmpty()) {
final CtTry largeTryCatchBlock = createLargeTryCatchBlock(testMethod.getThrownTypes(), factory);
largeTryCatchBlock.setBody(blockMain);
mainMethod.setBody(largeTryCatchBlock);
} else {
mainMethod.setBody(blockMain);
}
removeNonStaticElement(mainMethod, testClass);
return mainMethod;
}
use of fr.inria.diversify.dspot.assertGenerator.AssertionRemover in project Ex2Amplifier by STAMP-project.
the class ArgumentsExtractor method performExtraction.
public static CtMethod<?> performExtraction(CtMethod<?> ctMethod) {
final CtMethod<?> ctMethodWithoutAssertion = new AssertionRemover().removeAssertion(ctMethod);
final Factory factory = ctMethodWithoutAssertion.getFactory();
final CtMethod<?> extractedMethod = ctMethodWithoutAssertion.clone();
extractedMethod.setSimpleName("extract_" + ctMethodWithoutAssertion.getSimpleName());
new ArrayList<>(extractedMethod.getThrownTypes()).forEach(extractedMethod::removeThrownType);
ctMethodWithoutAssertion.getAnnotations().forEach(extractedMethod::removeAnnotation);
final int[] count = new int[1];
final Map<CtAbstractInvocation<?>, List<CtVariableAccess>> parametersPerInvocation = new HashMap<>();
extractedMethod.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).stream().filter(literal -> !(literal.getValue() instanceof String)).forEach(ctLiteral -> {
final CtParameter parameter = factory.createParameter(extractedMethod, Utils.getRealTypeOfLiteral(ctLiteral), "lit" + count[0]++);
final CtParameterReference<?> parameterReference = factory.createParameterReference();
parameterReference.setSimpleName(parameter.getSimpleName());
parameterReference.setType(parameter.getType());
final CtVariableAccess<?> variableRead = factory.createVariableRead(parameterReference, false);
final CtAbstractInvocation invocation = ctLiteral.getParent(CtAbstractInvocation.class);
if (invocation != null) {
if (!parametersPerInvocation.containsKey(invocation)) {
parametersPerInvocation.put(invocation, new ArrayList<>(invocation.getArguments()));
}
if (ctLiteral.getParent() instanceof CtUnaryOperator) {
ctLiteral.getParent().replace(variableRead);
} else {
ctLiteral.replace(variableRead);
}
} else {
ctLiteral.replace(variableRead);
}
});
extractedMethod.setThrownTypes(ctMethod.getThrownTypes());
return extractedMethod;
}
use of fr.inria.diversify.dspot.assertGenerator.AssertionRemover in project Ex2Amplifier by STAMP-project.
the class CATGAmplifier method buildMethodFromValues.
private CtMethod<?> buildMethodFromValues(List<String> values, CtMethod originalTestMethod) {
final Iterator<String> iteratorOnNewValues = values.iterator();
final CtMethod<?> clone = new AssertionRemover().removeAssertion(AmplificationHelper.cloneTestMethodForAmp(originalTestMethod, "_Ex2_CATG"));
final List<CtLiteral<?>> originalLiterals = clone.getBody().getElements(CT_LITERAL_TYPE_FILTER);
originalLiterals.forEach(ctLiteral -> ctLiteral.replace(buildNewLiteralFromString(iteratorOnNewValues.next(), ctLiteral)));
return clone;
}
Aggregations