<commit_before>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package pw.phylame.ycl.util;

import static java.lang.Character.isLowerCase;
import static java.lang.Character.isUpperCase;
import static pw.phylame.ycl.util.StringUtils.capitalized;
import static pw.phylame.ycl.util.StringUtils.isEmpty;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.val;

public final class Reflections {

    private Reflections() {
    }

    public static String normalizedName(String name) {
        if (isEmpty(name)) {
            return name;
        }
        val length = name.length();
        char ch = name.charAt(0);
        if (isUpperCase(ch)) {
            if (length > 2) {
                char next = name.charAt(1);
                if (isLowerCase(next)) {
                    throw new IllegalArgumentException("invalid name: " + name);
                } else {
                    return name;
                }
            } else {
                return name;
            }
        } else if (isLowerCase(ch)) {
            if (length > 2) {
                char next = name.charAt(1);
                if (isUpperCase(next)) {
                    return name;
                } else {
                    return capitalized(name);
                }
            } else {
                return name.toUpperCase();
            }
        }
        return name;
    }

    public static void makeAccessible(@NonNull AccessibleObject object) {
        if (!object.isAccessible()) {
            object.setAccessible(true);
        }
    }

    public static Class<?> getGenericType(@NonNull Class<?> clazz) {
        return getGenericType(clazz, 0);
    }

    public static Class<?> getGenericType(@NonNull Class<?> clazz, int index) {
        val type = clazz.getGenericSuperclass();
        if (!(type instanceof ParameterizedType)) {
            return Object.class;
        }
        val types = ((ParameterizedType) type).getActualTypeArguments();
        if ((index >= types.length) || (index < 0)) {
            return Object.class;
        }
        if (!(types[index] instanceof Class)) {
            return Object.class;
        }
        return (Class<?>) types[index];
    }

    public static Field findField(@NonNull Class<?> clazz, String name) {
        if (isEmpty(name)) {
            return null;
        }
        for (; clazz != null; clazz = clazz.getSuperclass()) {
            try {
                return clazz.getDeclaredField(name);
            } catch (NoSuchFieldException | SecurityException ignored) {
            }
        }
        return null;
    }

    public static Method findMethod(@NonNull Class<?> clazz, String name, Class<?>... types) {
        if (isEmpty(name)) {
            return null;
        }
        for (; clazz != null; clazz = clazz.getSuperclass()) {
            try {
                return clazz.getDeclaredMethod(name, types);
            } catch (NoSuchMethodException | SecurityException ignored) {
            }
        }
        return null;
    }

    @SneakyThrows(IllegalAccessException.class)
    public static Object invokeMethod(@NonNull Method method, Object target, Object... args) {
        makeAccessible(method);
        try {
            return method.invoke(target, args);
        } catch (InvocationTargetException e) {
            return null;
        }
    }
<commit_msg>add fields and methods filter for reflection utility<commit_after>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package pw.phylame.ycl.util;

import static java.lang.Character.isLowerCase;
import static java.lang.Character.isUpperCase;
import static pw.phylame.ycl.util.StringUtils.capitalized;
import static pw.phylame.ycl.util.StringUtils.isEmpty;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.val;

public final class Reflections {

    private Reflections() {
    }

    public static String normalizedName(String name) {
        if (isEmpty(name)) {
            return name;
        }
        val length = name.length();
        char ch = name.charAt(0);
        if (isUpperCase(ch)) {
            if (length > 2) {
                char next = name.charAt(1);
                if (isLowerCase(next)) {
                    throw new IllegalArgumentException("invalid name: " + name);
                } else {
                    return name;
                }
            } else {
                return name;
            }
        } else if (isLowerCase(ch)) {
            if (length > 2) {
                char next = name.charAt(1);
                if (isUpperCase(next)) {
                    return name;
                } else {
                    return capitalized(name);
                }
            } else {
                return name.toUpperCase();
            }
        }
        return name;
    }

    public static void makeAccessible(@NonNull AccessibleObject object) {
        if (!object.isAccessible()) {
            object.setAccessible(true);
        }
    }

    public static Class<?> getGenericType(@NonNull Class<?> clazz) {
        return getGenericType(clazz, 0);
    }

    public static Class<?> getGenericType(@NonNull Class<?> clazz, int index) {
        val type = clazz.getGenericSuperclass();
        if (!(type instanceof ParameterizedType)) {
            return Object.class;
        }
        val types = ((ParameterizedType) type).getActualTypeArguments();
        if ((index >= types.length) || (index < 0)) {
            return Object.class;
        }
        if (!(types[index] instanceof Class)) {
            return Object.class;
        }
        return (Class<?>) types[index];
    }

    public static Field findField(@NonNull Class<?> clazz, String name) {
        if (isEmpty(name)) {
            return null;
        }
        for (; clazz != null; clazz = clazz.getSuperclass()) {
            try {
                return clazz.getDeclaredField(name);
            } catch (NoSuchFieldException | SecurityException ignored) {
            }
        }
        return null;
    }

    public static List<Field> getFields(@NonNull Class<?> clazz, Function<Field, Boolean> prediction) {
        val fields = new ArrayList<Field>();
        for (; clazz != null; clazz = clazz.getSuperclass()) {
            for (val field : clazz.getDeclaredFields()) {
                if (prediction == null || prediction.apply(field)) {
                    fields.add(field);
                }
            }
        }
        return fields;
    }

    public static Method findMethod(@NonNull Class<?> clazz, String name, Class<?>... types) {
        if (isEmpty(name)) {
            return null;
        }
        for (; clazz != null; clazz = clazz.getSuperclass()) {
            try {
                return clazz.getDeclaredMethod(name, types);
            } catch (NoSuchMethodException | SecurityException ignored) {
            }
        }
        return null;
    }

    public static List<Method> getMethods(@NonNull Class<?> clazz, Function<Method, Boolean> prediction) {
        val methods = new ArrayList<Method>();
        for (; clazz != null; clazz = clazz.getSuperclass()) {
            for (val method : clazz.getDeclaredMethods()) {
                if (prediction == null || prediction.apply(method)) {
                    methods.add(method);
                }
            }
        }
        return methods;
    }

    @SneakyThrows(IllegalAccessException.class)
    public static Object invokeMethod(@NonNull Method method, Object target, Object... args) {
        makeAccessible(method);
        try {
            return method.invoke(target, args);
        } catch (InvocationTargetException e) {
            return null;
        }
    }

{'fraction_non_alphanumeric': 0.10206956333297215, 'fraction_numerical': 0.003250623036081916, 'mean_word_length': 1.9909267660401815, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4549345', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2395, 'n_words': 683}