提交 0520a92f 编写于 作者: W wanggz2014

添加代理和热部署

上级 06ef96b9
package dynamic;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import sun.misc.ProxyGenerator;
public class DynamicProxyTest {
static interface IHello{
void sayHello();
}
static class Hello implements IHello{
public void sayHello(){
System.out.println("hello world");
}
}
static class DynamicProxy implements InvocationHandler{
private Object orig;
public DynamicProxy(Object orig){
this.orig=orig;
}
//代理处理
@Override
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
System.out.println("hello proxy");
return arg1.invoke(orig, arg2);
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//ystem.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
Class[] interfaces=new Class[]{
IHello.class
};
Hello hello=new Hello();
IHello proxy=(IHello)Proxy.newProxyInstance(DynamicProxy.class.getClassLoader(),interfaces, new DynamicProxy(hello));
FileOutputStream os=new FileOutputStream("d:/$Proxy12.class");
byte[] file=ProxyGenerator.generateProxyClass("$Proxy12", new Class[]{IHello.class});
os.write(file);
os.flush();
os.close();
proxy.sayHello();
}
}
/**
* *********************** 版权声明 ***********************************
*
* 版权所有:wgz
* ©CopyRight wgz03310126 2014
*
* *******************************************************************
*/
package hotswap.bytechange;
/**
* 关于字节码操作工具类
*
* @author wgz
* @version Ver 3.0
* @since Ver 3.0
* @Date 2014-8-22
*
*/
public class ByteUtils {
public static int bytes2Int(byte[] bytes,int start,int len){
int sum=0;
int end=start+len;
for(int i=start;i<end;i++){
int n=((int)bytes[i])&0xff;
n<<=(--len)*8;
sum+=n;
}
return sum;
}
public static byte[] int2Bytes(int value,int len){
byte[] b=new byte[len];
for(int i=0; i< len ;i++){
b[len-i-1]=(byte)((value>>8*i)&0xff);
}
return b;
}
public static String bytes2String(byte[] bytes,int start, int len){
return new String(bytes,start,len);
}
public static byte[] string2Bytes(String str){
return str.getBytes();
}
public static byte[] bytesReplace(byte[] originalBytes,int offset,int len,byte[] replaceBytes){
byte[] newBytes=new byte[originalBytes.length+(replaceBytes.length-len)];
System.arraycopy(originalBytes, 0, newBytes, 0, offset);
System.arraycopy(replaceBytes, 0, newBytes, offset, replaceBytes.length);
System.arraycopy(originalBytes, offset+len, newBytes, offset+replaceBytes.length, originalBytes.length-offset-len);
return newBytes;
}
}
/**
* *********************** 版权声明 ***********************************
*
* 版权所有:wgz
* ©CopyRight wgz03310126 2014
*
* *******************************************************************
*/
package hotswap.bytechange;
/**
* 类字节码修改类
*
* @author wgz
* @version Ver 3.0
* @since Ver 3.0
* @Date 2014-8-22
*
*/
public class ClassModifier {
/**Class文件 中常量池的起始偏移*/
private static final int CONSTANT_POOL_COUNT_INDEX=8;
/**CONSTANT_UTF8_INFO 常量的tag标志*/
private static final int CONSTANT_UTF8_INFO=1;
/**常量池中11 种常量所占的长度,CONSTANTS_UTF8_INFO型常量除外,因为它不是定长的*/
private static final int[] CONSTANT_ITEM_LENGTH={-1,-1,-1,5,5,9,9,3,3,5,5,5,5};
private static final int u1=1;
private static final int u2=2;
private byte[] classByte;
public ClassModifier(byte[] classByte){
this.classByte=classByte;
}
/**
*
* 将class 常量池中特定字面量进行替换
*
* @param oldStr
* @param newStr
* @return
* @since Ver 3.0
*/
public byte[] modifyUTF8Constant(String oldStr,String newStr){
int constantsPoolSize=this.getConstantsPoolSize();
int offset=CONSTANT_POOL_COUNT_INDEX+u2;
for(int i=0;i<constantsPoolSize;i++){
int tag=ByteUtils.bytes2Int(classByte, offset, u1);
if(tag==CONSTANT_UTF8_INFO){
int len=ByteUtils.bytes2Int(classByte, offset+u1, u2);
offset+=u1+u2;
String str=ByteUtils.bytes2String(classByte, offset, len);
if(str.equals(oldStr)){
byte[] newStrBytes=ByteUtils.string2Bytes(newStr);
byte[] newStrLen=ByteUtils.int2Bytes(newStr.length(),u2);
classByte=ByteUtils.bytesReplace(classByte, offset-u2, u2, newStrLen);
classByte=ByteUtils.bytesReplace(classByte, offset, len, newStrBytes);
return classByte;
}else{
offset+=len;
}
}else{
offset+=CONSTANT_ITEM_LENGTH[tag];
}
}
return classByte;
}
/**
*
* 返回常量池个数
*
* @return
* @since Ver 3.0
*/
private int getConstantsPoolSize(){
return ByteUtils.bytes2Int(this.classByte, CONSTANT_POOL_COUNT_INDEX, u2);
}
}
/**
* *********************** 版权声明 ***********************************
*
* 版权所有:wgz
* ©CopyRight wgz03310126 2014
*
* *******************************************************************
*/
package hotswap.bytechange;
/**
* TODO
*
* @author wgz
* @version Ver 3.0
* @since Ver 3.0
* @Date 2014-8-22
*
*/
public class Demo1Test {
public void print() {
System.out.println("demo1 print");
}
}
/**
* *********************** 版权声明 ***********************************
*
* 版权所有:wgz
* ©CopyRight wgz03310126 2014
*
* *******************************************************************
*/
package hotswap.bytechange;
/**
* TODO
*
* @author wgz
* @version Ver 3.0
* @since Ver 3.0
* @Date 2014-8-22
*
*/
public class DemoExec {
public DemoExec(){
Demo1Test test=new Demo1Test();
test.print();
}
}
/**
* *********************** 版权声明 ***********************************
*
* 版权所有:wgz
* ©CopyRight wgz03310126 2014
*
* *******************************************************************
*/
package hotswap.bytechange;
/**
* TODO
*
* @author wgz
* @version Ver 3.0
* @since Ver 3.0
* @Date 2014-8-22
*
*/
public class DemoTest {
public void print() {
System.out.println("print demo");
}
}
/**
* *********************** 版权声明 ***********************************
*
* 版权所有:wgz
* ©CopyRight wgz03310126 2014
*
* *******************************************************************
*/
package hotswap.bytechange;
import java.util.ArrayList;
import java.util.List;
/**
* 热替换类加载器
*
* @author wgz
* @version Ver 3.0
* @since Ver 3.0
* @Date 2014-8-22
*
*/
public class HotSwapClassLoader extends ClassLoader {
public HotSwapClassLoader(){
super(HotSwapClassLoader.class.getClassLoader());
}
public Class<?> loadClass(byte[] bytes){
List<String> strList=new ArrayList<String>();
List list=strList;
list.add("ddff");
return defineClass(null, bytes, 0, bytes.length);
}
}
/**
* *********************** 版权声明 ***********************************
*
* 版权所有:wgz
* ©CopyRight wgz03310126 2014
*
* *******************************************************************
*/
package hotswap.bytechange;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* TODO
*
* @author wgz
* @version Ver 3.0
* @since Ver 3.0
* @Date 2014-8-22
*
*/
public class Main {
/**
* TODO
*
* @param args
* @throws IOException
* @throws IllegalAccessException
* @throws InstantiationException
* @since Ver 3.0
*/
public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException {
execute();
System.gc();
}
private static void execute() throws IOException, InstantiationException, IllegalAccessException{
DemoExec orgi=new DemoExec();
HotSwapClassLoader loader=new HotSwapClassLoader();
InputStream in=Main.class.getResourceAsStream("DemoExec.class");
byte[] classByte=new byte[in.available()];
in.read(classByte);
ClassModifier modifier=new ClassModifier(classByte);
classByte=modifier.modifyUTF8Constant("hotswap/bytechange/Demo1Test", "hotswap/bytechange/DemoTest");
loader.loadClass(classByte).newInstance();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册