zoukankan      html  css  js  c++  java
  • Python动态生成Bean类,并且带toString,equals,hashcode方法

    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("异常信息:参数错误,对象定义无法访问,无法反射性地创建一个实例!
    " + ex.getMessage());
            } catch (IllegalArgumentException ex) {
                System.err.println("异常信息:参数错误,向方法传递了一个不合法或不正确的参数!
    " + ex.getMessage());
            } catch (InvocationTargetException ex) {
                System.err.println("异常信息:参数错误,由调用方法或构造方法所抛出异常的经过检查的异常!
    " + ex.getMessage());
            }
            // 返回结果
            return content.toString();
        }
    }
  • 相关阅读:
    cinder支持nfs快照
    浏览器输入URL到返回页面的全过程
    按需制作最小的本地yum源
    创建可执行bin安装文件
    RPCVersionCapError: Requested message version, 4.17 is incompatible. It needs to be equal in major version and less than or equal in minor version as the specified version cap 4.11.
    惠普IPMI登陆不上
    Linux进程状态——top,ps中看到进程状态D,S,Z的含义
    openstack-neutron基本的网络类型以及分析
    openstack octavia的实现与分析(二)原理,架构与基本流程
    flask上下文流程图
  • 原文地址:https://www.cnblogs.com/staff/p/14302965.html
Copyright © 2011-2022 走看看