Method Parameter Reflection in Java – Java中的方法参数反思

最后修改: 2018年 6月 5日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Method Parameter Reflection support was added in Java 8. Simply put, it provides support for getting the names of parameters at runtime.

方法参数反射支持是在Java 8中添加的。简单地说,它提供了在运行时获取参数名称的支持。

In this quick tutorial, we’ll take a look at how to access parameter names for constructors and methods at runtime – using reflection.

在这个快速教程中,我们将看看如何在运行时访问构造函数和方法的参数名–使用反射。

2. Compiler Argument 

2.编译器论证

In order to be able to get access to method name information, we must opt-in explicitly.

为了能够获得方法名称信息,我们必须明确选择加入。

To do this, we specify the parameters option during compilation.

为了做到这一点,我们在编译时指定parameters选项

For a Maven project, we can declare this option in the pom.xml:

对于Maven项目,我们可以在pom.xml中声明该选项。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.1</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <compilerArgument>-parameters</compilerArgument>
  </configuration>
</plugin>

3. Example Class

3.实例类

We’ll use a contrived Person class with a single property called fullName to demonstrate:

我们将使用一个伪造的Person类来演示,该类有一个名为fullName的单一属性。

public class Person {

    private String fullName;

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

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    // other methods
}

4. Usage

4.使用方法

The Parameter class is new in Java 8 and has a variety of interesting methods. If the -parameters compiler option was provided, the isNamePresent() method will return true.

Parameter类在Java 8中是新的,有各种有趣的方法。如果提供了-parameters编译器选项,isNamePresent()方法将返回true。

To access the name of a parameter, we can simply call getName():

要访问一个参数的名称,我们可以简单地调用getName()

@Test
public void whenGetConstructorParams_thenOk() 
  throws NoSuchMethodException, SecurityException {
 
    List<Parameter> parameters 
        = Arrays.asList(Person.class.getConstructor(String.class).getParameters());
    Optional<Parameter> parameter 
        = parameters.stream().filter(Parameter::isNamePresent).findFirst();
    assertThat(parameter.get().getName()).isEqualTo("fullName");
}

@Test
public void whenGetMethodParams_thenOk() 
  throws NoSuchMethodException, SecurityException {
 
    List<Parameter> parameters = Arrays.asList(
      Person.class.getMethod("setFullName", String.class).getParameters());
    Optional<Parameter> parameter= parameters.stream()
      .filter(Parameter::isNamePresent)
      .findFirst();
 
    assertThat(parameter.get().getName()).isEqualTo("fullName");
}

5. Conclusion

5.结论

In this quick article, we looked at the new reflection support for parameter names that became available in Java 8.

在这篇快速文章中,我们研究了Java 8中对参数名的新反射支持。

The most obvious use case for this information is to help implement auto-complete support within editors.

这些信息最明显的用途是帮助实现编辑器内的自动完成支持。

As always, the source code can be found over on Github.

一如既往,源代码可以在Github上找到over