Paramo
Read method parameter names
Java compiler doesn't store method parameter names in the class file and we can't retrieve them using reflection. Actually, the names are stored in the LocalVariableTable
attribute of the method, if a class is compiled with debug symbols on (i.e., javac -g). The LocalVariableTable
attribute is not accessible using reflection, but it can be read using the bytecode parsing library like ASM.
Paramo is a little tool that extracts method or constructor parameter names from bytecode debug information in runtime.
Paramo works only if debug information is available in class files (compile with: javac -g
).
Usage
Using Paramo is very simple: just pass Method
or Constructor
to the static method Paramo#resolveParameters
. It returns MethodParameter[]
array with method parameter information, or an empty array if the method/constructor does not have any parameter.
MethodParameter
is a simple POJO that holds two information about the method parameter:
parameter name
bytecode signature, including the generic information!
MethodresolveParameters
does not cache anything; every time invoked it will examine the bytecode again. For efficient usage, wrap it and cache results. {: .attn}
In contrast to the rest of Jodd, Paramo is written to be 'raw', without many helper methods. This is done by purpose, since getting parameter names is usually part of some higher-level logic, that should wrap it in the way that serves the best to that logic.
Examples
Let's say we have the following class:
We will assume that we got references to Method
s and Constructor
using reflection (and with tool such ReflectUtil#findMethod
). Now, let's read parameter names:
Last updated