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

top.hendrixshen.magiclib.util.ReflectionUtil Maven / Gradle / Ivy

The newest version!
package top.hendrixshen.magiclib.util;

import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.util.collect.ValueContainer;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionUtil {
    public static @NotNull ValueContainer> getClass(String className) {
        try {
            return ValueContainer.of(Class.forName(className));
        } catch (ClassNotFoundException e) {
            return ValueContainer.exception(e);
        }
    }

    public static @NotNull ValueContainer> getClass(@NotNull Class clazz) {
        return ValueContainer.of(clazz);
    }

    @SuppressWarnings("unchecked")
    public static @NotNull  ValueContainer> getAnnotationClass(String className) {
        return ReflectionUtil.getClass(className)
                .filter(Class::isAnnotation)
                .map(clazz -> (Class) clazz)
                .or(() -> ValueContainer.exception(new RuntimeException("Class" + className + " is not an annotation.")));
    }

    public static  @NotNull ValueContainer newInstance(String className, Class[] type, Object... args) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.newInstance(clazz, type, args);
    }

    public static  @NotNull ValueContainer newInstance(@NotNull ValueContainer> classContainer,
                                                             Class[] type, Object... args) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.newInstance(classContainer.get(), type, args);
    }

    public static  @NotNull ValueContainer newInstance(@NotNull Class clazz, Class[] type, Object... args) {
        try {
            Constructor declaredConstructor = clazz.getDeclaredConstructor(type);
            declaredConstructor.setAccessible(true);
            @SuppressWarnings("unchecked")
            ValueContainer container = ValueContainer.of((T) declaredConstructor.newInstance(args));
            return container;
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static ValueContainer getStaticField(String className, String fieldName) {
        return ReflectionUtil.getDeclaredField(className, fieldName);
    }

    public static ValueContainer getStaticField(@NotNull ValueContainer> classContainer,
                                                       String fieldName) {
        return ReflectionUtil.getDeclaredField(classContainer, fieldName);
    }

    public static @NotNull ValueContainer getStaticField(@NotNull Class clazz, String fieldName) {
        return ReflectionUtil.getDeclaredField(clazz, fieldName);
    }

    public static  ValueContainer getStaticFieldValue(String className, String fieldName) {
        return ReflectionUtil.getDeclaredFieldValue(className, fieldName, null);
    }

    public static  ValueContainer getStaticFieldValue(@NotNull ValueContainer> classContainer,
                                                            String fieldName) {
        return ReflectionUtil.getDeclaredFieldValue(classContainer, fieldName, null);
    }

    public static  ValueContainer getStaticFieldValue(@NotNull Class clazz, String fieldName) {
        return ReflectionUtil.getDeclaredFieldValue(clazz, fieldName, null);
    }

    public static @NotNull ValueContainer setStaticFieldValue(String className, String fieldName,
                                                                       Object value) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.setDeclaredFieldValue(clazz, fieldName, null, value);
    }

    public static ValueContainer setStaticFieldValue(@NotNull ValueContainer> classContainer,
                                                              String fieldName, Object value) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.setDeclaredFieldValue(classContainer.get(), fieldName, null, value);
    }

    public static ValueContainer setStaticFieldValue(@NotNull Class clazz, String fieldName, Object value) {
        return ReflectionUtil.setDeclaredFieldValue(clazz, fieldName, null, value);
    }

    public static ValueContainer getDeclaredField(String className, String fieldName) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.getDeclaredField(clazz, fieldName);
    }

    public static ValueContainer getDeclaredField(@NotNull ValueContainer> classContainer,
                                                         String fieldName) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.getDeclaredField(classContainer.get(), fieldName);
    }

    public static @NotNull ValueContainer getDeclaredField(@NotNull Class clazz, String fieldName) {
        try {
            return ValueContainer.ofNullable(clazz.getDeclaredField(fieldName));
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static  ValueContainer getDeclaredFieldValue(String className, String fieldName, Object instance) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.getDeclaredFieldValue(clazz, fieldName, instance);
    }

    public static  ValueContainer getDeclaredFieldValue(@NotNull ValueContainer> classContainer,
                                                              String fieldName, Object instance) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.getDeclaredFieldValue(classContainer.get(), fieldName, instance);
    }

    public static  ValueContainer getDeclaredFieldValue(@NotNull Class clazz, String fieldName,
                                                              Object instance) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            @SuppressWarnings("unchecked")
            ValueContainer container = ValueContainer.ofNullable((T) field.get(instance));
            return container;
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static @NotNull ValueContainer setDeclaredFieldValue(String className, String fieldName,
                                                                         Object instance, Object value) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.setDeclaredFieldValue(clazz, fieldName, instance, value);
    }

    public static @NotNull ValueContainer setDeclaredFieldValue(
            @NotNull ValueContainer> classContainer, String fieldName, Object instance, Object value) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.setDeclaredFieldValue(classContainer.get(), fieldName, instance, value);
    }

    public static ValueContainer setDeclaredFieldValue(@NotNull Class clazz, String fieldName,
                                                                Object instance, Object value) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(instance, value);
            return ValueContainer.of(true);
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static ValueContainer getField(String className, String fieldName) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.getField(clazz, fieldName);
    }

    public static ValueContainer getField(@NotNull ValueContainer> classContainer,
                                                 String fieldName) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.getField(classContainer.get(), fieldName);
    }

    public static @NotNull ValueContainer getField(@NotNull Class clazz, String fieldName) {
        try {
            return ValueContainer.ofNullable(clazz.getField(fieldName));
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static  ValueContainer getFieldValue(String className, String fieldName, Object instance) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.getFieldValue(clazz, fieldName, instance);
    }

    public static  ValueContainer getFieldValue(@NotNull ValueContainer> classContainer,
                                                      String fieldName, Object instance) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.getFieldValue(classContainer.get(), fieldName, instance);
    }

    public static  ValueContainer getFieldValue(@NotNull Class clazz, String fieldName, Object instance) {
        try {
            Field field = clazz.getField(fieldName);
            field.setAccessible(true);
            @SuppressWarnings("unchecked")
            ValueContainer container = ValueContainer.ofNullable((T) field.get(instance));
            return container;
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static @NotNull ValueContainer setFieldValue(String className, String fieldName,
                                                                 Object instance, Object value) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.setFieldValue(clazz, fieldName, instance, value);
    }

    public static @NotNull ValueContainer setFieldValue(
            @NotNull ValueContainer> classContainer, String fieldName, Object instance, Object value) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.setFieldValue(classContainer.get(), fieldName, instance, value);
    }

    public static ValueContainer setFieldValue(@NotNull Class clazz, String fieldName,
                                                        Object instance, Object value) {
        try {
            Field field = clazz.getField(fieldName);
            field.setAccessible(true);
            field.set(instance, value);
            return ValueContainer.of(true);
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static  ValueContainer invokeStatic(String className, String methodName,
                                                     Class[] type, Object... args) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.invokeStatic(clazz, methodName, type, args);
    }

    public static  ValueContainer invokeStatic(@NotNull ValueContainer> classContainer,
                                                     String methodName, Class[] type, Object... args) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.invokeStatic(classContainer.get(), methodName, type, args);
    }

    public static  ValueContainer invokeStatic(@NotNull Class clazz, String methodName,
                                                     Class[] type, Object... args) {
        return ReflectionUtil.invokeDeclared(clazz, methodName, null, type, args);
    }

    public static ValueContainer getDeclaredMethod(String className, String methodName, Class... type) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.getDeclaredMethod(clazz, methodName, type);
    }

    public static ValueContainer getDeclaredMethod(@NotNull ValueContainer> classContainer,
                                                           String methodName, Class... type) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.getDeclaredMethod(classContainer.get(), methodName, type);
    }

    public static ValueContainer getDeclaredMethod(@NotNull Class clazz, String methodName,
                                                           Class... type) {
        try {
            Method declaredMethod = clazz.getDeclaredMethod(methodName, type);
            declaredMethod.setAccessible(true);
            return ValueContainer.of(declaredMethod);
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static  ValueContainer invokeDeclared(String className, String methodName, Object instance,
                                                       Class[] type, Object... args) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.invokeDeclared(clazz, methodName, instance, type, args);
    }

    public static  ValueContainer invokeDeclared(@NotNull ValueContainer> classContainer,
                                                       String methodName, Object instance, Class[] type,
                                                       Object... args) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.invokeDeclared(classContainer.get(), methodName, instance, type, args);
    }

    public static  ValueContainer invokeDeclared(@NotNull Class clazz, String methodName, Object instance,
                                                       Class[] type, Object... args) {
        try {
            Method declaredMethod = clazz.getDeclaredMethod(methodName, type);
            declaredMethod.setAccessible(true);
            @SuppressWarnings("unchecked")
            ValueContainer container = ValueContainer.of((T) declaredMethod.invoke(instance, args));
            return container;
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static ValueContainer getMethod(String className, String methodName, Class... type) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.getMethod(clazz, methodName, type);
    }

    public static ValueContainer getMethod(@NotNull ValueContainer> classContainer,
                                                   String methodName, Class... type) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.getMethod(classContainer.get(), methodName, type);
    }

    public static ValueContainer getMethod(@NotNull Class clazz, String methodName, Class... type) {
        try {
            Method method = clazz.getMethod(methodName, type);
            method.setAccessible(true);
            return ValueContainer.of(method);
        } catch (NoSuchMethodException e) {
            return ValueContainer.exception(e);
        }
    }

    public static  @NotNull ValueContainer invoke(String className, String methodName, Object instance,
                                                        Class[] type, Object... args) {
        ValueContainer> clazz = ReflectionUtil.getClass(className);
        return ReflectionUtil.invoke(clazz, methodName, instance, type, args);
    }

    public static  ValueContainer invoke(@NotNull ValueContainer> classContainer, String methodName,
                                               Object instance, Class[] type, Object... args) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.invoke(classContainer.get(), methodName, instance, type, args);
    }

    public static  ValueContainer invoke(@NotNull Class clazz, String methodName, Object instance,
                                               Class[] type, Object... args) {
        try {
            Method method = clazz.getMethod(methodName, type);
            method.setAccessible(true);
            @SuppressWarnings("unchecked")
            ValueContainer container = ValueContainer.of((T) method.invoke(instance, args));
            return container;
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }

    public static  ValueContainer getFieldAnnotation(Field field, String annotationClassName) {
        ValueContainer> clazz = ReflectionUtil.getAnnotationClass(annotationClassName);
        return ReflectionUtil.getFieldAnnotation(field, clazz);
    }

    public static  ValueContainer getFieldAnnotation(
            Field field, @NotNull ValueContainer> classContainer) {
        if (classContainer.isException()) {
            return ValueContainer.exception(classContainer.getException());
        }

        return ReflectionUtil.getFieldAnnotation(field, classContainer.get());
    }

    public static  ValueContainer getFieldAnnotation(Field field, Class annotationClass) {
        try {
            return ValueContainer.ofNullable(field.getAnnotation(annotationClass));
        } catch (Exception e) {
            return ValueContainer.exception(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy