Skip to content

Commit a1e8f94

Browse files
committed
reflection
1 parent b8e7f38 commit a1e8f94

8 files changed

Lines changed: 94 additions & 8 deletions

File tree

CoreJava/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
43
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
55
<classpathentry kind="output" path="bin"/>
66
</classpath>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chap5;
1+
package v1chap05;
22

33
import java.time.LocalDate;
44
import java.util.Objects;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chap5;
1+
package v1chap05;
22

33
/**
44
* this programe demonstrates inheritance
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chap5;
1+
package v1chap05;
22

33
public class EnumTest {
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chap5;
1+
package v1chap05;
22

33
import java.util.Objects;
44

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chap5;
1+
package v1chap05;
22

33
import java.lang.reflect.Constructor;
44
import java.lang.reflect.Field;
@@ -64,10 +64,10 @@ public static void printConstructors(Class cl) {
6464
Class[] paramTypes = c.getParameterTypes();
6565
for(int j = 0; j < paramTypes.length; j++) {
6666
if(j > 0)
67-
System.out.print(", ");
67+
System.out.print(", "); //first one "0" don't print
6868
System.out.print(paramTypes[j].getName());
6969
}
70-
System.out.println("),");
70+
System.out.println(");");
7171
}
7272
}
7373

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package v1chap05.objectAnalyzer;
2+
3+
import java.lang.reflect.Array;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Modifier;
6+
import java.util.ArrayList;
7+
import java.lang.reflect.AccessibleObject;
8+
9+
/**
10+
* Convert an object to a string represtation that lists all fields.
11+
* @author Code
12+
*
13+
*/
14+
15+
public class ObjectAnalyzer {
16+
private ArrayList<Object> visited = new ArrayList<>();
17+
18+
public String toString(Object obj) {
19+
if(obj == null) return "null";
20+
if(visited.contains(obj)) return "...";
21+
visited.add(obj);
22+
Class cl = obj.getClass();
23+
if(cl == String.class) return (String) obj;
24+
if(cl.isArray()) {
25+
String r = cl.getComponentType() + "[]{";
26+
for(int i = 0; i < Array.getLength(obj); i++) {
27+
if(i > 0) r += ",";
28+
Object val = Array.get(obj, i);
29+
if(cl.getComponentType().isPrimitive()) r += val;
30+
else r += toString(val); //递归
31+
}
32+
return r + "}";
33+
}
34+
35+
String r =cl.getName();
36+
//inspect the fields of this class and superclass
37+
do {
38+
r += "[";
39+
Field[] fields = cl.getDeclaredFields();
40+
//屏蔽Java语言的访问检查,使对象的私有属性也可以被查询设置
41+
AccessibleObject.setAccessible(fields, true);
42+
//get the names and values of all fields
43+
for(Field f: fields) {
44+
if(!Modifier.isStatic(f.getModifiers())) {
45+
if(!r.endsWith("[")) r += ",";
46+
r += f.getName() + "=";
47+
try {
48+
Class t = f.getType();
49+
Object val = f.get(obj);
50+
if(t.isPrimitive()) r += val;
51+
else r +=toString(val);
52+
}
53+
catch (Exception e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
}
58+
r += "]";
59+
cl = cl.getSuperclass();
60+
}
61+
while(cl != null);
62+
return r;
63+
}
64+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package v1chap05.objectAnalyzer;
2+
3+
import java.util.ArrayList;
4+
5+
import com.sun.org.apache.bcel.internal.generic.NEW;
6+
7+
/**
8+
* This program use reflection to spy on objects.
9+
* @author Code
10+
*
11+
*/
12+
13+
public class ObjectAnalyzerTest {
14+
public static void main(String[] args) {
15+
ArrayList<Integer> squares = new ArrayList<>();
16+
for(int i = 1; i <= 5; i++) {
17+
squares.add(i * i);
18+
}
19+
System.out.println(new ObjectAnalyzer().toString(squares));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)