将错误的对象作为参数传递会使 Java 完全忽略方法调用,而不会出现错误语句。是什么原因导致的?

Passing a faulty object as a parameter makes Java completely ignore method calls without an error statement. What can cause this?

提问人:Martyn Corsair 提问时间:5/15/2023 更新时间:5/15/2023 访问量:38

问:

我正在开发一个 minecraft 插件,我在其中使用 CommandAPI 添加命令。

我已经注册了一个用于打开菜单的命令,为了打开这个菜单,我正在调用我的 InterfaceManager 类,该类有一个 open() 方法,该方法需要一些参数来打开哪个接口。

但是,在游戏中执行命令时,在我看来,open() 方法似乎被完全忽略了。我的意思是,InterfaceManager.open() 行也可能被注释掉了,因为这就是代码的处理方式。

这让我非常困惑,因为这根本不是我习惯的 Java 行为。这就是为什么如果有人能向我解释我做错了什么,我会非常高兴。

以下是我如何定义执行命令的代码:

    executesPlayer((sender, args) -> {
        Main.logger().info("[MenuCommand] player executes menu command");
        Main.logger().info("[MenuCommand] test123");
        Main.test();
        InterfaceManager.open(DefaultInterfaceLabels.MAIN_MENU, sender);
        Main.logger().info("[MenuCommand] InterfaceManager open() called");
    });
    

下面是 InterfaceManager.open() 方法:

public static void open(InterfaceLabel<?> label, Player player, String... args) {
    Main.logger().info("[InterfaceManager] test123");
    Main.logger().info("[InterfaceManager] opening interface: " + label + " " + player);
    build(label, player, args, true);
}

这些是执行 /menu 后控制台中的结果:

[10:50:09] [Server thread/INFO]: Pizzanakin issued server command: /menu
[10:50:09] [Server thread/INFO]: [MenuCommand] player executes menu command
[10:50:09] [Server thread/INFO]: [MenuCommand] test123
[10:50:09] [Server thread/INFO]: [Main] testing method call
[10:50:10] [atlantispool housekeeper/DEBUG]: [com.zaxxer.hikari.pool.HikariPool] atlantispool - Before cleanup stats (total=6, active=0, idle=6, waiting=0)

Main 类中的 test() 方法只是为了确认代码能够执行其他类中定义的静态方法。

我希望,无论代码中后面是否存在任何错误,它至少会打印“[InterfaceManager] test123”,然后显示给定参数的以下行。但是整个方法都被跳过了,就好像它根本没有被调用一样。我以前从未见过这种行为,这就是为什么我非常困惑。

实际上,经过更多的测试,我进行了以下更改:

public static void test() {
    Main.logger().info("[InterfaceManager] testing method call");
}

public static void open(InterfaceLabel<?> label, Player player, String... args) {
    Main.logger().info("[InterfaceManager] test123");
    Main.logger().info("[InterfaceManager] opening interface: " + label + " " + player);
    //build(label, player, args, true);
}

基本上,将 test() 方法从 Main 移动到 InterfaceManager,只是为了检查是否可以从该类执行泛型静态方法。然后我还注释掉了 build() 方法,以防万一那里有任何错误。

生成的日志如下:

[11:10:04] [Server thread/INFO]: Pizzanakin issued server command: /menu
[11:10:04] [Server thread/INFO]: [MenuCommand] player executes menu command
[11:10:04] [Server thread/INFO]: [MenuCommand] test123
[11:10:04] [Server thread/INFO]: [InterfaceManager] testing method call
[11:10:10] [atlantispool housekeeper/DEBUG]: [com.zaxxer.hikari.pool.HikariPool] atlantispool - Before cleanup stats (total=6, active=0, idle=6, waiting=0)

我能从中得出的唯一结论是,我想传递给 open() 方法的参数有问题?因为这基本上是 test() 和 open() 之间唯一的区别,除了它们都只是打印语句,仅此而已。但是,很遗憾它没有为我提供错误,我实际上可以找到问题的详细信息。

我通过添加另一种方法检查了这一点:

public static void opentest(Player player, String... args) {
    Main.logger().info("[InterfaceManager] test123");
    Main.logger().info("[InterfaceManager] opening interface: " + player);
    //build(label, player, args, true);
}

并这样称呼它:

executesPlayer((sender, args) -> {
        Main.logger().info("[MenuCommand] player executes menu command");
        Main.logger().info("[MenuCommand] test123");
        InterfaceManager.test();
        InterfaceManager.opentest(sender, "test");
        InterfaceManager.open(DefaultInterfaceLabels.MAIN_MENU, sender);
        Main.logger().info("[MenuCommand] InterfaceManager open() called");
    });

现在我得到了这些结果:

[11:21:36] [Server thread/INFO]: Pizzanakin issued server command: /menu
[11:21:36] [Server thread/INFO]: [MenuCommand] player executes menu command
[11:21:36] [Server thread/INFO]: [MenuCommand] test123
[11:21:36] [Server thread/INFO]: [InterfaceManager] testing method call
[11:21:36] [Server thread/INFO]: [InterfaceManager] test123
[11:21:36] [Server thread/INFO]: [InterfaceManager] opening interface: CraftPlayer{name=Pizzanakin}
[11:21:41] [atlantispool housekeeper/DEBUG]: [com.zaxxer.hikari.pool.HikariPool] atlantispool - Before cleanup stats (total=6, active=0, idle=6, waiting=0)

这基本上证实了我的假设,即它与 InterfaceLabel 的设置方式有关。如果有人能深入了解这门课可能有什么问题,那将不胜感激。虽然我理解这是否困难,因为我没有共享类文件。考虑到这是一个相当大的项目,我怀疑有人真的会花精力去浏览我所有的课程只是为了找到问题,我只是在寻找一种预感来集中我的注意力。

提前致谢!

Java 方法 参数 runtime-error

评论


答: 暂无答案