java反射简单记录

1. Java 反射API的第一个主要作用是获取程序在运行时刻的内部结构。这对于程序的检查工具和调试器来说,是非常实用的功能。只需要短短的十几行代码,就可以遍历出来一个Java类的内部结构,包括其中的构造方法、声明的域和定义的方法等。这不得不说是一个很强大的能力。只要有了java.lang.Class类 的对象,就可以通过其中的方法来获取到该类中的构造方法、域和方法。对应的方法分别是getConstructor、getField和getMethod。这三个方法还有相应的getDeclaredXXX版本,区别在于getDeclaredXXX版本的方法只会获取该类自身所声明的元素,而不会考虑继承下来的。Constructor、Field和Method这三个类分别表示类中的构造方法、域和方法。这些类中的方法可以获取到所对应结构的元数据。

2.待测试类
Java代码
  1. packagecom.home.action.test.mode.reflection;
  2. importjava.util.Map;
  3. publicclassCounter{
  4. publicintcount;
  5. publicMap<String,Object>map;
  6. publicCounter(intcount){
  7. this.count=count;
  8. }
  9. publicvoidincrease(intstep){
  10. count=count+step;
  11. System.out.println("count:"+count);
  12. }
  13. }
package com.home.action.test.mode.reflection;

import java.util.Map;

public class Counter {
    public int count ;
    public Map<String, Object> map;
    
    public Counter(int count) {
        this.count = count;
    }
    
    public void increase(int step) {
        count = count + step;
        System.out.println("count: "+count);
    }
}

3.测试方法
Java代码
  1. packagecom.home.action.test.mode.reflection;
  2. importjava.lang.reflect.Constructor;
  3. importjava.lang.reflect.Field;
  4. importjava.lang.reflect.InvocationTargetException;
  5. importjava.lang.reflect.Method;
  6. importjava.lang.reflect.ParameterizedType;
  7. importjava.lang.reflect.Type;
  8. /**
  9. *测试基本类的反射
  10. *@authorli
  11. */
  12. publicclassTest{
  13. @SuppressWarnings("rawtypes")
  14. publicstaticvoidmain(String[]args){
  15. try{
  16. //构造函数
  17. Constructor<Counter>contructor=Counter.class.getConstructor(int.class);
  18. System.out.println("contructorName:"+contructor.getName());
  19. Countercounter=contructor.newInstance(10);
  20. counter.increase(10);
  21. //基本方法
  22. Method[]methods=Counter.class.getMethods();
  23. for(Methodmethod:methods){
  24. System.out.println("method:"+method.getName());
  25. }
  26. Methodmethod=Counter.class.getMethod("increase",int.class);
  27. method.invoke(counter,6);
  28. //属性值
  29. Field[]fields=Counter.class.getFields();
  30. for(Fieldfield:fields){
  31. System.out.println("fieldName:"+field.getName());
  32. }
  33. //count类型要设置public否则获取不到,提示异常信息
  34. Fieldfield=Counter.class.getField("count");
  35. System.out.println("countField:"+field.getName());
  36. //处理泛型
  37. FieldmapField=Counter.class.getDeclaredField("map");
  38. Typetype=mapField.getGenericType();
  39. System.out.println("mapType:"+type.toString());
  40. if(typeinstanceofParameterizedType){
  41. ParameterizedTypeparamerizedType=(ParameterizedType)type;
  42. Type[]actualTypes=paramerizedType.getActualTypeArguments();
  43. for(Typet:actualTypes){
  44. if(tinstanceofClass){
  45. Classclz=(Class)t;
  46. System.out.println("classType:"+clz.getName());
  47. }
  48. }
  49. }
  50. }catch(SecurityExceptione){
  51. e.printStackTrace();
  52. }catch(NoSuchMethodExceptione){
  53. e.printStackTrace();
  54. }catch(IllegalArgumentExceptione){
  55. e.printStackTrace();
  56. }catch(InstantiationExceptione){
  57. e.printStackTrace();
  58. }catch(IllegalAccessExceptione){
  59. e.printStackTrace();
  60. }catch(InvocationTargetExceptione){
  61. e.printStackTrace();
  62. }catch(NoSuchFieldExceptione){
  63. e.printStackTrace();
  64. }
  65. }
  66. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值