-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyStudent.java
More file actions
52 lines (47 loc) · 1.67 KB
/
ProxyStudent.java
File metadata and controls
52 lines (47 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.asher.proxy;
import sun.misc.ProxyGenerator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
*
* @author : 张勇杰
* @date : 2019/1/17 20:59
* @Version : v1.0
* @description 动态代理
**/
public class ProxyStudent implements InvocationHandler {
private Object target;
public Object getProxyInstance(Object t){
this.target
4A6B
= t;
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(),new Class[]{Student.class},this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
byte[] bytes = ProxyGenerator.generateProxyClass("$proxy0", new Class[]{Student.class});
File file = new File(this.getClass().getResource("").getPath()+File.separator+proxy.getClass().getSimpleName()+".class");
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
/*if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
FileInputStream fis = new FileInputStream(file);
int len = 0;
byte[] buff = new byte[1024];
while((len=fis.read(buff))!=-1){
fos.write(buff,0,len);
}
fis.close();
fos.close();*/
System.out.println("大炮揍了小明一顿");
method.invoke(this.target,args);
System.out.println("大炮再次打了小明一顿");
return null;
}
}