Search in sources :

Example 1 with CtNamedElement

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;
}
Also used : IntStream(java.util.stream.IntStream) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) ILine(org.jacoco.core.analysis.ILine) CtNamedElement(spoon.reflect.declaration.CtNamedElement) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) TestClassJSON(fr.inria.diversify.dspot.selector.json.coverage.TestClassJSON) ICounter(org.jacoco.core.analysis.ICounter) CtType(spoon.reflect.declaration.CtType) Gson(com.google.gson.Gson) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) TestCaseJSON(fr.inria.diversify.dspot.selector.json.coverage.TestCaseJSON) Map(java.util.Map) Counter(fr.inria.diversify.utils.Counter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) FileWriter(java.io.FileWriter) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) CoverageResults(fr.inria.stamp.coverage.CoverageResults) FileReader(java.io.FileReader) CtMethod(spoon.reflect.declaration.CtMethod) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) CoverageResults(fr.inria.stamp.coverage.CoverageResults)

Example 2 with CtNamedElement

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;
}
Also used : IntStream(java.util.stream.IntStream) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) ILine(org.jacoco.core.analysis.ILine) CtNamedElement(spoon.reflect.declaration.CtNamedElement) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) TestClassJSON(fr.inria.diversify.dspot.selector.json.coverage.TestClassJSON) ICounter(org.jacoco.core.analysis.ICounter) CtType(spoon.reflect.declaration.CtType) Gson(com.google.gson.Gson) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) TestCaseJSON(fr.inria.diversify.dspot.selector.json.coverage.TestCaseJSON) Map(java.util.Map) Counter(fr.inria.diversify.utils.Counter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) FileWriter(java.io.FileWriter) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) CoverageResults(fr.inria.stamp.coverage.CoverageResults) FileReader(java.io.FileReader) CtMethod(spoon.reflect.declaration.CtMethod) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) ArrayList(java.util.ArrayList) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtMethod(spoon.reflect.declaration.CtMethod) CoverageResults(fr.inria.stamp.coverage.CoverageResults)

Example 3 with CtNamedElement

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;
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtNamedElement(spoon.reflect.declaration.CtNamedElement)

Example 4 with CtNamedElement

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);
}
Also used : ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) CtType(spoon.reflect.declaration.CtType) CtPackage(spoon.reflect.declaration.CtPackage) CtNamedElement(spoon.reflect.declaration.CtNamedElement)

Example 5 with CtNamedElement

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.");
    }
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldReference(spoon.reflect.reference.CtFieldReference) Factory(spoon.reflect.factory.Factory) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) CtField(spoon.reflect.declaration.CtField) CtNamedElement(spoon.reflect.declaration.CtNamedElement) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) Test(org.junit.Test)

Aggregations

CtNamedElement (spoon.reflect.declaration.CtNamedElement)16 Test (org.junit.Test)5 File (java.io.File)4 Map (java.util.Map)4 CtType (spoon.reflect.declaration.CtType)4 FileReader (java.io.FileReader)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CtElement (spoon.reflect.declaration.CtElement)3 CtReference (spoon.reflect.reference.CtReference)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 AutomaticBuilderFactory (fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory)2 TestCaseJSON (fr.inria.diversify.dspot.selector.json.coverage.TestCaseJSON)2 TestClassJSON (fr.inria.diversify.dspot.selector.json.coverage.TestClassJSON)2 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)2 Counter (fr.inria.diversify.utils.Counter)2 DSpotUtils (fr.inria.diversify.utils.DSpotUtils)2 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)2 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)2