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;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值