使用ProxyGenerator類生成字節碼

首先,有一個實體類:java

public class User {
    private Integer id;

    private String name;

    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

       而後是一個Mapper文件,下面這個接口是我使用MyBatise生成工具生成的Mapper文件:app

import cn.freemethod.to.User;

public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

使用一個主類來生成UserMapper接口的代理類:工具

import java.io.FileOutputStream;
import java.io.IOException;

import cn.freemethod.dao.mapper.ms.UserMapper;
import sun.misc.ProxyGenerator;

@SuppressWarnings("restriction")
public class ProxyGeneratorUtil {
    
    private static String DEFAULT_CLASS_NAME = "$Proxy";
    
    public static byte [] saveGenerateProxyClass(String path,Class<?> [] interfaces) {  
   
        byte[] classFile = ProxyGenerator.generateProxyClass(DEFAULT_CLASS_NAME, interfaces);  
          
        FileOutputStream out = null;  
          
        try {  
            String filePath = path + "/" + DEFAULT_CLASS_NAME + ".class";
            out = new FileOutputStream(filePath);  
            out.write(classFile);  
            out.flush();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if(out != null) out.close();  
            } catch (IOException e) {  
                //ignore 
            }  
        }  
        return classFile;
    }  
    
    public static void main(String[] args) {
        
        Class<?> interfaces [] = {UserMapper.class};
        //運行時,確保目錄存在
        /*byte[] classFile = */saveGenerateProxyClass("f:/tmp", interfaces);
        
        
    }

}

       使用反編譯工具,如JD-GUI把生成保存在磁盤的字節碼文件反編譯出來:this

import cn.freemethod.dao.mapper.ms.UserMapper;
import cn.freemethod.to.User;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class $Proxy extends Proxy
  implements UserMapper
{
  private static Method m3;
  private static Method m1;
  private static Method m8;
  private static Method m4;
  private static Method m7;
  private static Method m5;
  private static Method m0;
  private static Method m6;
  private static Method m2;

  public $Proxy(InvocationHandler paramInvocationHandler)
    throws 
  {
    super(paramInvocationHandler);
  }

  public final int insert(User paramUser)
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m3, new Object[] { paramUser })).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final boolean equals(Object paramObject)
    throws 
  {
    try
    {
      return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final int updateByPrimaryKey(User paramUser)
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m8, new Object[] { paramUser })).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final int deleteByPrimaryKey(Integer paramInteger)
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m4, new Object[] { paramInteger })).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final int updateByPrimaryKeySelective(User paramUser)
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m7, new Object[] { paramUser })).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final int insertSelective(User paramUser)
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m5, new Object[] { paramUser })).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final int hashCode()
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m0, null)).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final User selectByPrimaryKey(Integer paramInteger)
    throws 
  {
    try
    {
      return (User)this.h.invoke(this, m6, new Object[] { paramInteger });
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  public final String toString()
    throws 
  {
    try
    {
      return (String)this.h.invoke(this, m2, null);
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }

  static
  {
    try
    {
      m3 = Class.forName("cn.freemethod.dao.mapper.ms.UserMapper").getMethod("insert", new Class[] { Class.forName("cn.freemethod.to.User") });
      m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
      m8 = Class.forName("cn.freemethod.dao.mapper.ms.UserMapper").getMethod("updateByPrimaryKey", new Class[] { Class.forName("cn.freemethod.to.User") });
      m4 = Class.forName("cn.freemethod.dao.mapper.ms.UserMapper").getMethod("deleteByPrimaryKey", new Class[] { Class.forName("java.lang.Integer") });
      m7 = Class.forName("cn.freemethod.dao.mapper.ms.UserMapper").getMethod("updateByPrimaryKeySelective", new Class[] { Class.forName("cn.freemethod.to.User") });
      m5 = Class.forName("cn.freemethod.dao.mapper.ms.UserMapper").getMethod("insertSelective", new Class[] { Class.forName("cn.freemethod.to.User") });
      m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
      m6 = Class.forName("cn.freemethod.dao.mapper.ms.UserMapper").getMethod("selectByPrimaryKey", new Class[] { Class.forName("java.lang.Integer") });
      m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
      return;
    }
    catch (NoSuchMethodException localNoSuchMethodException)
    {
      throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
    }
  }
}

   咱們能夠清楚的看到,生成的代理類是public final的繼承了Proxy,實現了要代理的接口UserMapper。spa

       每個代理的方法都是final的,都是經過調用InvacationHandlerinvoke方法來實現代理的。而且把自身和被代理的方法做爲參數傳遞進去。代理

相關文章
相關標籤/搜索