zoukankan      html  css  js  c++  java
  • cglib动态新增类方法

    <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>3.2.4</version>
            </dependency>
            
    
    import java.io.Serializable;
    import java.lang.reflect.Method;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import net.sf.cglib.core.Signature;
    import net.sf.cglib.proxy.Enhancer;
    import net.sf.cglib.proxy.InterfaceMaker;
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    
    import org.objectweb.asm.Type;
    
    public class CGLibExample {  
      
        @SuppressWarnings("rawtypes")
        public static void main(String[] args) {  
              
            // 定义一个参数是字符串类型的setCreatedAt方法  
            InterfaceMaker im = new InterfaceMaker();  
            im.add(new Signature("setCreatedAt", Type.VOID_TYPE,   
                    new Type[] { Type.getType(String.class) }), null);  
      
            
            Class myInterface = im.create();  
      
            Enhancer enhancer = new Enhancer();  
            enhancer.setSuperclass(ExampleBean.class);  
            enhancer.setInterfaces(new Class[] { myInterface });  
            enhancer.setCallback(new MethodInterceptor() {  
                @Override
                public Object intercept(Object obj, Method method, Object[] args,
                        MethodProxy proxy) throws Throwable {
                    
                    ExampleBean bean = (ExampleBean) obj;  
                      
                    // 调用字符串类型的setCreatedAt方法时,转换成Date型后调用Setter  
                    if (method.getName().startsWith("setCreatedAt")  
                            && args[0] != null && args[0] instanceof String) {  
      
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
                        Date date = null;  
                        try {  
                            date = sdf.parse((String) args[0]);  
                        } catch (final Exception e) { /* nop */ }  
                        bean.setCreatedAt(date);  
                        return null;  
      
                    }  
                    return proxy.invokeSuper(obj, args);  
                
                }  
            });  
      
            // 生成一个Bean  
            ExampleBean bean = (ExampleBean) enhancer.create();  
            bean.setId(999);  
      
            try {  
                Method method = bean.getClass().getMethod("setCreatedAt", new Class[] {String.class});  
                method.invoke(bean, new Object[]{"20160531"});  
            } catch (final Exception e) {  
                e.printStackTrace();  
            }  
              
            System.out.printf("id : [%d] createdAt : [%s]
    ", bean.getId(), bean.getCreatedAt());  
        }  
    }  
      
    class ExampleBean implements Serializable {  
        private static final long serialVersionUID = -8121418052209958014L;  
          
        private int id;  
        private Date createdAt;  
      
        public int getId() {  
            return id;  
        }  
      
        public void setId(int id) {  
            this.id = id;  
        }  
      
        public Date getCreatedAt() {  
            return createdAt;  
        }  
      
        public void setCreatedAt(Date createdAt) {  
            this.createdAt = createdAt;  
        }  
    }
  • 相关阅读:
    UIStoryBoard 中修改控件borderColor
    iOS自定义AlertView 与 ActionSheet 遮罩提示+弹出动画
    iOS开发 UIWebView+JavaScript 交互总结
    【注入攻击】SQL注入(不完整总结)
    [内存溢出]栈溢出基础版
    [Windows驱动开发]之内存管理
    [找工作]程序员面试宝典【笔记】(part 1)
    [Windows安装]安装程序无法创建新的系统分区,也无法定位现有系统分区
    M1卡分析
    [逆向/壳]脱壳方法
  • 原文地址:https://www.cnblogs.com/sprinng/p/6183983.html
Copyright © 2011-2022 走看看