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

eu.stamp.project.assertfixer.util.Util Maven / Gradle / Ivy

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

import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtType;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

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

    public static final List assertionErrors = new ArrayList<>();

    static {
        assertionErrors.add("java.lang.AssertionError");
        assertionErrors.add("org.junit.internal.ArrayComparisonFailure");
    }

    public static final Function fieldOfObjectToString = (object) ->
            object.getClass().getSimpleName() + "." + object.toString();

    public static final Predicate isFieldOfClass = (object) -> {
        try {
            return null != object.getClass().getField(object.toString());
        } catch (NoSuchFieldException e) {
            return false;
        }
    };

    public static final Predicate isPrimitiveArray = obj ->
            Character.isLowerCase(obj.getClass().getSimpleName().toCharArray()[0]);

    public static final Predicate isFail = ctInvocation ->
            ctInvocation.getExecutable().getSimpleName().startsWith("fail") ||
                    isAssertionClass(ctInvocation.getExecutable().getDeclaringType().getDeclaration());

    public static final Predicate isAssert = ctInvocation ->
            ctInvocation.getExecutable().getSimpleName().startsWith("assert") ||
                    isAssertionClass(ctInvocation.getExecutable().getDeclaringType().getDeclaration());

    public static boolean isAssertionClass(CtType klass) {
        return klass != null &&
                ("org.junit.Assert".equals(klass.getQualifiedName()) || "junit.framework.Assert".equals(klass.getQualifiedName())) &&
                klass.getSuperclass() != null && isAssertionClass(klass.getSuperclass().getDeclaration());
    }

    public static final String PATH_SEPARATOR = System.getProperty("path.separator");

    public static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

}