Skip to content

Commit 4bfccad

Browse files
committed
Polish Stage 7 Lesson 1
1 parent fc4c238 commit 4bfccad

File tree

16 files changed

+613
-0
lines changed

16 files changed

+613
-0
lines changed

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>stage-3</module>
1616
<module>stage-4</module>
1717
<module>stage-5</module>
18+
<module>stage-7</module>
1819
</modules>
1920

2021
<build>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>deep-in-java</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-7</artifactId>
13+
<packaging>pom</packaging>
14+
<name>「一入 Java 深似海 」系列 :: 第七期</name>
15+
<modules>
16+
<module>stage-7-lesson-1</module>
17+
</modules>
18+
19+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>stage-7</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-7-lesson-1</artifactId>
13+
<name>「一入 Java 深似海 」系列 :: 第七期 :: 第一节</name>
14+
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.segmentfault.deep.in.java.reflection;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.function.Consumer;
5+
import java.util.function.Supplier;
6+
7+
public class AccessibleObjectDemo {
8+
9+
public static void main(String[] args) throws Exception {
10+
11+
// AccessibleObject 是 Field、Method 和 Constructor 父类
12+
// 访问性检查 isAccessible()
13+
// 修改访问性 setAccessible(boolean)
14+
15+
String value = "Hello,World";
16+
17+
execute(() -> value.toString());
18+
19+
Method toStringMethod = String.class.getMethod("toString");
20+
21+
// 预热
22+
execute(() -> {
23+
try {
24+
// 默认情况任何 AccessibleObject 对象都会做访问性检查
25+
toStringMethod.invoke(value);
26+
} catch (Exception e) {
27+
}
28+
});
29+
30+
execute(() -> {
31+
try {
32+
// 默认情况任何 AccessibleObject 对象都会做访问性检查
33+
toStringMethod.setAccessible(true);
34+
toStringMethod.invoke(value);
35+
} catch (Exception e) {
36+
}
37+
});
38+
39+
execute(() -> {
40+
try {
41+
// 默认情况任何 AccessibleObject 对象都会做访问性检查
42+
// 因此,关闭此检查,可以提升性能
43+
toStringMethod.setAccessible(false);
44+
toStringMethod.invoke(value);
45+
} catch (Exception e) {
46+
}
47+
});
48+
// 反射调用
49+
50+
}
51+
52+
private static void execute(Runnable runnable) {
53+
int times = 100000;
54+
long startTime = System.currentTimeMillis();
55+
for (int i = 0; i < times; i++) {
56+
runnable.run();
57+
}
58+
long costTime = System.currentTimeMillis() - startTime;
59+
System.out.printf("执行 %d 时间消耗:%d ms\n", times, costTime);
60+
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.segmentfault.deep.in.java.reflection;
2+
3+
public class ClassMemberDemo {
4+
5+
6+
class A {
7+
8+
protected String value; // 一个类中可以定义唯一名称的字段
9+
10+
}
11+
12+
class B extends A {
13+
14+
protected String value;
15+
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.segmentfault.deep.in.java.reflection;
2+
3+
import java.beans.ConstructorProperties;
4+
import java.io.Serializable;
5+
import java.lang.reflect.Modifier;
6+
import java.util.AbstractList;
7+
8+
/**
9+
* Java 类对象
10+
*/
11+
public class ClassObjectDemo {
12+
13+
public static void main(String[] args) {
14+
// Class 对象
15+
// 具体类对象
16+
// 没有这类方法 isConcrete 是否为具体类
17+
System.out.println(!Modifier.isAbstract(Object.class.getModifiers()));
18+
// 抽象类对象
19+
System.out.println(Modifier.isAbstract(AbstractList.class.getModifiers()));
20+
// 接口类对象
21+
System.out.println(Serializable.class.isInterface());
22+
// 枚举类对象
23+
System.out.println(Color.class.isEnum());
24+
// 注解类对象
25+
System.out.println(ConstructorProperties.class.isAnnotation());
26+
// 原生类对象
27+
System.out.println(int.class.isPrimitive());
28+
System.out.println(double.class.isPrimitive());
29+
// 数组类对象
30+
System.out.println(int[].class.isArray());
31+
System.out.println(Object[].class.isArray());
32+
// 特殊类型
33+
System.out.println(void.class);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.segmentfault.deep.in.java.reflection;
2+
3+
public enum Color { // public enum Color = public final class Color extends java.lang.Enum
4+
RED, // RED = public static final Color = new Color("RED",1)
5+
YELLOW, // YELLOW = public static final Color = new Color("YELLOW",2)
6+
BLUE // BLUE = public static final Color = new Color("BLUE",3)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.segmentfault.deep.in.java.reflection;
2+
3+
public class JavaArrayDemo {
4+
5+
public static void main(String[] args) {
6+
int[] values = new int[0]; // 数组是 Java 对象
7+
// values.length; // length 字段也是虚拟机帮助提供
8+
// values 成员类型 = int.class
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.segmentfault.deep.in.java.reflection;
2+
3+
import java.lang.reflect.Field;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
import static java.util.Arrays.asList;
12+
13+
public class JavaClassMemberDemo {
14+
15+
private int value;
16+
17+
public static void main(String[] args) {
18+
// Java 类成员对象来自于 java.lang.Class
19+
// 成员访问性
20+
// 所有成员 getDeclaredXXX() ->
21+
// getDeclaredMethods()
22+
// getDeclaredFields()
23+
// getDeclaredConstructors
24+
25+
// getDeclaredFields() - 所有声明的字段(仅限当前类) 与 getFields() - public 字段(仅限当前类)区别
26+
List<Field> allDeclaredFields = getAllDeclaredFields(ExtendedData.class);
27+
Field[] declaredFields = ExtendedData.class.getDeclaredFields();
28+
Field[] fields = ExtendedData.class.getFields();
29+
System.out.println("ExtendedData 类所有层次的声明字段 : " + allDeclaredFields.stream().map(Field::getName).collect(Collectors.joining(",")));
30+
System.out.println("ExtendedData 类所有的声明字段 : " + Stream.of(declaredFields).map(Field::getName).collect(Collectors.joining(",")));
31+
System.out.println("ExtendedData 类所有的 public 字段 : " + Stream.of(fields).map(Field::getName).collect(Collectors.joining(",")));
32+
}
33+
34+
static List<Field> getAllDeclaredFields(Class<?> type) {
35+
if (Object.class.equals(type)) {
36+
return Collections.emptyList();
37+
}
38+
List<Field> allDeclaredFields = new LinkedList<>();
39+
Field[] declaredFields = type.getDeclaredFields();
40+
allDeclaredFields.addAll(asList(declaredFields));
41+
Class<?> superClass = type.getSuperclass();
42+
43+
while (true) {
44+
if (superClass == null || Object.class.equals(superClass)) {
45+
break;
46+
}
47+
allDeclaredFields.addAll(getAllDeclaredFields(superClass));
48+
superClass = superClass.getSuperclass();
49+
}
50+
51+
return allDeclaredFields;
52+
}
53+
54+
public JavaClassMemberDemo() {
55+
56+
}
57+
58+
class Data {
59+
60+
private int value;
61+
62+
public int getValue() {
63+
return value;
64+
}
65+
66+
protected void setValue(int value) {
67+
this.value = value;
68+
}
69+
}
70+
71+
class ExtendedData extends Data {
72+
73+
private String name;
74+
75+
public void setName(String name) {
76+
this.name = name;
77+
}
78+
}
79+
80+
}
81+
82+

0 commit comments

Comments
 (0)