<commit_before>package net.sourceforge.argparse4j.impl.type;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import net.sourceforge.argparse4j.helper.TextHelper;
import net.sourceforge.argparse4j.inf.Argument;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.ArgumentType;

/**
 * <p>
 * This implementation converts String value into given type using type's
 * {@code valueOf(java.lang.String)} static method or its constructor.
 * </p>
 */
public class ReflectArgumentType<T> implements ArgumentType<T> {

    private Class<T> type_;

    /**
     * <p>
     * Creates {@link ReflectArgumentType} object with given {@code type}.
     * </p>
     * <p>
     * This object first tries to convert given String using
     * {@code valueOf(java.lang.String)} static method of given {@code type}. If
     * that failed, then use constructor of given {@code type} for conversion.
     * The constructor of {@code type} must accept 1 String argument.
     * </p>
     * <p>
     * If error occurred inside the {@code valueOf} static method or
     * constructor, {@link ArgumentParserException} will be thrown. If error
     * occurred in other locations, subclass of {@link RuntimeException} will be
     * thrown.
     * </p>
     * <p>
     * Because the enums have {@code valueOf} static method, this object works
     * with them as well. The enums in its nature have limited number of
     * members. In {@link #convert(ArgumentParser, Argument, String)}, string
     * value will be converted to one of them. If it cannot be converted,
     * {@link #convert(ArgumentParser, Argument, String)} will throw
     * {@link ArgumentParserException}. This means it already act like a
     * {@link Argument#choices(Object...)}.
     * </p>
     * 
     * @param type
     *            The type String value should be converted to.
     */
    public ReflectArgumentType(Class<T> type) {
        type_ = type;
    }

    @Override
    public T convert(ArgumentParser parser, Argument arg, String value)
            throws ArgumentParserException {
        Method m = null;
        try {
            m = type_.getMethod("valueOf", String.class);
        } catch (NoSuchMethodException e) {
            // If no valueOf static method found, try constructor.
            return convertUsingConstructor(parser, arg, value);
        } catch (SecurityException e) {
            handleInstatiationError(e);
        }
        // Only interested in static valueOf method.
        if (!Modifier.isStatic(m.getModifiers())
                || !type_.isAssignableFrom(m.getReturnType())) {
            return convertUsingConstructor(parser, arg, value);
        }
        try {
            m.setAccessible(true);
        } catch (SecurityException e) {
            return convertUsingConstructor(parser, arg, value);
        }
        Object obj = null;
        try {
            obj = m.invoke(null, value);
        } catch (IllegalAccessException e) {
            return convertUsingConstructor(parser, arg, value);
        } catch (IllegalArgumentException e) {
            handleInstatiationError(e);
        } catch (InvocationTargetException e) {
            throwArgumentParserException(parser, arg, value,
                    e.getCause() == null ? e : e.getCause());
        }
        return (T) obj;
    }

    private T convertUsingConstructor(ArgumentParser parser, Argument arg,
            String value) throws ArgumentParserException {
        T obj = null;
        try {
            obj = type_.getConstructor(String.class).newInstance(value);
        } catch (InstantiationException e) {
            handleInstatiationError(e);
        } catch (IllegalAccessException e) {
            handleInstatiationError(e);
        } catch (InvocationTargetException e) {
            throwArgumentParserException(parser, arg, value,
                    e.getCause() == null ? e : e.getCause());
        } catch (NoSuchMethodException e) {
            handleInstatiationError(e);
        }
        return obj;
    }

    private void throwArgumentParserException(ArgumentParser parser,
            Argument arg, String value, Throwable t)
            throws ArgumentParserException {
        if (type_.isEnum()) {
            String choices = TextHelper.concat(type_.getEnumConstants(), 0,
                    ",", "{", "}");
            throw new ArgumentParserException(String.format(
                    "could not convert '%s' (choose from %s)", value, choices),
                    t, parser, arg);

        } else {
            throw new ArgumentParserException(String.format(
                    "could not convert '%s' to %s (%s)", value,
                    type_.getSimpleName(), t.getMessage()), t, parser, arg);
        }
    }

    private void handleInstatiationError(Exception e) {
        throw new IllegalArgumentException("reflect type conversion error", e);
    }
}
<commit_msg>ReflectArgumentType: Special handling for enums and remove setAccessible<commit_after>package net.sourceforge.argparse4j.impl.type;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import net.sourceforge.argparse4j.helper.TextHelper;
import net.sourceforge.argparse4j.inf.Argument;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.ArgumentType;

/**
 * <p>
 * This implementation converts String value into given type using type's
 * {@code valueOf(java.lang.String)} static method or its constructor.
 * </p>
 */
public class ReflectArgumentType<T> implements ArgumentType<T> {

    private Class<T> type_;

    /**
     * <p>
     * Creates {@link ReflectArgumentType} object with given {@code type}.
     * </p>
     * <p>
     * This object first tries to convert given String using
     * {@code valueOf(java.lang.String)} static method of given {@code type}. If
     * that failed, then use constructor of given {@code type} for conversion.
     * {@code valueOf()} method and/or constructor must be declared as public.
     * Otherwise, they cannot be invoked. The constructor of {@code type} must
     * accept 1 String argument.
     * </p>
     * <p>
     * If error occurred inside the {@code valueOf} static method or
     * constructor, {@link ArgumentParserException} will be thrown. If error
     * occurred in other locations, subclass of {@link RuntimeException} will be
     * thrown.
     * </p>
     * <p>
     * This object works with enums as well. The enums in its nature have
     * limited number of members. In
     * {@link #convert(ArgumentParser, Argument, String)}, string value will be
     * converted to one of them. If it cannot be converted,
     * {@link #convert(ArgumentParser, Argument, String)} will throw
     * {@link ArgumentParserException}. This means it already act like a
     * {@link Argument#choices(Object...)}.
     * </p>
     * 
     * @param type
     *            The type String value should be converted to.
     */
    public ReflectArgumentType(Class<T> type) {
        type_ = type;
    }

    @Override
    public T convert(ArgumentParser parser, Argument arg, String value)
            throws ArgumentParserException {
        // Handle enums separately. Enum.valueOf() is very convenient here.
        // It somehow can access private enum values, where normally T.valueOf()
        // cannot without setAccessible(true).
        if (type_.isEnum()) {
            try {
                return (T) Enum.valueOf((Class<Enum>) type_, value);
            } catch (IllegalArgumentException e) {
                String choices = TextHelper.concat(type_.getEnumConstants(), 0,
                        ",", "{", "}");
                throw new ArgumentParserException(String.format(
                        "could not convert '%s' (choose from %s)", value,
                        choices), e, parser, arg);
            }
        }
        Method m = null;
        try {
            m = type_.getMethod("valueOf", String.class);
        } catch (NoSuchMethodException e) {
            // If no valueOf static method found, try constructor.
            return convertUsingConstructor(parser, arg, value);
        } catch (SecurityException e) {
            handleInstatiationError(e);
        }
        // Only interested in static valueOf method.
        if (!Modifier.isStatic(m.getModifiers())
                || !type_.isAssignableFrom(m.getReturnType())) {
            return convertUsingConstructor(parser, arg, value);
        }
        Object obj = null;
        try {
            obj = m.invoke(null, value);
        } catch (IllegalAccessException e) {
            return convertUsingConstructor(parser, arg, value);
        } catch (IllegalArgumentException e) {
            handleInstatiationError(e);
        } catch (InvocationTargetException e) {
            throwArgumentParserException(parser, arg, value,
                    e.getCause() == null ? e : e.getCause());
        }
        return (T) obj;
    }

    private T convertUsingConstructor(ArgumentParser parser, Argument arg,
            String value) throws ArgumentParserException {
        T obj = null;
        try {
            obj = type_.getConstructor(String.class).newInstance(value);
        } catch (InstantiationException e) {
            handleInstatiationError(e);
        } catch (IllegalAccessException e) {
            handleInstatiationError(e);
        } catch (InvocationTargetException e) {
            throwArgumentParserException(parser, arg, value,
                    e.getCause() == null ? e : e.getCause());
        } catch (NoSuchMethodException e) {
            handleInstatiationError(e);
        }
        return obj;
    }

    private void throwArgumentParserException(ArgumentParser parser,
            Argument arg, String value, Throwable t)
            throws ArgumentParserException {
        throw new ArgumentParserException(String.format(
                "could not convert '%s' to %s (%s)", value,
                type_.getSimpleName(), t.getMessage()), t, parser, arg);
    }

    private void handleInstatiationError(Exception e) {
        throw new IllegalArgumentException("reflect type conversion error", e);
    }
}

{'fraction_non_alphanumeric': 0.09908010732081257, 'fraction_numerical': 0.0015331544653123803, 'mean_word_length': 2.4096700424697812, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '984806', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2661, 'n_words': 830}