java - 如何在 Java 编译后保留接口(interface)的参数名称?

标签 java interface javac

我想在 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/

相关文章:

java - "<T extends ...>"返回类型的意外编译器警告

java - 将 jdbc 连接传递给 Spark 读取

java - 通用静态方法对类型的限制太多

java - 如何从不在包中的 jar 导入 Java 类?

.net - IEnumerable & IEnumerator

jar - 在源代码中和编译期间包含 jar 文件

Java Netbeans : How to open the JPanel from another class?

javascript - typescript -对象作为私有(private)或 protected

java - 如何只允许在同一个包中实现接口(interface)

java - '(' or '['UNIX下预期编译错误