Java-注解和反射


Java-注解和反射

内置注解

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
import java.util.ArrayList;
import java.util.List;

public class Test01 extends Object{

//@Override 重写的注解
@Override
public String toString(){
return super.toString();
}

//Deprecated
@Deprecated
public static void test(){
System.out.println("Deprecated");
}

@SuppressWarnings("all")
public void test02(){
List list = new ArrayList();
}

public static void main(String[] args) {
test();
}
}

元注解

  • Java里有四个元注解,重点放在Target和Retention上
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
package annotations;

import java.lang.annotation.*;

//测试元注解
@MyAnnotation
public class Test02 {

@MyAnnotation
public void test(){

}

}

//定义一个注解
//Target 表示我们的注解可以用在什么地方
//ElementType.METHOD 代表可以用在方法上,ElementType.TYPE代表可以用在Class,interface上
@Target(value= {ElementType.METHOD,ElementType.TYPE})

//Retention 表示我们的注解在什么地方还有效
//runtime>class>source, 代表如果只定义了class那么在source也有效,在runtime无效
@Retention(value = RetentionPolicy.RUNTIME)

//Document 表示是否将我们的注解生成在JAVAdoc中
@Documented

//Inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation{

}

自定义注解

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
package annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//自定义注解
public class Test03 {
@MyAnnotation2(name = "门酱",schools = {"河北工业大学"})
public void test(){
}
//如果没有默认值,则必须给注解赋值
@MyAnnotation2(schools = {"河北工业大学"})
public void test2(){
}

@MyAnnotation3("门酱")
public void test3(){

}
}

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
//这是注解的参数
//参数类型+参数名()
String name() default "";
int age() default 0;
int id() default -1; //如果默认值为-1,代表不存在,indexof,如果找不到就返回-1
String[] schools();
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();
}

反射机制

Java不是动态语言(动态语言即可以在运行时代码根据某些条件改变自身结构),但因为反射机制,可以称之为准动态语言。这使得Java编程时更加灵活,但是风险也提升。

获得反射对象

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
package reflection;

//什么叫反射
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException {
//通过反射获取类的class对象
Class<?> c1 = Class.forName("reflection.User");
System.out.println(c1);

Class<?> c2 = Class.forName("reflection.User");
Class<?> c3 = Class.forName("reflection.User");
Class<?> c4 = Class.forName("reflection.User");

//一个类在内存中只有一个class对象
//一个类被加载后,类的整个结构都会被封装在class对象中。
System.out.println(c2.hashCode());
System.out.println(c3.hashCode());
System.out.println(c4.hashCode());

}

}

//实体类
class User{
private String name;
private int id;
private int age;
public String getName() {
return name;
}

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

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getAge() {
return age;
}

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

运行结果

1
2
3
4
class reflection.User
1915910607
1915910607
1915910607

得到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
57
58
package reflection;

public class Test03 {

public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
System.out.println("这个人是:"+person.name);
//方式一:通过对象获得
Class c1 = person.getClass();
System.out.println(c1.hashCode());

//方式二:forname获得
Class c2 = Class.forName("reflection.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 c5 = c1.getSuperclass();
System.out.println(c5);
}
}

class Person{
public String name;

public Person() {
}

public Person(String name) {
this.name = name;
}

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

class Student extends Person{
public Student(){
this.name = "学生";
}
}

class Teacher extends Person{
public Teacher(){
this.name = "老师";
}
}

运行结果

1
2
3
4
5
6
这个人是:学生
284720968
284720968
284720968
int
class reflection.Person

所有类型的Class对象

  • class:外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类
  • interface:接口
  • []:数组
  • enum:枚举
  • annotation:注解@interface
  • primitive type:基本数据类型
  • void
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
package reflection;

import javax.swing.text.Element;
import java.lang.annotation.ElementType;

//所有类型的Class对象
public class Test04 {
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
int[] a = new int[10];
int[] b = new int[100];
System.out.println(a.getClass().hashCode());
System.out.println(b.getClass().hashCode());
}
}

运行结果

1
2
3
4
5
6
7
8
9
class java.lang.Object
interface java.lang.Comparable
class [Ljava.lang.String;
class [[I
interface java.lang.Override
class java.lang.annotation.ElementType
class java.lang.Integer
void
class java.lang.Class

类加载内存分析

image-20230630211743387

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Test05 {
public static void main(String[] args) {
A a = new A();
System.out.println(A.m);
}
}

class A{
static {
System.out.println("A类静态代码块初始化");
m=300;
}
static int m =100;
public A(){
System.out.println("A类的无参构造初始化");
}

}

运行结果

1
2
3
A类静态代码块初始化
A类的无参构造初始化
100

注意修改static int m =100; 放到static代码块前面,m最终是300

分析类初始化

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
package reflection;

public class Test06 {

static {
System.out.println("Main类被加载");
}

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

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


//不会产生类的引用的方法
// System.out.println(Son.b);
/**
* Main类被加载
* 父类被加载
* 2
* **/
// Son[] array = new Son[5];
/**
* Main类被加载
* **/

System.out.println(Son.M);
/**
* Main类被加载
* 1
* **/
}

}

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

class Son extends Father{
static {
System.out.println("子类被加载");
m = 300;
}
static int m = 100;
static final int M=1;
}

Author: Liang Junyi
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Liang Junyi !
  TOC