java 反射机制

2025-05-29 0 43

本文导引:

通过反射机制

  • 获取类的基本信息
  • 获取类的注解信息
  • 获取泛型信息
?

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

@AnnotationUserTable("datebaseExample")

public class User {

@AnnotationUserField(uName="name",type="varchar",length=10)

private String name;

@AnnotationUserField(uName="age",type="int",length=3)

private int age;

@AnnotationUserField(uName="sex",type="char",length=2)

private String sex;

public User() {

super();

}

public User(String name, int age, String sex) {

super();

this.name = name;

this.age = age;

this.sex = sex;

}

public String getName() {

return name;

}

public void setName() {

this.name = "test";

}

public int getAge() {

return age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

}

bean:User

?

1

2

3

4

5

6

7

8

9

10

11

12
package reflection;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(value={ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationUserTable {

String value();

}

自定义注解:类注解

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
package reflection;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(value={ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationUserField {

String uName();

String type();

int length();

}

自定义注解:属性注解

?

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

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class Demo01 {

static Class<?> c = null;

public static void main(String[] args) {

try {

c = Class.forName("reflection.User");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

test();//获取类的属性、方法等信息

}

static void test(){

try {

// 获取类的名称

System.out.println("获取类的名称");

System.out.println("getName():" + c.getName());// 获得包名+类名

System.out.println("getSimpleName():" + c.getSimpleName());// 获得类名

System.out.println("getCanonicalName():" + c.getCanonicalName());// 获得类名

System.out.println("*******************************");

// 获取属性信息

System.out.println("获取属性信息");

Field[] fields = c.getDeclaredFields();

// Field[] fields = c.getFields(); 只能获取public修饰的属性信息

for (Field f : fields) {

String fName = f.getName();

System.out.println(c.getDeclaredField(fName));

}

System.out.println("*******************************");

// 获取方法信息

System.out.println("获取方法信息");

Method[] methods = c.getDeclaredMethods();

for (Method m : methods) {

// String mName = m.getName();

System.out.println(m.getName() + "-->" + m);

}

System.out.println("通过名称单独获取对应的getName方法:" + c.getDeclaredMethod("getName"));

System.out.println("通过名称单独获取对应的setSex方法:" + c.getDeclaredMethod("setSex", String.class));// 方法有参,必须传递参数类型

System.out.println("*******************************");

// 获取构造器信息

System.out.println("获取构造器信息");

Constructor<?>[] constructor = c.getConstructors();

for (Constructor<?> cons : constructor) {

System.out.println(cons);

}

} catch (NoSuchFieldException | SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

}

}

main1:通过反射机制获取类的基本信息

output:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
获取类的名称

getName():reflection.User

getSimpleName():User

getCanonicalName():reflection.User

*******************************

获取属性信息

private java.lang.String reflection.User.name

private int reflection.User.age

private java.lang.String reflection.User.sex

*******************************

获取方法信息

getName-->public java.lang.String reflection.User.getName()

setName-->public void reflection.User.setName()

setSex-->public void reflection.User.setSex(java.lang.String)

getSex-->public java.lang.String reflection.User.getSex()

getAge-->public int reflection.User.getAge()

通过名称单独获取对应的getName方法:public java.lang.String reflection.User.getName()

通过名称单独获取对应的setSex方法:public void reflection.User.setSex(java.lang.String)

*******************************

获取构造器信息

public reflection.User()

public reflection.User(java.lang.String,int,java.lang.String)

View Console

下面的例子,是通过反射机制获取类的注解信息。

?

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

import java.lang.reflect.Field;

/**

* 获取类的属性、方法等信息

* 1.获取元素对象(如属性)(注意:读取类的注解,看似要少一步)

* 2.获取该元素对象的指定类型的注解对象

* 3.读取注解对象相应的值

*/

public class Test02 {

static Class<?> c = null;

public static void main(String[] args) {

try {

c = Class.forName("reflection.User");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

test();

}

static void test(){

try {

// 获取类的指定注解

System.out.println("***********类的指定注解**************");

AnnotationUserTable table = (AnnotationUserTable)c.getAnnotation(AnnotationUserTable.class);

System.out.println(table.value());

// 获取属性的指定注解

System.out.println("***********属性的指定注解*************");

Field field = c.getDeclaredField("name");

AnnotationUserField annoField = (AnnotationUserField)field.getAnnotation(AnnotationUserField.class);

System.out.println(annoField.uName()+"\\t"+annoField.type()+"\\t"+annoField.length());

// 根据获得的表名、字段的信息,拼写出DDL语句,然后通过JDBC连接数据库查询

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

}

}

}

output:

?

1

2

3

4
***********类的指定注解**************

datebaseExample

***********属性的指定注解*************

name varchar 10

下面的例子,是通过反射机制获取泛型信息

?

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

import java.lang.reflect.Method;

import java.lang.reflect.Type;

import java.util.List;

import java.util.Map;

/**

* 通过反射机制获取泛型

* @author Administrator

*

*/

public class Test03 {

public static void main(String[] args) {

Class<?> c = Test03.class;

try {

System.out.println("*******获取参数值的类型**********");

Method m1 = c.getDeclaredMethod("method01", Map.class,List.class);

Type[] types = m1.getGenericParameterTypes();

for(Type t:types){

System.out.println(t.getTypeName());

System.out.println(t.toString());

}

System.out.println("*******获取返回值的类型**********");

Method m2 = c.getDeclaredMethod("method02");

Type ret = m2.getGenericReturnType();

System.out.println(ret.getTypeName());

System.out.println(ret.toString());

} catch (NoSuchMethodException | SecurityException e) {

e.printStackTrace();

}

}

public void method01(Map<String,String> args1,List<Integer> args2){

}

public Map<String,String> method02(){

return null;

}

}

通过反射机制获取泛型信息

output:

?

1

2

3

4

5

6

7

8
java.util.Map<java.lang.String, java.lang.String>

java.util.Map<java.lang.String, java.lang.String>

java.util.Map<java.lang.String, java.lang.String>

java.util.Map<java.lang.String, java.lang.String>

java.util.List<java.lang.Integer>

java.util.List<java.lang.Integer>

View Console

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持快网idc!

原文链接:http://www.cnblogs.com/fhw-space/p/6367325.html#start

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 反射机制 https://www.kuaiidc.com/118841.html

相关文章

发表评论
暂无评论