资讯专栏INFORMATION COLUMN

JAVA 8 反射获取参数名

shiweifu / 3040人阅读

摘要:前言在之前编译是不会把构造器和方法的参数名编译进中,如果需要获取参数名,可以在方法上加上注解,反射获取注解的值从而获取参数名,比如的和。带在中添加命令行在后面加运行结果构造器方法一方法二方法三方法四这样就把参数名给打印出来了,为。

前言

在JDK8之前javac编译是不会把构造器和方法的参数名编译进class中,如果需要获取参数名,可以在方法上加上注解,反射获取注解的值从而获取参数名,比如Jackson的@JsonCreator@JsonProperty 。而JDK8新增了这一个功能,可以直接调用java.lang.reflect.Parameter.getName()获取到,前提是javac需要添加-parameters这个参数。通常来说不建议这样做,因为这会增大.class和在JVM中会占用更多的内存。

正文 代码

直接上代码。

</>复制代码

  1. 用来打印类信息

</>复制代码

  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.Method;
  3. import java.lang.reflect.Parameter;
  4. import static java.lang.System.out;
  5. public class MethodParameterSpy {
  6. private static final String fmt = "%24s: %s%n";
  7. public static void printClassConstructors(Class c) {
  8. Constructor[] allConstructors = c.getConstructors();
  9. out.format(fmt, "Number of constructors", allConstructors.length);
  10. for (Constructor currentConstructor : allConstructors) {
  11. printConstructor(currentConstructor);
  12. }
  13. Constructor[] allDeclConst = c.getDeclaredConstructors();
  14. out.format(fmt, "Number of declared constructors",
  15. allDeclConst.length);
  16. for (Constructor currentDeclConst : allDeclConst) {
  17. printConstructor(currentDeclConst);
  18. }
  19. }
  20. public static void printClassMethods(Class c) {
  21. Method[] allMethods = c.getDeclaredMethods();
  22. out.format(fmt, "Number of methods", allMethods.length);
  23. for (Method m : allMethods) {
  24. printMethod(m);
  25. }
  26. }
  27. public static void printConstructor(Constructor c) {
  28. out.format("%s%n", c.toGenericString());
  29. Parameter[] params = c.getParameters();
  30. out.format(fmt, "Number of parameters", params.length);
  31. for (int i = 0; i < params.length; i++) {
  32. printParameter(params[i]);
  33. }
  34. }
  35. public static void printMethod(Method m) {
  36. out.format("%s%n", m.toGenericString());
  37. out.format(fmt, "Return type", m.getReturnType());
  38. out.format(fmt, "Generic return type", m.getGenericReturnType());
  39. Parameter[] params = m.getParameters();
  40. for (int i = 0; i < params.length; i++) {
  41. printParameter(params[i]);
  42. }
  43. }
  44. public static void printParameter(Parameter p) {
  45. out.format(fmt, "Parameter class", p.getType());
  46. out.format(fmt, "Parameter name", p.getName());
  47. out.format(fmt, "Modifiers", p.getModifiers());
  48. out.format(fmt, "Is implicit?", p.isImplicit());
  49. out.format(fmt, "Is name present?", p.isNamePresent());
  50. out.format(fmt, "Is synthetic?", p.isSynthetic());
  51. }
  52. public static void main(String... args) {
  53. printClassConstructors(ExampleMethods.class);
  54. printClassMethods(ExampleMethods.class);
  55. }
  56. }

</>复制代码

  1. 包含各种类型方法的类

</>复制代码

  1. import java.util.*;
  2. public class ExampleMethods {
  3. public boolean simpleMethod(String stringParam, int intParam) {
  4. System.out.println("String: " + stringParam + ", integer: " + intParam);
  5. return true;
  6. }
  7. public int varArgsMethod(String... manyStrings) {
  8. return manyStrings.length;
  9. }
  10. public boolean methodWithList(List listParam) {
  11. return listParam.isEmpty();
  12. }
  13. public void genericMethod(T[] a, Collection c) {
  14. System.out.println("Length of array: " + a.length);
  15. System.out.println("Size of collection: " + c.size());
  16. }
  17. }
不带-parameters

不带-parameters运行结果:

</>复制代码

  1. Number of constructors: 1
  2. public ExampleMethods()
  3. Number of parameters: 0
  4. Number of declared constructors: 1
  5. # 构造器
  6. public ExampleMethods()
  7. Number of parameters: 0
  8. Number of methods: 4
  9. # 方法一
  10. public boolean ExampleMethods.simpleMethod(java.lang.String,int)
  11. Return type: boolean
  12. Generic return type: boolean
  13. Parameter class: class java.lang.String
  14. Parameter name: arg0
  15. Modifiers: 0
  16. Is implicit?: false
  17. Is name present?: false
  18. Is synthetic?: false
  19. Parameter class: int
  20. Parameter name: arg1
  21. Modifiers: 0
  22. Is implicit?: false
  23. Is name present?: false
  24. Is synthetic?: false
  25. # 方法二
  26. public boolean ExampleMethods.methodWithList(java.util.List)
  27. Return type: boolean
  28. Generic return type: boolean
  29. Parameter class: interface java.util.List
  30. Parameter name: arg0
  31. Modifiers: 0
  32. Is implicit?: false
  33. Is name present?: false
  34. Is synthetic?: false
  35. # 方法三
  36. public void ExampleMethods.genericMethod(T[],java.util.Collection)
  37. Return type: void
  38. Generic return type: void
  39. Parameter class: class [Ljava.lang.Object;
  40. Parameter name: arg0
  41. Modifiers: 0
  42. Is implicit?: false
  43. Is name present?: false
  44. Is synthetic?: false
  45. Parameter class: interface java.util.Collection
  46. Parameter name: arg1
  47. Modifiers: 0
  48. Is implicit?: false
  49. Is name present?: false
  50. Is synthetic?: false
  51. # 方法四
  52. public int ExampleMethods.varArgsMethod(java.lang.String...)
  53. Return type: int
  54. Generic return type: int
  55. Parameter class: class [Ljava.lang.String;
  56. Parameter name: arg0
  57. Modifiers: 0
  58. Is implicit?: false
  59. Is name present?: false
  60. Is synthetic?: false

可以看出Parameter name全都是arg0~argN,因为参数名在编译期已经丢失了。Is name present为false。

带-parameters

</>复制代码

  1. mavenpom.xml中添加

</>复制代码

  1. org.apache.maven.plugins
  2. maven-compiler-plugin
  3. 8
  4. 8
  5. -parameters

命令行在javac 后面加 -parameters

</>复制代码

  1. 运行结果

</>复制代码

  1. Number of constructors: 1
  2. public ExampleMethods()
  3. Number of parameters: 0
  4. Number of declared constructors: 1
  5. # 构造器
  6. public ExampleMethods()
  7. Number of parameters: 0
  8. Number of methods: 4
  9. # 方法一
  10. public boolean ExampleMethods.methodWithList(java.util.List)
  11. Return type: boolean
  12. Generic return type: boolean
  13. Parameter class: interface java.util.List
  14. Parameter name: listParam
  15. Modifiers: 0
  16. Is implicit?: false
  17. Is name present?: true
  18. Is synthetic?: false
  19. # 方法二
  20. public int ExampleMethods.varArgsMethod(java.lang.String...)
  21. Return type: int
  22. Generic return type: int
  23. Parameter class: class [Ljava.lang.String;
  24. Parameter name: manyStrings
  25. Modifiers: 0
  26. Is implicit?: false
  27. Is name present?: true
  28. Is synthetic?: false
  29. # 方法三
  30. public void ExampleMethods.genericMethod(T[],java.util.Collection)
  31. Return type: void
  32. Generic return type: void
  33. Parameter class: class [Ljava.lang.Object;
  34. Parameter name: a
  35. Modifiers: 0
  36. Is implicit?: false
  37. Is name present?: true
  38. Is synthetic?: false
  39. Parameter class: interface java.util.Collection
  40. Parameter name: c
  41. Modifiers: 0
  42. Is implicit?: false
  43. Is name present?: true
  44. Is synthetic?: false
  45. # 方法四
  46. public boolean ExampleMethods.simpleMethod(java.lang.String,int)
  47. Return type: boolean
  48. Generic return type: boolean
  49. Parameter class: class java.lang.String
  50. Parameter name: stringParam
  51. Modifiers: 0
  52. Is implicit?: false
  53. Is name present?: true
  54. Is synthetic?: false
  55. Parameter class: int
  56. Parameter name: intParam
  57. Modifiers: 0
  58. Is implicit?: false
  59. Is name present?: true
  60. Is synthetic?: false

这样就把参数名给打印出来了,Is name present为true。

留个问题

出于好奇,以十六进制打开ExampleMethods的class文件,maven项目在idea中build出来的class不管有没有带-parameters都会把参数名编译进去,但是多了MethodParameters这几个字眼。如下图:

然后尝试直接用javac -parameters编译,打开后

很明显是没有把参数名编译进去的。

好像找不到idea执行build的时候执行了什么,所有我猜测控制java.lang.reflect.Parameter.getName()返回是否真实的参数名就是在于MethodParameters这词,在jvm加载class时识别是否有MethodParameters,而决定是否加载参数名。

这仅是猜测,望大家相告是其真正的原理。
文中也可能存在错误,也望大家指出。

代码来源

上述代码全部来自#参考资料中的Obtaining Names of Method Parameters

参考资料

Obtaining Names of Method Parameters

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76827.html

相关文章

  • Java反射详细介绍

    摘要:通过反射获取带参无返回值成员方法并使用设置安全检查,访问私有构造函数必须创建实例这种不行,注意和方法需要传递参数测试复制这个功能获取私有方法,同样注意和的区别赋予访问权限调用方法。 反射 目录介绍 1.反射概述 1.1 反射概述 1.2 获取class文件对象的三种方式 1.3 反射常用的方法介绍 1.4 反射的定义 1.5 反射的组成 1.6 反射的作用有哪些 2.反射的...

    ingood 评论0 收藏0
  • Java反射机制及API使用

    摘要:有一个参数的构造方法姓名有多个参数的构造方法姓名年龄这的执行效率有问题,以后解决。私有构造方法私有的构造方法年龄反射获取对象的三种方式通过对象名方法获取通过类名方式获得通过方法获得在运行期间,一个类,只有一个对象产生。 原文地址 反射简单来说,就是动态加载对象,并对对象进行剖析。在Java中的反射机制是指在运行状态中,对于任意一个类,都能够知道并获取这个类的所有属性和方法。 Java反...

    vibiu 评论0 收藏0
  • 进击的Android工程师之Java基础: 反射

    摘要:不包含父类或父接口的方法返回,根据方法名和类型获取。类或接口的及父类父接口公共成员方法。是的返回方法名,不包括修饰符,参数和返回值。打印打印抛出因为的访问权限为抛出,因为是父类的方法。 反射机制呢就是在程序运行时,动态的获取类(class),类的方法(method)属性(field)等。主要的注意点就是程序运行时动态的获取。这里主要是从代码的角度来讲解Java反射。在使用中我们用的较多...

    aaron 评论0 收藏0
  • 1、类加载器 2、反射构造方法 3、反射成员变量 4、反射成员方法 5、反射配置文件运行类中的方法

    摘要:通过反射获取无参构造方法并使用得到无参构造方法获取所有的修饰的构造方法。如果方法没有返回值,返回的是反射获取空参数成员方法并运行代码演示反射获取成员方法并运行获取对象中的成员方法获取的是文件中的所有公共成员方法包括继承的类是描述 01类加载器 * A.类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化。  ...

    Profeel 评论0 收藏0
  • Reflection:Java反射机制基础

    摘要:反射机制是什么反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法对于任意一个对象,都能够调用它的任意一个方法和属性这种动态获取的信息以及动态调用对象的方法的功能称为语言的反射机制反射机制能做什么反射机制主要提供了以下功 反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种...

    hizengzeng 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<