Reflection getMethod(...) 以不同的方法名称抛出 NoSuchMethodException

问题描述 投票:0回答:0

使用

myClass.getMethod("func_181057_v")
在控制台中抛出异常 - “NoSuchMethodException: myClass.v()”。
我不认为它应该这样做——为什么抛出的异常中的方法名称与我实际输入的方法名称不同,我如何检索该方法?

我已经尝试了

.getMethod(..)
.getDelcaredMethod(..)
以及打印的
.getDeclaredMethods()
列表和 .func_181057_v() 在列表中。当我更改名称的任何内容(例如 func_181057__v)时,将查找准确键入的方法而不是“v”。

我的代码:

try {
   Class<?> fmlCommonHandlerClass = Class.forName("net.minecraftforge.fml.common.FMLCommonHandler");
   Class<?> minecraftServerClass = Class.forName("net.minecraft.server.MinecraftServer");
   Class<?> playerListClass = Class.forName("net.minecraft.server.management.PlayerList");

   Method getMinecraftServerInstanceMethod = fmlCommonHandlerClass.getDeclaredMethod("getMinecraftServerInstance");
   Method getPlayerListMethod = minecraftServerClass.getDeclaredMethod("getPlayerList");
   Method getPlayersMethod = playerListClass.getDeclaredMethod("func_181057_v");
} catch (Exception exception) {
   throw new RuntimeException(exception);
}

完整代码:https://github.com/KittyNath/ReflectionExample
确切的方法名称是

public java.util.List net.minecraft.server.management.PlayerList.func_181057_v()

如果你想执行它,你需要编译并安装它作为 Mohist 服务器的插件,我使用的是 1.12.2 版本,build 320.
Java 1.8.0_361.

完全例外:

[18:25:56 ERROR]: Error occurred while enabling ReflectionExample v1.0-SNAPSHOT (Is it up to date?)
 java.lang.RuntimeException: java.lang.NoSuchMethodException: net.minecraft.server.management.PlayerList.v()
        at com.example.plugin.reflectionExample.ReflectionExample.onEnable(ReflectionExample.java:20) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:177) ~[JavaPlugin.class:?]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:318) [JavaPluginLoader.class:?]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:399) [SimplePluginManager.class:?]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:477) [CraftServer.class:?]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:418) [CraftServer.class:?]
        at net.minecraft.server.MinecraftServer.func_71247_a(MinecraftServer.java:383) [MinecraftServer.class:?]
        at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:315) [nz.class:?]
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:590) [MinecraftServer.class:?]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_361]
Caused by: java.lang.NoSuchMethodException: net.minecraft.server.management.PlayerList.v()
        at java.lang.Class.getMethod(Unknown Source) ~[?:1.8.0_361]
        at com.mohistmc.bukkit.nms.proxy.ProxyClass.getMethod(ProxyClass.java:43) ~[ProxyClass.class:?]
        at com.example.plugin.reflectionExample.ReflectionExample.onEnable(ReflectionExample.java:18) ~[?:?]
        ... 9 more
java reflection minecraft minecraft-forge
© www.soinside.com 2019 - 2024. All rights reserved.