要一直走下去

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

 

import com.wyz.bean.BaseVo;
import net.sf.cglib.beans.BeanGenerator;
import org.junit.Test;

import java.lang.reflect.Method;


public class MyTest1 {
    @Test
    public void test01() throws Exception {
        BeanGenerator beanGenerator = new BeanGenerator();
        beanGenerator.setSuperclass(BaseVo.class);
        beanGenerator.addProperty("value", String.class);
        BaseVo myBean = (BaseVo) beanGenerator.create();
        Method setter = myBean.getClass().getMethod("setValue", String.class);
        setter.invoke(myBean, "张三1");


        BaseVo myBean1 = (BaseVo) beanGenerator.create();
//        Method setter1 = myBean.getClass().getMethod("setValue", String.class);
//        setter1.invoke(myBean1, "张三2");
//        for (Method m :myBean.getClass().getMethods()){
//            System.out.println(m.getName());
//        }

//
//        Method toString = myBean.getClass().getMethod("toString");
//        System.out.println(toString.invoke(myBean, null));
//
//        Method getter = myBean.getClass().getMethod("getValue");
//        System.out.println(getter.invoke(myBean));
        System.out.println(myBean);
        System.out.println(myBean.equals(myBean1));
//        System.out.println(myBean.hashCode());
    }
}




package com.wyz.bean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseVo{
    /**
     * 指示其他某个对象是否与此对象“相等”
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || this.getClass() != obj.getClass()) return false;
        try {
            for (Method method : this.getClass().getMethods()) {
                String methodName = method.getName();
                // 过滤Bean类字段的get方法
                if (methodName.startsWith("get") && !methodName.equals("getClass")) {
                    // 获取被比较对象的字段值
                    Method objGetMethod = obj.getClass().getMethod(methodName);
                    // 执行方法以获取返回值比较(关键点:注意参数不相同)
                    Object objProperty = objGetMethod.invoke(obj);
                    Object thisProperty = method.invoke(this);

                    // 空值报异
                    if (objProperty == null) {
                        System.err.println("异常信息:类" + obj.getClass().getName() + "中的" + methodName + "方法为null值!无法进行对象比较!");
                    }
                    if (thisProperty == null) {
                        System.err.println("异常信息:类" + this.getClass().getName() + "中的" + methodName + "方法为null值!无法进行对象比较!");
                    }
                    // 返回值不相等则返回逻辑假
                    if (!thisProperty.equals(objProperty)) {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 返回该对象的哈希码值
     */
    @Override
    public int hashCode() {
        // 生成简单的位运算hash散列码
        String key = this.toString();
        int prime = key.hashCode();
        int hash = prime;
        for (int i = 0; i < key.length(); i++) {
            hash ^= (hash << 23 >> 17) ^ key.charAt(i) * 13131;
        }
        // 返回结果
        return (hash % prime) * 33;
    }

    /**
     * 返回该对象的字符串表示(类似数组的toString方法输出结果)
     */
    @Override
    public String toString() {
        // 当前类反射方法组
        Method[] methodGroup = this.getClass().getMethods();
        // toString内容
        StringBuffer content = new StringBuffer("{");
        try {
            // 遍历反射方法组并提取属性的getter方法
            for (Method method : methodGroup) {
                // 过滤与属性无关的get方法
                String methodName = method.getName();
                if (methodName.startsWith("get") && !methodName.equals("getClass")) {
                    String propName = methodName.substring(3).toLowerCase();
                    content.append(propName);
                    content.append("=");
                    content.append(method.invoke(this));
                    content.append(",");
                }
            }
            content.append("}");
        } catch (IllegalAccessException ex) {
            System.err.println("异常信息:参数错误,对象定义无法访问,无法反射性地创建一个实例!\r\n" + ex.getMessage());
        } catch (IllegalArgumentException ex) {
            System.err.println("异常信息:参数错误,向方法传递了一个不合法或不正确的参数!\r\n" + ex.getMessage());
        } catch (InvocationTargetException ex) {
            System.err.println("异常信息:参数错误,由调用方法或构造方法所抛出异常的经过检查的异常!\r\n" + ex.getMessage());
        }
        // 返回结果
        return content.toString();
    }
}
posted on 2021-01-20 15:06  要一直走下去  阅读(546)  评论(0编辑  收藏  举报