use of spoon.reflect.declaration.CtNamedElement in project dspot by STAMP-project.
the class JacocoCoverageSelector method selectToAmplify.
@Override
public List<CtMethod<?>> selectToAmplify(List<CtMethod<?>> testsToBeAmplified) {
if (this.currentClassTestToBeAmplified == null && !testsToBeAmplified.isEmpty()) {
this.currentClassTestToBeAmplified = testsToBeAmplified.get(0).getDeclaringType();
this.initialCoverage = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified);
this.selectedToBeAmplifiedCoverageResultsMap = null;
this.selectedAmplifiedTest.clear();
}
final List<String> methodNames = testsToBeAmplified.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
final Map<String, CoverageResults> coverageResultsMap = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified, methodNames);
final List<String> pathExecuted = new ArrayList<>();
final List<CtMethod<?>> filteredTests = testsToBeAmplified.stream().filter(ctMethod -> ctMethod != null && coverageResultsMap.get(ctMethod.getSimpleName()) != null).filter(ctMethod -> {
final String pathByExecInstructions = computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder());
if (pathExecuted.contains(pathByExecInstructions)) {
return false;
} else {
pathExecuted.add(pathByExecInstructions);
return true;
}
}).collect(Collectors.toList());
if (this.selectedToBeAmplifiedCoverageResultsMap == null) {
final List<String> filteredMethodNames = filteredTests.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
this.selectedToBeAmplifiedCoverageResultsMap = coverageResultsMap.keySet().stream().filter(filteredMethodNames::contains).collect(Collectors.toMap(Function.identity(), coverageResultsMap::get));
}
return filteredTests;
}
use of spoon.reflect.declaration.CtNamedElement in project dspot by STAMP-project.
the class JacocoCoverageSelector method selectToKeep.
@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
if (amplifiedTestToBeKept.isEmpty()) {
return amplifiedTestToBeKept;
}
final List<String> methodNames = amplifiedTestToBeKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
final Map<String, CoverageResults> coverageResultsMap = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified, methodNames);
final List<String> pathExecuted = new ArrayList<>();
final List<CtMethod<?>> methodsKept = amplifiedTestToBeKept.stream().filter(ctMethod -> {
final String simpleNameOfFirstParent = getFirstParentThatHasBeenRun(ctMethod).getSimpleName();
return this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent) == null || coverageResultsMap.get(ctMethod.getSimpleName()).isBetterThan(this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent)) && !computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder()).equals(computePathExecuted.apply(this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent).getCoverageBuilder()));
}).filter(ctMethod -> {
final String pathByExecInstructions = computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder());
if (pathExecuted.contains(pathByExecInstructions)) {
return false;
} else {
pathExecuted.add(pathByExecInstructions);
return true;
}
}).collect(Collectors.toList());
this.selectedToBeAmplifiedCoverageResultsMap.putAll(methodsKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toMap(Function.identity(), coverageResultsMap::get)));
this.selectedAmplifiedTest.addAll(new ArrayList<>(methodsKept));
return methodsKept;
}
use of spoon.reflect.declaration.CtNamedElement 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;
}
}
use of spoon.reflect.declaration.CtNamedElement in project spoon by INRIA.
the class JDTImportBuilder method build.
// package visible method in a package visible class, not in the public API
void build() {
if (declarationUnit.imports == null || declarationUnit.imports.length == 0) {
return;
}
for (ImportReference importRef : declarationUnit.imports) {
String importName = importRef.toString();
if (!importRef.isStatic()) {
if (importName.endsWith("*")) {
int lastDot = importName.lastIndexOf(".");
String packageName = importName.substring(0, lastDot);
// only get package from the model by traversing from rootPackage the model
// it does not use reflection to achieve that
CtPackage ctPackage = this.factory.Package().get(packageName);
if (ctPackage != null) {
this.imports.add(factory.Type().createImport(ctPackage.getReference()));
}
} else {
CtType klass = this.getOrLoadClass(importName);
if (klass != null) {
this.imports.add(factory.Type().createImport(klass.getReference()));
}
}
} else {
int lastDot = importName.lastIndexOf(".");
String className = importName.substring(0, lastDot);
String methodOrFieldName = importName.substring(lastDot + 1);
CtType klass = this.getOrLoadClass(className);
if (klass != null) {
if (methodOrFieldName.equals("*")) {
this.imports.add(factory.Type().createImport(factory.Type().createWildcardStaticTypeMemberReference(klass.getReference())));
} else {
List<CtNamedElement> methodOrFields = klass.getElements(new NamedElementFilter<>(CtNamedElement.class, methodOrFieldName));
if (methodOrFields.size() > 0) {
CtNamedElement methodOrField = methodOrFields.get(0);
this.imports.add(factory.Type().createImport(methodOrField.getReference()));
}
}
}
}
}
spoonUnit.setImports(this.imports);
}
use of spoon.reflect.declaration.CtNamedElement in project spoon by INRIA.
the class FilterTest method testFieldAccessFilter.
@Test
public void testFieldAccessFilter() throws Exception {
// also specifies VariableAccessFilter since FieldAccessFilter is only a VariableAccessFilter with additional static typing
CtClass<?> foo = factory.Package().get("spoon.test.filters").getType("Foo");
assertEquals("Foo", foo.getSimpleName());
List<CtNamedElement> elements = foo.getElements(new NamedElementFilter<>(CtNamedElement.class, "i"));
assertEquals(1, elements.size());
CtFieldReference<?> ref = (CtFieldReference<?>) (elements.get(0)).getReference();
List<CtFieldAccess<?>> expressions = foo.getElements(new FieldAccessFilter(ref));
assertEquals(2, expressions.size());
final Factory build = build(FieldAccessFilterTacos.class);
final CtType<FieldAccessFilterTacos> fieldAccessFilterTacos = build.Type().get(FieldAccessFilterTacos.class);
try {
List<CtField> fields = fieldAccessFilterTacos.getElements(new TypeFilter<CtField>(CtField.class));
for (CtField ctField : fields) {
fieldAccessFilterTacos.getElements(new FieldAccessFilter(ctField.getReference()));
}
} catch (NullPointerException e) {
fail("FieldAccessFilter must not throw a NPE.");
}
}
Aggregations