Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As mentioned in the comments on <a href="https://stackoverflow.com/questions/2729580/how-to-get-the-parameter-names-of-an-objects-constructors-reflection/2729595#2729595">Roman's answer</a>, the parameter names <em>can</em> be retrieved if the compiler included debugging symbols, though not through the standard Java Reflection API. Below is an example illustrating how you could obtain parameter names via the debugging symbols using the <a href="http://asm.ow2.org/" rel="nofollow noreferrer">ASM bytecode library</a>:</p> <pre><code>/** * Returns a list containing one parameter name for each argument accepted * by the given constructor. If the class was compiled with debugging * symbols, the parameter names will match those provided in the Java source * code. Otherwise, a generic "arg" parameter name is generated ("arg0" for * the first argument, "arg1" for the second...). * * This method relies on the constructor's class loader to locate the * bytecode resource that defined its class. * * @param constructor * @return * @throws IOException */ public static List&lt;String&gt; getParameterNames(Constructor&lt;?&gt; constructor) throws IOException { Class&lt;?&gt; declaringClass = constructor.getDeclaringClass(); ClassLoader declaringClassLoader = declaringClass.getClassLoader(); Type declaringType = Type.getType(declaringClass); String constructorDescriptor = Type.getConstructorDescriptor(constructor); String url = declaringType.getInternalName() + ".class"; InputStream classFileInputStream = declaringClassLoader.getResourceAsStream(url); if (classFileInputStream == null) { throw new IllegalArgumentException("The constructor's class loader cannot find the bytecode that defined the constructor's class (URL: " + url + ")"); } ClassNode classNode; try { classNode = new ClassNode(); ClassReader classReader = new ClassReader(classFileInputStream); classReader.accept(classNode, 0); } finally { classFileInputStream.close(); } @SuppressWarnings("unchecked") List&lt;MethodNode&gt; methods = classNode.methods; for (MethodNode method : methods) { if (method.name.equals("&lt;init&gt;") &amp;&amp; method.desc.equals(constructorDescriptor)) { Type[] argumentTypes = Type.getArgumentTypes(method.desc); List&lt;String&gt; parameterNames = new ArrayList&lt;String&gt;(argumentTypes.length); @SuppressWarnings("unchecked") List&lt;LocalVariableNode&gt; localVariables = method.localVariables; for (int i = 0; i &lt; argumentTypes.length; i++) { // The first local variable actually represents the "this" object parameterNames.add(localVariables.get(i + 1).name); } return parameterNames; } } return null; } </code></pre> <p>This example uses the ASM library's <a href="http://asm.ow2.org/asm32/javadoc/user/org/objectweb/asm/tree/package-frame.html" rel="nofollow noreferrer">tree API</a>. If speed and memory are precious, you can refactor the example to use its <a href="http://asm.ow2.org/asm32/javadoc/user/org/objectweb/asm/package-frame.html" rel="nofollow noreferrer">visitor API</a> instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload