Preserving Parameter/Argument Names in Compiled Java Classes

Preserving parameter/argument names in compiled Java classes

To preserve names in the class file for debugging purposes try project properties, Java compiler, then "Add variable attributes to generated class files" (See Eclipse Help).

Compiling the following source:

public class StackOverflowTest {
public void test(String foo, String bar) {
// blah
}
}

Is decompiled into:

// Compiled from StackOverflowTest.java (version 1.5 : 49.0, super bit)
public class StackOverflowTest {

// Method descriptor #6 ()V
// Stack: 1, Locals: 1
public StackOverflowTest();
0 aload_0 [this]
1 invokespecial java.lang.Object() [8]
4 return
Line numbers:
[pc: 0, line: 1]
Local variable table:
[pc: 0, pc: 5] local: this index: 0 type: StackOverflowTest

// Method descriptor #15 (Ljava/lang/String;Ljava/lang/String;)V
// Stack: 0, Locals: 3
public void test(java.lang.String foo, java.lang.String bar);
0 return
Line numbers:
[pc: 0, line: 4]
Local variable table:
[pc: 0, pc: 1] local: this index: 0 type: StackOverflowTest
[pc: 0, pc: 1] local: foo index: 1 type: java.lang.String
[pc: 0, pc: 1] local: bar index: 2 type: java.lang.String
}

See the parameter names are preserved in the class files.

I would suggest you look into how your source is being compiled, which version it is compiled for etc.

EDIT:

Ah, I see this is different for interfaces - they don't seem to have this information available for the debugger which I guess makes sense. I don't think there'll be a way round this, if you just want to see the parameter names when you're editing source you'll need to go the javadoc route as Nagrom_17 suggests (attach the source).

Store method parameter names for some classes when compiling in Java 8

Yes in a way. This is an option to javac (see -parameters) and javac can be run on whatever set of files you would like. However, there is not any option to selectively apply -parameters to certain classes when running javac on multiple files, so you would have to run multiple javac's most likely. This could be done through a build file most likely with a build language (for instance Ant or Gradle).

Executable.hasRealParameterData() and Parameter.isNamePresent() don't work as expected

As it was pointed out by Daniel Fuchs we need to compile the code with -parameters flag.

https://docs.oracle.com/en/java/javase/18/docs/specs/man/javac.html

Proguard keep interface method paramternames

Proguard can preserve parameter names just fine. Unfortunately, javac does not preserve them by default. In order to emit necessary attributes, make sure to compile your code with full debugging info enabled. Java 8 javac does this with -g and -parameters flags.

See this blogpost for some background.

Persist method argument names in bytecode so that they are available through reflection when building with Maven

To store parameter names in generated bytecode you must pass the -parameters flag to the java compiler. If you've using maven, you can do so via the maven-compiler-plugin:

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>

Java method parameter names from a JAR file in Eclipse

Java method argument names are not represented in the class file format. The relevant parts of the JVM spec are sections 4.3.3 and 4.6. (Strictly, they can be in some cases, but they cannot be included for interfaces, and won't be included unless the relevant compiler options were set when the class was compiled.)

In practice, Eclipse can also present you with method argument names if it can find and parse the source code corresponding to the class files. In the simple case, you can address this by downloading a ZIP file etc containing the relevant source code, and attaching it to the binary JAR.

FOLLOWUP re your comment:

I wasn't meaning to include the source folder in the JAR (though that obviously works). If you look at the Eclipse properties for a dependent JAR file, there's a way to attach the corresponding source JAR or directory. (I cannot run Eclipse right now to give you precise details.) Anyway, this approach doesn't require you to recompile or otherwise change the dependent JAR.



Related Topics



Leave a reply



Submit