-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex2Amplifier.java
101 lines (90 loc) · 4.37 KB
/
Ex2Amplifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package fr.inria.stamp;
import fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory;
import fr.inria.diversify.dspot.amplifier.Amplifier;
import fr.inria.diversify.dspot.assertGenerator.AssertionRemover;
import fr.inria.diversify.dspot.support.DSpotCompiler;
import fr.inria.diversify.utils.AmplificationHelper;
import fr.inria.diversify.utils.DSpotUtils;
import fr.inria.diversify.utils.sosiefier.InputConfiguration;
import fr.inria.stamp.MainGenerator;
import fr.inria.stamp.catg.CATGExecutor;
import fr.inria.stamp.catg.CATGUtils;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.filter.TypeFilter;
import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by Benjamin DANGLOT
* benjamin.danglot@inria.fr
* on 24/01/18
*/
public class Ex2Amplifier implements Amplifier {
private InputConfiguration configuration;
public Ex2Amplifier(InputConfiguration configuration) {
this.configuration = configuration;
}
@Override
public List<CtMethod> apply(CtMethod ctMethod) {
if (ctMethod.getElements(new TypeFilter<CtLiteral>(CtLiteral.class)).isEmpty()) {
return Collections.emptyList();
}
final CtMethod<?> mainMethodFromTestMethod =
MainGenerator.generateMainMethodFromTestMethod(ctMethod);
final CtClass testClass = ctMethod.getParent(CtClass.class);
testClass.addMethod(mainMethodFromTestMethod);
DSpotUtils.printJavaFileWithComment(testClass, new File("target/dspot/tmp_test_sources"));
String classpath = AutomaticBuilderFactory
.getAutomaticBuilder(this.configuration)
.buildClasspath(this.configuration.getInputProgram().getProgramDir())
+ AmplificationHelper.PATH_SEPARATOR +
this.configuration.getInputProgram().getProgramDir() + "/" + this.configuration.getInputProgram().getClassesDir()
+ AmplificationHelper.PATH_SEPARATOR +
this.configuration.getInputProgram().getProgramDir() + "/" + this.configuration.getInputProgram().getTestClassesDir();
DSpotCompiler.compile("target/dspot/tmp_test_sources", classpath + AmplificationHelper.PATH_SEPARATOR + "lib/catg-dev.jar",
new File(this.configuration.getInputProgram().getProgramDir() + "/" +
this.configuration.getInputProgram().getTestClassesDir())
);
final List<List<String>> execute = CATGExecutor.execute(classpath, testClass.getQualifiedName());
CATGUtils.eraseOldFiles();
testClass.removeMethod(mainMethodFromTestMethod);
return execute.stream()
.map(values -> buildMethodFromValues(values, ctMethod))
.collect(Collectors.toList());
}
private CtMethod<?> buildMethodFromValues(List<String> values, CtMethod originalTestMethod) {
final Iterator<String> iteratorOnNewValues = values.iterator();
final CtMethod<?> clone = new AssertionRemover().removeAssertion(
AmplificationHelper.cloneMethodTest(originalTestMethod, "Ex2Amplifier")
);
final List<CtLiteral> originalLiterals = clone.getBody().getElements(new TypeFilter<CtLiteral>(CtLiteral.class));
originalLiterals.forEach(ctLiteral ->
ctLiteral.replace(buildNewLiteralFromString(iteratorOnNewValues.next(), ctLiteral))
);
return clone;
}
private CtLiteral<?> buildNewLiteralFromString(String value, CtLiteral<?> originalLiteral) {
final Object originalLiteralValue = originalLiteral.getValue();
final Factory factory = originalLiteral.getFactory();
if (originalLiteralValue instanceof String) {
return factory.createLiteral(value);
} else if (originalLiteralValue instanceof Integer){
return factory.createLiteral(Integer.parseInt(value));
} else {
throw new UnsupportedOperationException(originalLiteralValue.getClass() + " is not supported");
}
}
@Override
public CtMethod applyRandom(CtMethod ctMethod) {
throw new UnsupportedOperationException();
}
@Override
public void reset(CtType ctType) {
}
}