Parameter Names
Parameter Names contains utility methods for fetching the parameter names for a method or constructor.
Java 8 added the ability to access the parameter names of a method or constructor, but this only works if the code was compiled with the Java 8 version of javac
with the -parameters
option enabled. This utility falls back to reading parameters from the debug symbols in the method bytecode for classes compiled without the -parameters
option enabled. Note that this fallback will not work for interfaces, as interface methods do not have bytecode and thus do not have debug symbols.
Usage
Simply, pass java.lang.reflect.Method
or java.lang.reflect.Constructor
to ParameterNames.getParameterNames
. For example:
Method method = ParameterNames.class.getMethod("getParameterNames", Executable.class);
List<String> parameterNames = ParameterNames.getParameterNames(method);
Parameter names are loaded in the following order:
- Java 8 reflection names created by
javac
with the-parameters
option - Bytecode debug symbols created by
javac
with the-g
option - Default name of
argN
whereN
is the index of the parameter starting from zero
Maven Dependency
<dependency>
<groupId>io.airlift</groupId>
<artifactId>parameternames</artifactId>
<version>1.0</version>
</dependency>
License
Parameter Names is released under the Apache License, Version 2.0.