Skip to content

Commit 4027974

Browse files
committed
🔖 反射示例
1 parent 93d5c1f commit 4027974

4 files changed

Lines changed: 128 additions & 0 deletions

File tree

codes/advanced/src/test/java/io/github/dunwu/javase/reflect/ReflectClassTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package io.github.dunwu.javase.reflect;
22

3+
import java.lang.reflect.Constructor;
34
import java.lang.reflect.Field;
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.util.ArrayList;
47
import java.util.HashSet;
8+
import java.util.List;
59
import java.util.Set;
610
import org.junit.Test;
711

@@ -102,4 +106,31 @@ public void demo5() throws NoSuchFieldException {
102106
Class c3 = Thread.State.class.getEnclosingClass();
103107
System.out.println(c3.getCanonicalName());
104108
}
109+
110+
@Test
111+
public void demo06() {
112+
ArrayList arrayList = new ArrayList();
113+
if (arrayList instanceof List) {
114+
System.out.println("ArrayList is List");
115+
}
116+
if (List.class.isInstance(arrayList)) {
117+
System.out.println("ArrayList is List");
118+
}
119+
}
120+
121+
@Test
122+
public void demo07()
123+
throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
124+
Class<?> c1 = String.class;
125+
Object str1 = c1.newInstance();
126+
System.out.println(str1.getClass().getCanonicalName());
127+
128+
//获取String所对应的Class对象
129+
Class<?> c2 = String.class;
130+
//获取String类带一个String参数的构造器
131+
Constructor constructor = c2.getConstructor(String.class);
132+
//根据构造器创建实例
133+
Object obj = constructor.newInstance("bbb");
134+
System.out.println(obj.getClass().getCanonicalName());
135+
}
105136
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.dunwu.javase.reflect;
2+
3+
import java.io.FileOutputStream;
4+
import java.lang.reflect.Constructor;
5+
import org.junit.Test;
6+
7+
/**
8+
* @author Zhang Peng
9+
* @date 2018/6/5
10+
*/
11+
public class ReflectConstructorTest {
12+
@Test
13+
public void demo1() throws NoSuchMethodException {
14+
Constructor<?>[] constructors1 = FileOutputStream.class.getDeclaredConstructors();
15+
System.out.println("FileOutputStream getDeclaredConstructors 清单(数量 = " + constructors1.length + "):");
16+
for (Constructor c : constructors1) {
17+
System.out.println(c);
18+
}
19+
20+
Constructor<?>[] constructors2 = FileOutputStream.class.getConstructors();
21+
System.out.println("FileOutputStream getConstructors 清单(数量 = " + constructors2.length + "):");
22+
for (Constructor c : constructors2) {
23+
System.out.println(c);
24+
}
25+
26+
System.out.println("====================");
27+
Constructor constructor = FileOutputStream.class.getConstructor(String.class, boolean.class);
28+
System.out.println(constructor);
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.dunwu.javase.reflect;
2+
3+
import java.lang.reflect.Field;
4+
import java.util.List;
5+
import org.junit.Test;
6+
7+
/**
8+
* @author Zhang Peng
9+
* @date 2018/6/5
10+
*/
11+
public class ReflectFieldTest {
12+
class FieldSpy<T> {
13+
public boolean[][] b = {{ false, false }, { true, true } };
14+
public String name = "Alice";
15+
public List<Integer> list;
16+
public T val;
17+
}
18+
19+
@Test
20+
public void demo1() {
21+
try {
22+
Field f1 = FieldSpy.class.getField("b");
23+
System.out.format("Type: %s%n", f1.getType());
24+
25+
Field f2 = FieldSpy.class.getField("name");
26+
System.out.format("Type: %s%n", f2.getType());
27+
28+
Field f3 = FieldSpy.class.getField("list");
29+
System.out.format("Type: %s%n", f3.getType());
30+
31+
Field f4 = FieldSpy.class.getField("val");
32+
System.out.format("Type: %s%n", f4.getType());
33+
} catch (NoSuchFieldException x) {
34+
x.printStackTrace();
35+
}
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.dunwu.javase.reflect;
2+
3+
import java.lang.reflect.Method;
4+
import org.junit.Test;
5+
6+
/**
7+
* @author Zhang Peng
8+
* @date 2018/6/5
9+
*/
10+
public class ReflectMethodTest {
11+
@Test
12+
public void demo1() throws NoSuchMethodException {
13+
// getDeclaredMethods()方法返回类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。
14+
Method[] methods1 = Thread.class.getDeclaredMethods();
15+
System.out.println("Thread getDeclaredMethods 清单(数量 = " + methods1.length + "):");
16+
for (Method m : methods1) {
17+
System.out.println(m);
18+
}
19+
20+
// getMethods() 方法返回某个类的所有公用(public)方法,包括其继承类的公用方法。
21+
Method[] methods2 = Thread.class.getMethods();
22+
System.out.println("Thread getMethods 清单(数量 = " + methods2.length + "):");
23+
for (Method m : methods2) {
24+
System.out.println(m);
25+
}
26+
27+
Method method = Thread.class.getMethod("join", long.class, int.class);
28+
System.out.println(method);
29+
}
30+
}

0 commit comments

Comments
 (0)