反射获取class的方法并且调用(手写copy),通过aspectj的JoinPoint获取参数;获取class的所有方法(含父类)

来一个demo

Category category = new Category();

Class<? extends Category> aClass = category.getClass();

Method[] methods = aClass.getMethods();
for (Method method : methods) {
    System.out.println(method.getName());
    if("setId".equals(method.getName())){
        try {
        	//传参的时候类型要传对,不然抛异常的
            method.invoke(category, 11l);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
//查看是否设置成功
System.out.println(category.getId());

打印的结果
在这里插入图片描述

通过属性名反射获取方法调用

import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.beans.PropertyDescriptor;

private boolean checkSingleParam(Object o, String propertyName){
    if(Objects.isNull(o)){
        return false;
    }

    Field companyNameField = ReflectionUtils.findField(o.getClass(), propertyName);
    try {
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor(companyNameField.getName(), o.getClass());
        //获取get方法
        Method method = propertyDescriptor.getReadMethod();
        
		//Method method = propertyDescriptor.getWriteMethod(); set方法

        if(Objects.isNull(method)){
            log.error("未找到方法class:{},propertyName:{}", o.getClass(), propertyName);
            return false;
        }

        Object result = null;
        try {
            result = method.invoke(o);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        if(Objects.isNull(result)){
            return false;
        }

    } catch (IntrospectionException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

通过JoinPoint获取参数

post或者put方法获取请求体中参数

joinPoint.getArgs()

方法名称

String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();

反射手动copy属性

public static void copy(Object originVO, Object destVO) throws InvocationTargetException, IllegalAccessException {

    List<Field> allFilelds = Lists.newArrayList();
    for(Class<?> currentClass = originVO.getClass() ;currentClass != Object.class; currentClass = currentClass.getSuperclass() ){
        Field[] declaredFields = currentClass.getDeclaredFields();
        allFilelds.addAll(Arrays.asList(declaredFields));
    }

    for (Field field : allFilelds) {
        if(!field.isAccessible()){
            field.setAccessible(Boolean.TRUE);
        }

        PropertyDescriptor origin = getPropertyDescriptor(field.getName(), originVO.getClass());

        PropertyDescriptor dest = getPropertyDescriptor(field.getName(), destVO.getClass());

        if(Objects.isNull(origin) || Objects.isNull(dest)){
            continue;
        }

        //获取get方法
        Method method = origin.getReadMethod();
        Object readValue = method.invoke(originVO);
        if(Objects.isNull(readValue)){
            continue;
        }

        Method writeMethod = dest.getWriteMethod();
        writeMethod.invoke(destVO, readValue);
    }

}

private static PropertyDescriptor getPropertyDescriptor(String fileldName, Class<?> currentClass){
    try {
        PropertyDescriptor origin = new PropertyDescriptor(fileldName, currentClass);
        return origin;
    } catch (IntrospectionException e) {
        if(Object.class.equals(currentClass)){
            return null;
        }
        log.error("getPropertyDescriptor() called with parameters => 【fileldName = {}】, 【currentClass = {}】, exception = 【{}】", fileldName, currentClass, e);
        return getPropertyDescriptor(fileldName, currentClass.getSuperclass());
    }
}

获取class的所有方法(含父类)

public static Field[] getFieldsDirectly(Class<?> beanClass, boolean withSuperClassFields) throws SecurityException {
    Assert.notNull(beanClass);

    Field[] allFields = null;
    Class<?> searchType = beanClass;
    Field[] declaredFields;
    while (searchType != null) {
        declaredFields = searchType.getDeclaredFields();
        if (null == allFields) {
            allFields = declaredFields;
        } else {
            //allFields = ArrayUtil.append(allFields, declaredFields);
            if(declaredFields.length > 0){
                Field[] tmp = new Field[allFields.length + declaredFields.length];
                System.arraycopy(allFields, 0, tmp, 0, allFields.length);
                System.arraycopy(declaredFields, 0, tmp, allFields.length, declaredFields.length);
                allFields = tmp;
            }
        }
        searchType = withSuperClassFields ? searchType.getSuperclass() : null;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值