超级通道: Java泛型学习系列-绪论

java.lang.reflect.Parameter类提供了用于获取和操作构造器的静态方法。

1.通过Parameter可以做什么

通过Parameter可以做以下事情:

  • 如何通过Class对象获取其方法或构造器的参数Parameter对象?
  • 如何通过Parameter获取相关信息:参数名、修饰符、参数类型、参数化类型、可变参数、注解

2.代码实例

实体类:

/*** <p>用户表</p>** @author hanchao 2018/2/14 22:30*/
public class User{public String username = "张三";private int password = 123456;/*** <p>测试:java反射-参数Parameter</p>** @author hanchao 2018/3/4 14:24**/public void initUser(@MyAnnotationA @MyAnnotationB String username, @MyAnnotationB String password) {}
}

实例类:

/*** Created by 韩超 on 2018/3/1.*/
public class ReflectParameterDemo {private final static Logger LOGGER = Logger.getLogger(ReflectParameterDemo.class);/*** <p>Title: java反射-参数Parameter</p>** @author 韩超 2018/3/1 15:56*/public static void main(String[] args) throws NoSuchMethodException {//===================================== 通过Class对象获取Parameter对象 =====================================LOGGER.info("===================================== 通过Class对象获取Parameter对象 =====================================");//首先获取Class对象Class userClass = User.class;LOGGER.info("首先获取Class对象:" + userClass);//然后获取Method(或者Constructor)对象Method method = userClass.getDeclaredMethod("initUser", String.class, String.class);LOGGER.info("然后获取Method或者Constructor对象:" + method);//然后获取Parameter对象数组Parameter[] parameters = method.getParameters();LOGGER.info("然后通过getParameters()获取Parameter对象数组");//然后获取Parameter对象Parameter parameter = parameters[0];LOGGER.info("最终获得参数Parameter对象" + parameter + "\n");//===================================== Parameter信息获取 =====================================LOGGER.info("===================================== Parameter信息获取 =====================================");LOGGER.info("通过parameter.getModifiers()获取参数修饰符:" + Modifier.toString(parameter.getModifiers()));LOGGER.info("通过parameter.getName()获取参数名:" + parameter.getName());LOGGER.info("通过parameter.getParameterizedType()获取参数化类型(泛型):" + parameter.getParameterizedType());LOGGER.info("通过parameter.toString()获取参数的字符串描述:" + parameter.toString());LOGGER.info("通过parameter.isSynthetic()判断参数是否是合成的:" + parameter.isSynthetic());LOGGER.info("通过parameter.isImplicit()判断参数是否是隐式的:" + parameter.isImplicit());LOGGER.info("通过parameter.isNamePresent()判断参数是否以类文件名命名:" + parameter.isNamePresent());LOGGER.info("通过parameter.isVarArgs()判断参数是否是可变的:" + parameter.isVarArgs() + "\n");//===================================== Parameter注解信息 =====================================LOGGER.info("===================================== Parameter注解信息 =====================================");//通过parameter.getAnnotatedType()获取注解的类型(组合类型)AnnotatedType annotatedType = parameter.getAnnotatedType();LOGGER.info("通过parameter.getAnnotatedType()获取注解的类型(组合类型)--参数类型:" + annotatedType.getType() + "\n");//通过parameter.getAnnotation()和parameter.getDeclaredAnnotation()获取参数的一个注解LOGGER.info("通过parameter.getAnnotation()获取参数的一个注解:" + parameter.getAnnotation(MyAnnotationB.class));LOGGER.info("通过parameter.getDeclaredAnnotation()获取参数的一个注解:" + parameter.getDeclaredAnnotation(MyAnnotationB.class) + "\n");//通过parameter.getAnnotationsByType(annotation.class)获取一类注解Annotation[] typeAnnotations = parameter.getAnnotationsByType(MyAnnotationB.class);for (Annotation annotation : typeAnnotations) {LOGGER.info("通过parameter.getAnnotationsByType(annotation.class)获取一类注解:" + annotation);}//通过parameter.getDeclaredAnnotationsByType(annotation.class)获取一类注解Annotation[] typeAnnotations1 = parameter.getDeclaredAnnotationsByType(MyAnnotationB.class);for (Annotation annotation : typeAnnotations1) {LOGGER.info("通过parameter.getDeclaredAnnotationsByType(annotation.class)获取一类注解:" + annotation);}System.out.println("");//通过parameter.getAnnotations()获取全部注解Annotation[] annotations = parameter.getAnnotations();for (Annotation annotation : annotations) {LOGGER.info("通过parameter.getAnnotations()获取全部注解:" + annotation);}//通过parameter.getDeclaredAnnotations()获取全部注解Annotation[] annotations1 = parameter.getDeclaredAnnotations();for (Annotation annotation : annotations1) {LOGGER.info("通过parameter.getDeclaredAnnotations()获取全部注解:" + annotation);}}
}

3.运行结果

2018-03-04 14:46:45 INFO  ReflectParameterDemo:26 - ===================================== 通过Class对象获取Parameter对象 =====================================
2018-03-04 14:46:45 INFO  ReflectParameterDemo:29 - 首先获取Class对象:class pers.hanchao.reflect.common.User
2018-03-04 14:46:45 INFO  ReflectParameterDemo:32 - 然后获取Method或者Constructor对象:public void pers.hanchao.reflect.common.User.initUser(java.lang.String,java.lang.String)
2018-03-04 14:46:45 INFO  ReflectParameterDemo:35 - 然后通过getParameters()获取Parameter对象数组
2018-03-04 14:46:45 INFO  ReflectParameterDemo:38 - 最终获得参数Parameter对象java.lang.String arg02018-03-04 14:46:45 INFO  ReflectParameterDemo:41 - ===================================== Parameter信息获取 =====================================
2018-03-04 14:46:45 INFO  ReflectParameterDemo:42 - 通过parameter.getModifiers()获取参数修饰符:
2018-03-04 14:46:45 INFO  ReflectParameterDemo:43 - 通过parameter.getName()获取参数名:arg0
2018-03-04 14:46:45 INFO  ReflectParameterDemo:44 - 通过parameter.getParameterizedType()获取参数化类型(泛型):class java.lang.String
2018-03-04 14:46:45 INFO  ReflectParameterDemo:45 - 通过parameter.toString()获取参数的字符串描述:java.lang.String arg0
2018-03-04 14:46:45 INFO  ReflectParameterDemo:46 - 通过parameter.isSynthetic()判断参数是否是合成的:false
2018-03-04 14:46:45 INFO  ReflectParameterDemo:47 - 通过parameter.isImplicit()判断参数是否是隐式的:false
2018-03-04 14:46:45 INFO  ReflectParameterDemo:48 - 通过parameter.isNamePresent()判断参数是否以类文件名命名:false
2018-03-04 14:46:45 INFO  ReflectParameterDemo:49 - 通过parameter.isVarArgs()判断参数是否是可变的:false2018-03-04 14:46:45 INFO  ReflectParameterDemo:52 - ===================================== Parameter注解信息 =====================================
2018-03-04 14:46:45 INFO  ReflectParameterDemo:55 - 通过parameter.getAnnotatedType()获取注解的类型(组合类型)--参数类型:class java.lang.String2018-03-04 14:46:45 INFO  ReflectParameterDemo:58 - 通过parameter.getAnnotation()获取参数的一个注解:@pers.hanchao.reflect.common.MyAnnotationB()
2018-03-04 14:46:45 INFO  ReflectParameterDemo:59 - 通过parameter.getDeclaredAnnotation()获取参数的一个注解:@pers.hanchao.reflect.common.MyAnnotationB()2018-03-04 14:46:45 INFO  ReflectParameterDemo:64 - 通过parameter.getAnnotationsByType(annotation.class)获取一类注解:@pers.hanchao.reflect.common.MyAnnotationB()
2018-03-04 14:46:45 INFO  ReflectParameterDemo:69 - 通过parameter.getDeclaredAnnotationsByType(annotation.class)获取一类注解:@pers.hanchao.reflect.common.MyAnnotationB()2018-03-04 14:46:45 INFO  ReflectParameterDemo:76 - 通过parameter.getAnnotations()获取全部注解:@pers.hanchao.reflect.common.MyAnnotationA()
2018-03-04 14:46:45 INFO  ReflectParameterDemo:76 - 通过parameter.getAnnotations()获取全部注解:@pers.hanchao.reflect.common.MyAnnotationB()
2018-03-04 14:46:45 INFO  ReflectParameterDemo:81 - 通过parameter.getDeclaredAnnotations()获取全部注解:@pers.hanchao.reflect.common.MyAnnotationA()
2018-03-04 14:46:45 INFO  ReflectParameterDemo:81 - 通过parameter.getDeclaredAnnotations()获取全部注解:@pers.hanchao.reflect.common.MyAnnotationB()

4.总结

根据代码实例和运行结果,总结如下:

  • 通过Class对象获取Parameter对象

    1. 首先获取Class对象
    2. 然后获取Method(或者Constructor)对象
    3. 然后通过getParameters()获取Parameter对象数组
    4. 最终获得参数Parameter对象
  • Parameter信息获取

    1. 通过parameter.getModifiers()获取参数修饰符
    2. 通过parameter.getName()获取参数名
    3. 通过parameter.getParameterizedType()获取参数化类型(泛型)
    4. 通过parameter.toString()获取参数的字符串描述
    5. 通过parameter.isSynthetic()判断参数是否是合成的
    6. 通过parameter.isImplicit()判断参数是否是隐式的
    7. 通过parameter.isNamePresent()判断参数是否以类文件名命名
    8. 通过parameter.isVarArgs()判断参数是否是可变的
    9. 通过parameter.getAnnotatedType()获取注解的类型(组合类型)
    10. 通过parameter.getAnnotation()和parameter.getDeclaredAnnotation()获取参数的一个注解
    11. 通过parameter.getAnnotationsByType(annotation.class)和parameter.getDeclaredAnnotationsByType(annotation.class)获取一类注解
    12. 通过parameter.getAnnotations()和parameter.getDeclaredAnnotations()获取全部注解
  • 有关获取Class对象的三种方式参见:Java反射02 : Class对象获取的三种方式和通过反射实例化对象的两种方式

  • 有关getXxxxgetDeclaredXxxx参见:Java反射 : Declared的作用 ( 例如 : getMethods和getDeclaredMethods )
  • 有关参数化类型(泛型)参见系列:Java泛型学习系列-绪论

Java反射09 : 参数Parameter学习示例相关推荐

  1. Java反射08 : 成员方法Method学习示例

    超级通道: Java泛型学习系列-绪论 java.lang.reflect.Method类提供了用于获取和操作成员方法的静态方法. 1.通过Method可以做什么 通过Method可以做以下事情: 如 ...

  2. Java 反射 不定参数bug

    Java 反射 不定参数bug 遇到的第一个关于反射的bug:java.lang.IllegalArgumentException: wrong number of arguments的问题解析如下: ...

  3. java反射 enum参数_CookBook/3-Java反射.md at master · Byron4j/CookBook · GitHub

    Java核心(三)反射 Java反射给我们提供了在运行时检查甚至修改应用行为的机制. 反射是java高级的核心技术,所有有经验的程序员都应该理解. 通过反射机制,我们可以在运行时检视 类.接口.枚举, ...

  4. 12000+字Java反射,一起全面了解Java反射机制,为学习框架铺路

    文章目录 Java反射机制 理解Class类 获取Class类实例 类的加载过程 类加载器ClassLoader 创建运行时类的对象 获取运行时类的结构 调用运行时类的指定结构 动态代理 Java反射 ...

  5. java反射 获取参数类型_Java反射带参构造创建对象时如何自动转换参数类型

    需求是这样的:有一个类,类的路径知道,例如是com.xx.xx.xx其中有不同类型的成员变量(个数未知),有对应的setter和getter方法,有一个无参构造和一个全参构造.现在需要用反射机制... ...

  6. java命令行参数_Java学习从入门到精通,JDK工具条知识点学习资料

    JDK是java的核心,包括java运行环境(java运行时环境),一堆java工具和类库(rt.jar)基于java.任何java应用服务器是一个内置的版本的JDK.因此掌握JDK是学习java的第 ...

  7. java 反射 不定参数_关于 Java 中的 不定参数

    简单修改 JAVA中可以使用不定参数, 例如 public void test(String ...args){...} 这里test方法可以传入参数的情况是: 1.不使用参数,如test() 2.使 ...

  8. java反射 获取参数名_java

    传List.class即可,泛型参数在编译后会被擦除掉,无论List里面是String还是什么别的东西都不会影响获取到那个method,在invoke的时候传的参数对不上的话才会产生异常 //Test ...

  9. java statement 动态参数_java_web学习(九) PreparedStatement动态参数的引入

    一.PreparedStatement 概述 在数据库的操作过程中,PreparedStatement 对象是一个很不起眼但是记为重要的接口对象,它继承 于Statement,并与之在两方面有所不同: ...

最新文章

  1. 2020年ACM团队新生第一次周赛题解
  2. Unity* 实体组件系统 (ECS)、C# 作业系统和突发编译器入门
  3. ubuntu18 python_ubuntu18.0.4 python 开发环境
  4. bootstrap搜索框样式代码及效果
  5. 用 Docker 构建、运行、发布来一个 Spring Boot 应用
  6. ubuntu 在 rc.local 里添加了命令为什么无法执行
  7. CCF NOI1010 邮寄包裹
  8. java8 接口调用默认方法_Java8接口里的默认方法特性
  9. h5页面长按保存图片
  10. 【数据结构与算法】之深入解析“路径总和”的求解思路与算法示例
  11. C++继承的继承方式
  12. Mach 微内核的命名趣闻
  13. linux多进程优先级,Linux多线程之优先级
  14. vba excel 退出编辑状态_偷梁换柱之EXCEL编辑保护和VBA隐藏代码保护的解锁
  15. 通信、计算机、电子相关专业技术工作
  16. Java笔记-Java通过JNI调用Linux上so文件
  17. 1032. Sharing (25)
  18. RK3288-安卓5.1-AP6212-WIFI模组调试
  19. roundcube mysql_Webmail Roundcube安装配置
  20. Quartz 数据库表

热门文章

  1. 遇到数据库隔离性问题(读已提交和可重复读、可重复读导致调息前后两次查询数据一样)
  2. scrapy爬取天涯帖子内容
  3. golang 日志库seelog 笔记
  4. 使用httpClient4.4登录豆瓣,并发表说说
  5. FutureTask源码解析
  6. Android程序员面试必备的知识点,androidauto地图
  7. 照片怎样变漫画图片?建议收藏这些方法
  8. VC6.0调试功能使用介绍
  9. 乌班图利用指令修改桌面分辨率
  10. 智能手机Web开发笔记