我想在 Java 编译后保留接口(interface)方法的参数名称。
下面是一个例子。
编译前:
interface IFoo {
void hello(String what);
}
使用javac -g:vars IFoo.java
编译后
interface IFoo {
void hello(String str);
}
参数 what
被重命名为 str
。
如何保留参数名称?
最佳答案
参数或局部变量没有名称,只有一个数字。
我相信用于添加局部变量名称的命令行参数是 -parameters
通过反射启用访问 https://www.beyondjava.net/reading-java-8-method-parameter-named-reflection
确定/猜测变量名是反编译器的工作。我使用 Fernflower,它做得很合理。
输入
import java.util.stream.Stream;
interface IFoo {
public abstract void hello(String what);
public static void print(String... args) {
Stream<String> stream = Stream.of(args);
stream.forEach(System.out::println);
}
}
使用 Fernflower 输出
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import java.io.PrintStream;
import java.util.function.Consumer;
import java.util.stream.Stream;
interface IFoo {
void hello(String what);
static void print(String... args) {
Stream<String> stream = Stream.of(args);
PrintStream var10001 = System.out;
System.out.getClass();
stream.forEach(var10001::println);
}
}
注意:System.out.getClass();
由 javac
生成编译器测试 null
值(value)。
关于java - 如何在 Java 编译后保留接口(interface)的参数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52400922/