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

com.github.anhem.testpopulator.internal.util.LombokUtil Maven / Gradle / Ivy

Go to download

Populate java classes with fixed or random data using reflection. Facilitates the creation of objects in tests.

The newest version!
package com.github.anhem.testpopulator.internal.util;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.github.anhem.testpopulator.internal.util.PopulateUtil.*;
import static java.util.Arrays.stream;

public class LombokUtil {
    private static final String CLEAR_METHOD_PATTERN = String.format("%s%s", "clear", MATCH_FIRST_CHARACTER_UPPERCASE);

    private LombokUtil() {
    }

    public static Map> getMethodsForLombokBuilderGroupedByInvokeOrder(Class clazz, List blacklistedMethods) {
        return getDeclaredMethods(clazz, blacklistedMethods).stream()
                .filter(method -> !isDeclaringJavaBaseClass(method))
                .collect(Collectors.groupingBy(LombokUtil::lombokMethodInvokeOrder));
    }

    public static int calculateExpectedChildren(Map> builderObjectMethodsGroupedByInvokeOrder) {
        return builderObjectMethodsGroupedByInvokeOrder.entrySet().stream()
                .filter(e -> e.getKey() > 0)
                .map(e -> e.getValue().size())
                .reduce(0, Integer::sum);
    }

    private static int lombokMethodInvokeOrder(Method method) {
        if (stream(method.getParameterTypes()).anyMatch(PopulateUtil::isCollection)) {
            return 3;
        }
        if (isClearMethod(method)) {
            return 2;
        }
        if (hasAtLeastOneParameter(method)) {
            return 1;
        }
        return 0;
    }

    private static boolean isClearMethod(Method method) {
        return method.getName().matches(CLEAR_METHOD_PATTERN) && method.getParameterTypes().length == 0;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy