All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.stamp.project.assertfixer.asserts.AssertionReplacer Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package eu.stamp.project.assertfixer.asserts;

import eu.stamp.project.assertfixer.util.Util;
import spoon.reflect.code.*;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Benjamin DANGLOT
 * [email protected]
 * on 24/05/17
 */
public class AssertionReplacer {

    static List replaceByLogStatement(CtMethod clone) {
        final List assertionsToBeReplaced = clone.getElements(new TypeFilter(CtInvocation.class) {
            @Override
            public boolean matches(CtInvocation element) {
                return Util.isAssert.test(element) && super.matches(element);
            }
        }).stream()
                .sorted(Comparator.comparingInt(ctInvocation -> ctInvocation.getPosition().getLine()))
                .collect(Collectors.toList());
        final List indices = new ArrayList<>();
        assertionsToBeReplaced.forEach(assertionToBeReplaced -> {
            final int index = assertionToBeReplaced.getParent(CtBlock.class).getStatements().indexOf(assertionToBeReplaced);
            indices.add(index);
            String snippet = "eu.stamp.project.assertfixer.asserts.log.Logger.log(";
            if (assertionToBeReplaced.getExecutable().getSimpleName().endsWith("True") ||
                    assertionToBeReplaced.getExecutable().getSimpleName().endsWith("False")) {
                assertionToBeReplaced.replace(
                        clone.getFactory().createCodeSnippetStatement(snippet + index + "," + assertionToBeReplaced.getArguments().get(0) + ")")
                );
            } else {
                assertionToBeReplaced.replace(
                        clone.getFactory().createCodeSnippetStatement(snippet + index + "," + assertionToBeReplaced.getArguments().get(1) + ")")
                );
            }
        });
        return indices;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy