use of spoon.reflect.declaration.CtMethod in project Ex2Amplifier by STAMP-project.
the class Ex2AmplifierTest method testUsingJBSE2.
@Test
public void testUsingJBSE2() throws Exception {
final Ex2Amplifier ex2Amplifier = Ex2Amplifier.getEx2Amplifier(Ex2Amplifier.Ex2Amplifier_Mode.JBSE);
ex2Amplifier.init(this.configuration);
final CtClass<?> testClass = this.launcher.getFactory().Class().get("fr.inria.stamp.MainTest");
ex2Amplifier.reset(testClass);
final CtMethod<?> test = testClass.getMethodsByName("test2").get(0);
final List<CtMethod> apply = ex2Amplifier.apply(test);
System.out.println(apply);
}
use of spoon.reflect.declaration.CtMethod in project Ex2Amplifier by STAMP-project.
the class Ex2AmplifierTest method test.
@Test
public void test() throws Exception {
/*
Test that the Ex2Amplifier returns a List of CtMethod build thank to CATG.
Amplified CtMethod has the same "structural" inputs of the test, but with
different test data input, i.e. literals has been modified.
*/
final Ex2Amplifier ex2Amplifier = Ex2Amplifier.getEx2Amplifier(Ex2Amplifier.Ex2Amplifier_Mode.CATG);
ex2Amplifier.init(this.configuration);
final CtClass<?> testClass = this.launcher.getFactory().Class().get("fr.inria.stamp.MainTest");
ex2Amplifier.reset(testClass);
final CtMethod<?> test = testClass.getMethodsByName("test").get(0);
final List<CtMethod> apply = ex2Amplifier.apply(test);
assertEquals(3, apply.size());
try (BufferedReader buffer = new BufferedReader(new FileReader(this.configuration.getOutputDirectory() + "/" + testClass.getQualifiedName() + "/test_values.csv"))) {
assertEquals("\"\\\"bar\\\"\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "\"NEW\\nLINE\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "true,false,false,false" + AmplificationHelper.LINE_SEPARATOR + "'<','0','0','0'" + AmplificationHelper.LINE_SEPARATOR + "'\\'','0','0','0'" + AmplificationHelper.LINE_SEPARATOR + "3,0,0,0,0,0,0,0,0,0,0,0,0" + AmplificationHelper.LINE_SEPARATOR + "16,0,0,0" + AmplificationHelper.LINE_SEPARATOR + "100,0,0,0" + AmplificationHelper.LINE_SEPARATOR + "\"Potion\",\"\\uffff\",\"\\u0000\",\"\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "5,0,0,1" + AmplificationHelper.LINE_SEPARATOR + "\"Timoleon\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "1000,0,0,0", buffer.lines().collect(Collectors.joining(AmplificationHelper.LINE_SEPARATOR)));
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println(apply);
}
use of spoon.reflect.declaration.CtMethod in project Ex2Amplifier by STAMP-project.
the class JBSEAmplifier method generateNewTestMethod.
private CtMethod<?> generateNewTestMethod(CtMethod<?> testMethod, Map<String, List<String>> conditionForParameter) {
final CtMethod clone = AmplificationHelper.cloneTestMethodForAmp(testMethod, "_Ex2_JBSE");
final List<?> solutions = SMTSolver.solve(conditionForParameter);
final Iterator<?> iterator = solutions.iterator();
final List<CtLiteral> originalLiterals = clone.getElements(new TypeFilter<>(CtLiteral.class));
conditionForParameter.keySet().forEach(s -> {
try {
final int indexOfLit = Integer.parseInt(s.substring("param".length()));
final CtLiteral literalToBeReplaced = originalLiterals.get(indexOfLit);
final CtLiteral<?> newLiteral = testMethod.getFactory().createLiteral(iterator.next());
if (!this.intermediateAmplification.containsKey(literalToBeReplaced)) {
this.intermediateAmplification.put(literalToBeReplaced, new ArrayList<>());
}
this.intermediateAmplification.get(literalToBeReplaced).add(newLiteral);
if (literalToBeReplaced.getParent() instanceof CtUnaryOperator) {
literalToBeReplaced.getParent().replace(newLiteral);
} else {
literalToBeReplaced.replace(newLiteral);
}
} catch (Exception e) {
LOGGER.warn("Error when trying to generate a value for {}", s);
}
});
return clone;
}
use of spoon.reflect.declaration.CtMethod 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 spoon.reflect.declaration.CtMethod in project spoon by INRIA.
the class AnnotationFactory method annotate.
/**
* Creates/updates an element's annotation value.
*
* @param element
* the program element to annotate
* @param annotationType
* the annotation type
* @param annotationElementName
* the annotation element name
* @param value
* the value of the annotation element
* @return the created/updated annotation
*/
public <A extends Annotation> CtAnnotation<A> annotate(CtElement element, CtTypeReference<A> annotationType, String annotationElementName, Object value) {
final CtAnnotation<A> annotation = annotate(element, annotationType);
boolean isArray;
// try with CT reflection
CtAnnotationType<A> ctAnnotationType = ((CtAnnotationType<A>) annotation.getAnnotationType().getDeclaration());
boolean newValue = annotation.getValue(annotationElementName) == null;
if (ctAnnotationType != null) {
CtMethod<?> e = ctAnnotationType.getMethod(annotationElementName);
isArray = (e.getType() instanceof CtArrayTypeReference);
} else {
Method m;
try {
m = annotation.getAnnotationType().getActualClass().getMethod(annotationElementName, new Class[0]);
} catch (Exception ex) {
annotation.addValue(annotationElementName, value);
return annotation;
}
isArray = m.getReturnType().isArray();
}
if (isArray || newValue) {
annotation.addValue(annotationElementName, value);
} else {
throw new SpoonException("cannot assign an array to a non-array annotation element");
}
return annotation;
}
Aggregations