<commit_before>package org.cobbzilla.util.reflect;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang3.ArrayUtils;
import org.cobbzilla.util.collection.ExpirationMap;
import org.cobbzilla.util.string.StringUtil;

import java.io.Closeable;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isStatic;
import static org.apache.commons.lang3.reflect.FieldUtils.getAllFields;
import static org.cobbzilla.util.collection.ArrayUtil.arrayToString;
import static org.cobbzilla.util.daemon.ZillaRuntime.*;
import static org.cobbzilla.util.string.StringUtil.uncapitalize;

/**
 * Handy tools for working quickly with reflection APIs, which tend to be verbose.
 */
@Slf4j
public class ReflectionUtil {

    public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
    public static final Class<?>[] SINGLE_STRING_ARG = {String.class};

    public static Boolean toBoolean(Object object) {
        if (object == null) return null;
        if (object instanceof Boolean) return (Boolean) object;
        if (object instanceof String) return Boolean.valueOf(object.toString());
        return null;
    }

    public static Boolean toBoolean(Object object, String field, boolean defaultValue) {
        final Boolean val = toBoolean(get(object, field));
        return val == null ? defaultValue : val;
    }

    public static Long toLong(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).longValue();
        if (object instanceof String) return Long.valueOf(object.toString());
        return null;
    }

    public static Integer toInteger(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).intValue();
        if (object instanceof String) return Integer.valueOf(object.toString());
        return null;
    }

    public static Integer toIntegerOrNull(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).intValue();
        if (object instanceof String) {
            try {
                return Integer.valueOf(object.toString());
            } catch (Exception e) {
                log.info("toIntegerOrNull("+object+"): "+e);
                return null;
            }
        }
        return null;
    }

    public static Short toShort(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).shortValue();
        if (object instanceof String) return Short.valueOf(object.toString());
        return null;
    }

    public static Float toFloat(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).floatValue();
        if (object instanceof String) return Float.valueOf(object.toString());
        return null;
    }

    public static Double toDouble(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).doubleValue();
        if (object instanceof String) return Double.valueOf(object.toString());
        return null;
    }

    public static BigDecimal toBigDecimal(Object object) {
        if (object == null) return null;
        if (object instanceof Double) return big((Double) object);
        if (object instanceof Float) return big((Float) object);
        if (object instanceof Number) return big(((Number) object).longValue());
        if (object instanceof String) return big(object.toString());
        return null;
    }

    /**
     * Do a Class.forName and only throw unchecked exceptions.
     * @param clazz full class name. May end in [] to indicate array class
     * @param <T> The class type
     * @return A Class&lt;clazz&gt; object
     */
    public static <T> Class<? extends T> forName(String clazz) {
        if (empty(clazz)) return (Class<? extends T>) Object.class;
        if (clazz.endsWith("[]")) return arrayClass(forName(clazz.substring(0, clazz.length()-2)));
        try {
            return (Class<? extends T>) Class.forName(clazz);
        } catch (Exception e) {
            return die("Class.forName("+clazz+") error: "+e);
        }
    }

    public static Collection<Class> forNames(String[] classNames) {
        final List<Class> list = new ArrayList<>();
        if (!empty(classNames)) for (String c : classNames) list.add(forName(c));
        return list;
    }

    public static <T> Class<? extends T> arrayClass (Class clazz) { return forName("[L"+clazz.getName()+";"); }

    /**
     * Create an instance of a class, only throwing unchecked exceptions. The class must have a default constructor.
     * @param clazz we will instantiate an object of this type
     * @param <T> The class type
     * @return An Object that is an instance of Class&lt;clazz&gt; object
     */
    public static <T> T instantiate(Class<T> clazz) {
        try {
            return clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            return die("Error instantiating "+clazz+": "+e, e);<commit_msg>add support for primitive types to forName utility<commit_after>package org.cobbzilla.util.reflect;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang3.ArrayUtils;
import org.cobbzilla.util.collection.ExpirationMap;
import org.cobbzilla.util.string.StringUtil;

import java.io.Closeable;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isStatic;
import static org.apache.commons.lang3.reflect.FieldUtils.getAllFields;
import static org.cobbzilla.util.collection.ArrayUtil.arrayToString;
import static org.cobbzilla.util.daemon.ZillaRuntime.*;
import static org.cobbzilla.util.string.StringUtil.uncapitalize;

/**
 * Handy tools for working quickly with reflection APIs, which tend to be verbose.
 */
@Slf4j
public class ReflectionUtil {

    public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
    public static final Class<?>[] SINGLE_STRING_ARG = {String.class};

    public static Boolean toBoolean(Object object) {
        if (object == null) return null;
        if (object instanceof Boolean) return (Boolean) object;
        if (object instanceof String) return Boolean.valueOf(object.toString());
        return null;
    }

    public static Boolean toBoolean(Object object, String field, boolean defaultValue) {
        final Boolean val = toBoolean(get(object, field));
        return val == null ? defaultValue : val;
    }

    public static Long toLong(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).longValue();
        if (object instanceof String) return Long.valueOf(object.toString());
        return null;
    }

    public static Integer toInteger(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).intValue();
        if (object instanceof String) return Integer.valueOf(object.toString());
        return null;
    }

    public static Integer toIntegerOrNull(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).intValue();
        if (object instanceof String) {
            try {
                return Integer.valueOf(object.toString());
            } catch (Exception e) {
                log.info("toIntegerOrNull("+object+"): "+e);
                return null;
            }
        }
        return null;
    }

    public static Short toShort(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).shortValue();
        if (object instanceof String) return Short.valueOf(object.toString());
        return null;
    }

    public static Float toFloat(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).floatValue();
        if (object instanceof String) return Float.valueOf(object.toString());
        return null;
    }

    public static Double toDouble(Object object) {
        if (object == null) return null;
        if (object instanceof Number) return ((Number) object).doubleValue();
        if (object instanceof String) return Double.valueOf(object.toString());
        return null;
    }

    public static BigDecimal toBigDecimal(Object object) {
        if (object == null) return null;
        if (object instanceof Double) return big((Double) object);
        if (object instanceof Float) return big((Float) object);
        if (object instanceof Number) return big(((Number) object).longValue());
        if (object instanceof String) return big(object.toString());
        return null;
    }

    /**
     * Do a Class.forName and only throw unchecked exceptions.
     * @param clazz full class name. May end in [] to indicate array class
     * @param <T> The class type
     * @return A Class&lt;clazz&gt; object
     */
    public static <T> Class<? extends T> forName(String clazz) {
        if (empty(clazz)) return (Class<? extends T>) Object.class;
        if (clazz.endsWith("[]")) return arrayClass(forName(clazz.substring(0, clazz.length()-2)));
        try {
            return (Class<? extends T>) Class.forName(clazz);
        } catch (ClassNotFoundException e) {
            switch (clazz) {
                case "boolean": return (Class<? extends T>) boolean.class;
                case "byte":    return (Class<? extends T>) byte.class;
                case "short":   return (Class<? extends T>) short.class;
                case "char":    return (Class<? extends T>) char.class;
                case "int":     return (Class<? extends T>) int.class;
                case "long":    return (Class<? extends T>) long.class;
                case "float":   return (Class<? extends T>) float.class;
                case "double":  return (Class<? extends T>) double.class;
            }
            return die("Class.forName("+clazz+") error: "+shortError(e));

        } catch (Exception e) {
            return die("Class.forName("+clazz+") error: "+shortError(e));
        }
    }

    public static Collection<Class> forNames(String[] classNames) {
        final List<Class> list = new ArrayList<>();
        if (!empty(classNames)) for (String c : classNames) list.add(forName(c));
        return list;
    }

    public static <T> Class<? extends T> arrayClass (Class clazz) { return forName("[L"+clazz.getName()+";"); }

    /**
     * Create an instance of a class, only throwing unchecked exceptions. The class must have a default constructor.
     * @param clazz we will instantiate an object of this type
     * @param <T> The class type
     * @return An Object that is an instance of Class&lt;clazz&gt; object
     */
    public static <T> T instantiate(Class<T> clazz) {
        try {
            return clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            return die("Error instantiating "+clazz+": "+e, e);
{'fraction_non_alphanumeric': 0.11295788284653865, 'fraction_numerical': 0.0012909472325318702, 'mean_word_length': 3.05197777051324, 'pattern_counts': {'":': 10, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '676367', 'n_tokens_mistral': 3387, 'n_tokens_neox': 3301, 'n_words': 1043}