Skip to content

Commit dcdf7b2

Browse files
author
chenweijie
committed
添加反射相关的方法
1 parent 3b23458 commit dcdf7b2

File tree

13 files changed

+361
-2
lines changed

13 files changed

+361
-2
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
一、javac.exe、 java.exe、 java虚拟机三者之间的区别与联系:
1+
### javac.exe、 java.exe、 java虚拟机三者之间的区别与联系:
22

33
JDK中
4+
45
javac:Java编译器,将Java源代码换成字节代;
6+
57
java:Java解释器,直接从类文件执行Java应用程序代码;
68

79
先编译 *.java文件――――>*.class文件
10+
811
运行 *.class ――加载――> JVM
12+
913
jvm加载二进制文件
1014

1115
javac编译后得到的class文件是二进制指令,但不是机器指令,而是java虚拟机可识别的指令。这样class文件就有了可移植行。你可以把class文件拿到windows、linux或者solaris等不同的系统上去,在jvm上执行。
1216

13-
java是启动jvm,jvm负责对class文件的内容进行处理,将字节码文件解释或者编译为机器指令,执行。
17+
java是启动jvm,jvm负责对class文件的内容进行处理,将字节码文件解释或者编译为机器指令,执行。
18+
19+
### java虚拟机的体系结构
20+
21+
下图表示了Java虚拟机的结构框图,主要描述了JVM子系统和内存区。
22+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.chen.api.util.reflection;
2+
3+
/**
4+
* 汽车类
5+
*
6+
* @author chen weijie
7+
* @date 2017-12-08 12:09 AM
8+
*/
9+
public class Car {
10+
11+
private String brand;
12+
13+
private String color;
14+
15+
private Integer maxSpeed;
16+
17+
18+
public Car() {
19+
}
20+
21+
public Car(String brand, String color, Integer maxSpeed) {
22+
this.brand = brand;
23+
this.color = color;
24+
this.maxSpeed = maxSpeed;
25+
}
26+
27+
public String introduce() {
28+
return "Car{" +
29+
"brand='" + brand + '\'' +
30+
", color='" + color + '\'' +
31+
", maxSpeed=" + maxSpeed +
32+
'}';
33+
}
34+
35+
public String getBrand() {
36+
return brand;
37+
}
38+
39+
public void setBrand(String brand) {
40+
this.brand = brand;
41+
}
42+
43+
public String getColor() {
44+
return color;
45+
}
46+
47+
public void setColor(String color) {
48+
this.color = color;
49+
}
50+
51+
public Integer getMaxSpeed() {
52+
return maxSpeed;
53+
}
54+
55+
public void setMaxSpeed(Integer maxSpeed) {
56+
this.maxSpeed = maxSpeed;
57+
}
58+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.chen.api.util.reflection.ClassMethod;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.Method;
5+
6+
/**
7+
* 测试主类
8+
*
9+
* @author Chen WeiJie
10+
* @date 2017-12-22 12:33
11+
**/
12+
public class Main {
13+
14+
15+
public static Sub makeInstance(Class<?> clazz) {
16+
17+
Sub sub = null;
18+
try {
19+
sub = (Sub) clazz.newInstance();
20+
} catch (InstantiationException | IllegalAccessException e) {
21+
e.printStackTrace();
22+
}
23+
24+
return sub;
25+
26+
}
27+
28+
29+
public static void printBasicInfo(Class<?> clazz) {
30+
31+
System.out.println("CanonicalName:" + clazz.getCanonicalName());
32+
System.out.println("simpleName:" + clazz.getSimpleName());
33+
System.out.println("AnnotatedSuperclass:" + clazz.getAnnotatedSuperclass());
34+
35+
Class<?>[] interfaces = clazz.getInterfaces();
36+
for (Class<?> inter : interfaces) {
37+
System.out.println("Interface SimpleName : "
38+
+ inter.getSimpleName());
39+
}
40+
41+
Constructor<?>[] constructors = clazz.getConstructors();
42+
for (Constructor<?> cons : constructors) {
43+
System.out.println("Constructor Name : " + cons.getName()
44+
+ " And Parameter Count : " + cons.getParameterCount());
45+
}
46+
47+
Method[] methods = clazz.getMethods();
48+
49+
for (Method method : methods) {
50+
System.out.println("Method Name : " + method.getName());
51+
}
52+
53+
54+
System.out.println("AnnotatedSuperclass:" + clazz.getAnnotatedSuperclass());
55+
}
56+
57+
public static void main(String[] args) {
58+
59+
//Sub sub = new Sub();
60+
//Class<?> clazz = sub.getClass();
61+
Class<?> clazz = Sub.class;
62+
Sub instance = makeInstance(clazz);
63+
if (instance != null) {
64+
System.out.println("make instance successful");
65+
} else {
66+
System.out.println("make instance unsuccessful");
67+
}
68+
printBasicInfo(clazz);
69+
}
70+
71+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.chen.api.util.reflection.ClassMethod;
2+
3+
/**
4+
* sub类
5+
*
6+
* @author Chen WeiJie
7+
* @date 2017-12-22 12:32
8+
**/
9+
public class Sub extends SuperC implements SuperInterfaceA, SuperInterfaceB {
10+
11+
private String name;
12+
13+
public Sub() {
14+
super();
15+
}
16+
17+
public Sub(String name) {
18+
super(name);
19+
this.name = name;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.chen.api.util.reflection.ClassMethod;
2+
3+
/**
4+
* @author Chen WeiJie
5+
* @date 2017-12-22 12:28
6+
**/
7+
public class SuperC {
8+
9+
10+
private String name;
11+
12+
public SuperC() {
13+
14+
}
15+
16+
public SuperC(String name) {
17+
this.name = name;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return name;
23+
}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.chen.api.util.reflection.ClassMethod;
2+
3+
/**
4+
* super接口A
5+
*
6+
* @author Chen WeiJie
7+
* @date 2017-12-22 12:27
8+
**/
9+
public interface SuperInterfaceA {
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.chen.api.util.reflection.ClassMethod;
2+
3+
/**
4+
* super接口B
5+
*
6+
* @author Chen WeiJie
7+
* @date 2017-12-22 12:28
8+
**/
9+
public interface SuperInterfaceB {
10+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.chen.api.util.reflection;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.Method;
5+
6+
/**
7+
* 反射测试类
8+
*
9+
* @author chen weijie
10+
* @date 2017-12-08 12:09 AM
11+
*/
12+
public class ReflectionTest2 {
13+
14+
public static Car initByDefaultConst() throws Throwable {
15+
16+
//通过类装载器获取car类对象
17+
ClassLoader loader = Thread.currentThread().getContextClassLoader();
18+
Class clazz = loader.loadClass("com.chen.api.util.reflection.Car");
19+
20+
//获取类的默认构造器对象.并通过它实例化car
21+
Constructor constructor = clazz.getDeclaredConstructor((Class[]) null);
22+
Car car = (Car) constructor.newInstance();
23+
24+
//通过属性方法设置属性
25+
Method setBrand = clazz.getMethod("setBrand", String.class);
26+
setBrand.invoke(car, "红旗");
27+
28+
Method setColor = clazz.getMethod("setColor", String.class);
29+
setColor.invoke(car, "黑色");
30+
31+
Method setMaxSpeed = clazz.getMethod("setMaxSpeed", Integer.class);
32+
setMaxSpeed.invoke(car, 200);
33+
34+
return car;
35+
}
36+
37+
38+
public static void main(String[] args) throws Throwable {
39+
Car car = initByDefaultConst();
40+
41+
System.out.println(car.introduce());
42+
}
43+
44+
45+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.reflection.proxy;
2+
3+
4+
import java.lang.reflect.Proxy;
5+
6+
/**
7+
* 动态代理Main
8+
*
9+
* @author Chen WeiJie
10+
* @date 2017-12-22 14:21
11+
**/
12+
public class DynamicProxy {
13+
14+
public static void main(String[] args) {
15+
RealObject proxied = new RealObject();
16+
proxied.doSomething();
17+
proxied.doSomethingElse("chenweijie");
18+
19+
Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class
20+
.getClassLoader(), new Class[]{Interface.class}, new DynamicProxyHandler(proxied));
21+
proxy.doSomething();
22+
proxy.doSomethingElse("chenweijie");
23+
24+
}
25+
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.api.util.reflection.proxy;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
6+
/**
7+
* 动态代理处理类
8+
*
9+
* @author Chen WeiJie
10+
* @date 2017-12-22 14:17
11+
**/
12+
public class DynamicProxyHandler implements InvocationHandler {
13+
14+
15+
private Object proxied;
16+
17+
public DynamicProxyHandler(Object proxied) {
18+
this.proxied = proxied;
19+
}
20+
21+
22+
@Override
23+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
24+
25+
if (method.getName().startsWith("do")) {
26+
System.out.println("call do*** method");
27+
}
28+
29+
method.invoke(proxied, args);
30+
31+
return null;
32+
}
33+
34+
35+
}

0 commit comments

Comments
 (0)