Skip to content

Commit 00f4b44

Browse files
committed
动态代理 (jdk)
1 parent 9be72af commit 00f4b44

File tree

11 files changed

+131
-11
lines changed

11 files changed

+131
-11
lines changed

.idea/libraries/javassist.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java-Javassist/java-Javassist.iml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10-
<orderEntry type="module-library" exported="">
11-
<library>
12-
<CLASSES>
13-
<root url="jar://$USER_HOME$/Desktop/jboss-javassist-javassist-85d69ea/javassist.jar!/" />
14-
</CLASSES>
15-
<JAVADOC />
16-
<SOURCES />
17-
</library>
18-
</orderEntry>
10+
<orderEntry type="library" exported="" name="javassist" level="project" />
1911
</component>
2012
</module>

java-Javassist/src/com/zyj/javassist/Demo02.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.IOException;
66
import java.lang.reflect.Field;
77
import java.lang.reflect.Method;
8-
import java.text.MessageFormat;
98
import java.util.Arrays;
109

1110
/**

java-proxy/java-proxy.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.asher.proxy;
2+
3+
import sun.misc.ProxyGenerator;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.lang.reflect.InvocationHandler;
9+
import java.lang.reflect.Method;
10+
import java.lang.reflect.Proxy;
11+
12+
/**
13+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
14+
*
15+
* @author : 张勇杰
16+
* @date : 2019/1/17 20:59
17+
* @Version : v1.0
18+
* @description 动态代理
19+
**/
20+
public class ProxyStudent implements InvocationHandler {
21+
private Object target;
22+
23+
public Object getProxyInstance(Object t){
24+
this.target = t;
25+
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(),new Class[]{Student.class},this);
26+
}
27+
28+
@Override
29+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
30+
byte[] bytes = ProxyGenerator.generateProxyClass("$proxy0", new Class[]{Student.class});
31+
File file = new File(this.getClass().getResource("").getPath()+File.separator+proxy.getClass().getSimpleName()+".class");
32+
FileOutputStream fos = new FileOutputStream(file);
33+
fos.write(bytes);
34+
fos.flush();
35+
/*if(!file.exists()){
36+
file.createNewFile();
37+
}
38+
FileOutputStream fos = new FileOutputStream(file);
39+
FileInputStream fis = new FileInputStream(file);
40+
int len = 0;
41+
byte[] buff = new byte[1024];
42+
while((len=fis.read(buff))!=-1){
43+
fos.write(buff,0,len);
44+
}
45+
fis.close();
46+
fos.close();*/
47+
System.out.println("大炮揍了小明一顿");
48+
method.invoke(this.target,args);
49+
System.out.println("大炮再次打了小明一顿");
50+
return null;
51+
}
52+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.asher.proxy;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2019/1/17 20:58
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public interface Student {
12+
void say();
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.asher.proxy;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2019/1/17 21:03
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class Test {
12+
public static void main(String[] args) {
13+
Student sd = (Student) new ProxyStudent().getProxyInstance(new XiaoMing());
14+
sd.say();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.asher.proxy;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2019/1/17 21:01
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class XiaoMing implements Student{
12+
@Override
13+
public void say() {
14+
System.out.println("小明说他很叼");
15+
}
16+
}

java-reflect/src/com/zyj/reflect/Demo02.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void main(String[] args) throws NoSuchFieldException, NoSuchMethod
2121
try {
2222
Class clazz = Class.forName(path);
2323
//获取类的名字
24-
System.out.println(clazz.getName());//获取包名+类名:com.zyj.bean
24+
System.out.println(clazz.getName());//获取包名+类名:com.zyj.bean
2525
System.out.println(clazz.getSimpleName()); //获取类名:User
2626
//获取属性信息
2727
//Field[] fields = clazz.getFields();//此方法只能获得public的field

java-reflect/src/com/zyj/reflect/Demo04.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,16 @@ public static void main(String[] args) throws NoSuchMethodException {
3838
}
3939
}
4040
}
41+
42+
Method m2 = Demo04.class.getMethod("test02");
43+
Type returnType = m2.getGenericReturnType();
44+
System.out.println(returnType);
45+
46+
if(returnType instanceof ParameterizedType){
47+
Type[] genericTypes = ((ParameterizedType) returnType).getActualTypeArguments();
48+
for(Type t:genericTypes){
49+
System.out.println("--"+t);
50+
}
51+
}
4152
}
4253
}

0 commit comments

Comments
 (0)