/parameternames

Utility methods for fetching Java method or constructor parameter names

Primary LanguageJavaApache License 2.0Apache-2.0

Parameter Names

Maven Central Build Status

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:

  1. Java 8 reflection names created by javac with the -parameters option
  2. Bytecode debug symbols created by javac with the -g option
  3. Default name of argN where N 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.