反射

概念:类加载之后,在堆内存的方法区中就生产了一个Class类型的对象,一个类只有一个Class对象,这个对象就包含了完整的类的结构信息。我们可以通过这个对象看到类的结构,这个对象就像是一面镜子,透过这个镜子看到类的结构,称之为反射

正常方式:引入需要的”包类”名称 ==> 通过new实例化 ==>获取实例化对象
反射方式:实例化对象 ==> getClass()方法 ==> 取得完整的”包类”名称

  • 优点:可以实现动态创建对象和编译,体现出很大的灵活性
  • 缺点:对性能有影响。使用反射基本上是一种解释操作,我们告诉JVM要做什么,这类操作总是慢于直接执行相同的操作
    一个类只有一个Class对象

获取Class对象的几种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 测试类
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException {
Person student = new Studnet();

// 方式一:通过对象获取
Class c1 = student.getClass();
System.out.println(c1.hashCode());

// 方式二:forName获取
Class c2 = Class.forName("Student");
System.out.println(c2.hashCode());

// 方式三:通过类名.class获取
Class c3 = Student.class;
System.out.println(c3.hashCode());

// 方式四:对于基本数据类型,可以使用其包装类的TYPE属性获取
Class c4 = Integer.TYPE;
System.out.println(c4);
}
}

class Studnet extends Person{

}

class Person {

}

所有类型的Class对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 所有类型的Class
public class Test02 {
public static void main(String[] args) {
Class c1 = Object.class; //类
Class c2 = Comparable.class; //接口
Class c3 = String[].class; //一维数组
Class c4 = int[][].class; //二维数组
Class c5 = Override.class; //注解
Class c6 = ElementType.class; //枚举
Class c7 = Integer.class; //基本数据类型
Class c8 = void.class; //void
Class c9 = Class.class; //Class

System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
System.out.println(c6);
System.out.println(c7);
System.out.println(c8);
System.out.println(c9);
}
}

Class对象获取类信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Class获取类的信息
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class.forName("Student");

System.out.println(c1.getName()); // 获得包名 + 类名
System.out.println(c1.getSimpleName()); // 获得类名

// 获得类的public属性
Field[] fields = c1.getFields();
for (Field field : fields) {
System.out.println(field);
}
// 获取类的所有属性
fields = c1.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}

// 获取指定属性
Field name = c1.getDeclaredField("name");
System.out.println(name);

//获取类的方法:除了类自身的方法外,还会获取所继承的类中的方法
System.out.println("===============================");
Method[] methods = c1.getMethods();
for (Method method : methods) {
System.out.println(method);
}
methods = c1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}

// 获取指定方法
Method getName = c1.getMethod("getName",null);
Method setName = c1.getMethod("setName", String.class);
System.out.println(getName);
System.out.println(setName);

// 获取构造器
System.out.println("===============================");
Constructor[] constructors = c1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
constructors = c1.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}

// 获取指定构造器
Constructor declaredConstructor = c1.getDeclaredConstructor(String.class,int.class);
System.out.println(declaredConstructor);
}
}

类的加载过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 测试类的初始化过程
public class Test02 {
static {
System.out.println("Main类被加载");
}

public static void main(String[] args) throws ClassNotFoundException {
// 主动引用
//Son son = new Son();

// 反射也会产生主动引用
Class.forName("Son");

// 被动引用不会引起类的初始化
System.out.println(Son.b);

Son[] array = new Son[5];

System.out.println(Son.M);
}
}

class Father {
static int b = 2;
static {
System.out.println("父类被加载");
}
}

class Son extends Father {
static {
System.out.println("子类被加载");
}

static int m = 100;
static final int M = 1;
}

类加载器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 测试类的初始化过程
public class Test02 {

public static void main(String[] args) throws ClassNotFoundException {
// 获取系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);

// 获取系统类加载器的父类加载器(扩展类加载器)
ClassLoader parent = systemClassLoader.getParent();
System.out.println(parent);

//获取扩展类加载器的父类加载器(根加载器)
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);

//测试当前类是哪个加载器加载的
ClassLoader classLoader = Class.forName("Test02").getClassLoader();
System.out.println(classLoader);

//测试JDK内置的类是哪个加载器加载的
classLoader = Class.forName("java.lang.Object").getClassLoader();
System.out.println(classLoader);

}
}

双亲委派机制

  • 描述:某个特定的类加载器在接到加载类的请求时,首先将加载任务委托给父类加载器,依次递归,如果父类加载器可以完成类加载任务,就成功返回;只有父类加载器无法完成此加载任务时,才自己去加载。

  • 意义:防止内存中出现多份同样的字节码

反射操作对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 通过反射,动态创建对象
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
// 获得Class对象
Class c1 = Class.forName("Student");

// 通过反射构建一个对象
Student student = (Student) c1.newInstance();

// 通过构造器构建对象
Constructor constructor = c1.getDeclaredConstructor(String.class,int.class);
Student student1 = (Student)constructor.newInstance("小明",18);

// 通过反射直接调用方法
Student student2 = (Student) c1.newInstance();
// 通过反射获取一个方法
Method setName = c1.getDeclaredMethod("setName",String.class);
// invoke激活获取的方法
setName.invoke(student2,"张三");
System.out.println(student2);

// 通过反射操作属性
Student student3 = new Student();
Field name = c1.getDeclaredField("name");
// 对于私有属性,不能直接操作,需要关闭程序的安全检测,true为关闭
name.setAccessible(true);
name.set(student3,"李四");
System.out.println(student3);
}
}

反射操作泛型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 反射操作泛型
public class Test02 {
public static void main(String[] args) throws NoSuchMethodException {
Method method = Test02.class.getMethod("test1", Map.class, List.class);
// 获得泛型参数列表
Type[] genericParameterTypes = method.getGenericParameterTypes();
for (Type genericParameterType : genericParameterTypes) {
System.out.println(genericParameterType);
if (genericParameterType instanceof ParameterizedType) {
// 遍历参数列表内的泛型信息
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}

method = Test02.class.getMethod("test2",null);
// 获取泛型返回值
Type genericReturnType = method.getGenericReturnType();
if (genericReturnType instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
// 遍历返回值内的泛型参数
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}

}

public void test1(Map<String,String> map, List<String> list) {
System.out.println("test01");
}

public List<String> test2() {
return null;
}
}

反射操作注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 反射操作泛型
public class Test02 {
public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("Student");
// 获取注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}

// 获取指定的类注解的值
MyAnnotation annotation = (MyAnnotation) c1.getAnnotation(MyAnnotation.class);
System.out.println(annotation.value());

// 获取指定的属性注解的值
Field f = c1.getDeclaredField("name");
MyAnnotation2 annotation1 = f.getAnnotation(MyAnnotation2.class);
System.out.println(annotation1.columnName());
System.out.println(annotation1.type());
System.out.println(annotation1.length());
}
}

@MyAnnotation("student")
class Student {
@MyAnnotation2(columnName = "name",type = "String",length = 10)
private String name;
@MyAnnotation2(columnName = "age",type = "int",length = 4)
private int age;

public Student() {
}

public Student(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

// 类的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value();
}

// 属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
String columnName();
String type();
int length();
}