java反射工具类

一个很好用的反射工具类 在 由google开发者写的 flogger-core-2.0.0.jar包下面

import com.jahnelgroup.flogger.BindExpand;
import com.jahnelgroup.flogger.FloggerException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class ReflectionUtils {

    private static final String ILLEGAL_ACCESS_EXCEPTION_MSG = "Error accessing %s on class: %s";
    private static final String CLASS_NOT_FOUND_EXCEPTION_MSG = "Error finding class: %s";
    private static final String NO_SUCH_METHOD_EXCEPTION_MSG = "Error finding %s(%s) on class: %s";
    private static final String INVOCATION_TARGET_EXCEPTION_MSG = "%s.%s threw an exception";

    public static Method getMethodFromJointPoint(JoinPoint joinPoint) throws FloggerException {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String className = methodSignature.getDeclaringTypeName();
        Class[] parameterTypes = methodSignature.getParameterTypes();
        try {
            return Class.forName(className).getDeclaredMethod(methodSignature.getName(), parameterTypes);
        } catch (ClassNotFoundException e) {
            throw new FloggerException(String.format(CLASS_NOT_FOUND_EXCEPTION_MSG, className), e);
        } catch (NoSuchMethodException e) {
            throw new FloggerException(String.format(
                    NO_SUCH_METHOD_EXCEPTION_MSG, methodSignature.getName(),
                    Arrays.toString(parameterTypes).substring(1, parameterTypes.length - 1),
                    className), e);
        }
    }

    public static Stream<Field> getFieldsOnClass(Class<?> clazz) {
        return Arrays.stream(clazz.getDeclaredFields());
    }

    public static Stream<Method> getMethodsOnClass(Class<?> clazz) {
        return Arrays.stream(clazz.getDeclaredMethods());
    }

    public static Stream<Method> getMethodsWithoutParametersOnClass(Class<?> clazz) {
        return getMethodsOnClass(clazz).filter(method -> method.getParameterCount() == 0);
    }

    public static Stream<Field> getIncludedBindableFieldsOnClass(Class<?> clazz) {
        Stream<Field> fields = getFieldsOnClass(clazz);
        return fields.filter(field -> field.isAnnotationPresent(BindExpand.Include.class));
    }

    public static Stream<Field> filterOutExcludedFields(Stream<Field> fields) {
        return fields.filter(field -> !field.isAnnotationPresent(BindExpand.Exclude.class));
    }

    public static List<Field> getBindableFieldsOnClass(Class<?> clazz) {
        if (clazz.isAnnotationPresent(BindExpand.class)) {
            BindExpand bindExpand = clazz.getDeclaredAnnotation(BindExpand.class);
            if (bindExpand.onlyExplicityIncluded()) {
                return getIncludedBindableFieldsOnClass(clazz).collect(Collectors.toList());
            }
        }
        return filterOutExcludedFields(getFieldsOnClass(clazz)).collect(Collectors.toList());
    }

    public static Stream<Method> getIncludedBindableMethodsOnClass(Class<?> clazz) {
        Stream<Method> methods = getMethodsWithoutParametersOnClass(clazz);
        return methods.filter(method -> method.isAnnotationPresent(BindExpand.Include.class));
    }

    public static Stream<Method> filterOutExcludedMethods(Stream<Method> methods) {
        return methods.filter(method -> !method.isAnnotationPresent(BindExpand.Exclude.class));
    }

    public static List<Method> getBindableMethodsOnClass(Class<?> clazz) {
        if (clazz.isAnnotationPresent(BindExpand.class)) {
            BindExpand bindExpand = clazz.getDeclaredAnnotation(BindExpand.class);
            if (bindExpand.onlyExplicityIncluded()) {
                return getIncludedBindableMethodsOnClass(clazz).collect(Collectors.toList());
            }
        }
        return filterOutExcludedMethods(getMethodsWithoutParametersOnClass(clazz)).collect(Collectors.toList());
    }

    public static Object getFieldValue(Field field, Object obj) throws FloggerException {
        boolean isAccessible = field.isAccessible();
        if (!isAccessible) {
            field.setAccessible(true);
        }
        try {
            return field.get(obj);
        } catch (IllegalAccessException e) {
            throw new FloggerException(String.format(ILLEGAL_ACCESS_EXCEPTION_MSG, field.getName(), obj.getClass()), e);
        } finally {
            field.setAccessible(isAccessible);
        }
    }

    public static Object getMethodReturnValue(Method method, Object obj) throws FloggerException {
        boolean isAccessible = method.isAccessible();
        if (!isAccessible) {
            method.setAccessible(true);
        }
        try {
            return method.invoke(obj);
        } catch (IllegalAccessException e) {
            throw new FloggerException(String.format(ILLEGAL_ACCESS_EXCEPTION_MSG, method.getName(), obj.getClass()), e);
        } catch (InvocationTargetException e) {
            throw new FloggerException(String.format(INVOCATION_TARGET_EXCEPTION_MSG, obj.getClass(), method.getName()), e);
        } finally {
            method.setAccessible(isAccessible);
        }
    }

    public static String[] getParameterNames(Parameter[] parameters) {
        String[] parameterNames = new String[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            parameterNames[i] = parameters[i].getName();
        }
        return parameterNames;
    }

 

Java 反射工具类 ReflectUtils 是一个提供了便捷的反射操作方法的工具类。它封装了 Java 反射 API 的一些常用功能,使得开发者能够更加方便地使用反射进行类、方法、字段的操作。 ReflectUtils 提供了以下几个方法: 1. getTypeArguments:通过反射获取指定类的泛型类型参数。可以通过该方法来获取泛型类型的具体参数类型,方便在运行时进行类型判断和操作。 2. invokeGetter:通过反射调用指定对象的指定字段的 getter 方法。在无法直接访问字段或需要动态调用字段的 getter 方法时,可以使用该方法。 3. invokeSetter:通过反射调用指定对象的指定字段的 setter 方法。在无法直接访问字段或需要动态调用字段的 setter 方法时,可以使用该方法。 4. getFieldValue:通过反射获取指定对象的指定字段的值。在无法直接访问字段时,可以使用该方法获取字段的值。 5. setFieldValue:通过反射设置指定对象的指定字段的值。在无法直接访问字段时,可以使用该方法设置字段的值。 6. getField:通过反射获取指定类的指定字段。可以用于获取字段的修饰符、类型等信息。 7. getMethod:通过反射获取指定类的指定方法。可以用于获取方法的修饰符、返回值类型、参数类型等信息。 ReflectUtils 的使用能够简化反射操作的代码,提高开发效率,但也需要注意合理使用,避免引入不必要的复杂性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值