gpt4 book ai didi

获取构造函数时出现Java NoSuchMethodException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:55:50 29 4
gpt4 key购买 nike

我正在尝试使用反射来加载一个类的实例。当我尝试这样做时,我没有得到这样的方法异常。我已经检查并检查并重新检查。该构造函数显然确实存在。有人有什么想法吗?我之前在另一个代码基本相同的项目中成功使用过它,所以我不确定我在哪里搞砸了。来源可以在这里找到:

private void loadCommands() {
try {
for (Class<?> clazz : ReflectionsReplacement.getSubtypesOf(BaseCommand.class, "us.zsugano.itemsave.commands", plugin.getClass().getClassLoader(), BaseCommand.class)) {

BaseCommand baseCommand = null;
try {
baseCommand = (BaseCommand) clazz.getConstructor(ItemSave.class).newInstance(plugin);

if(Listener.class.isAssignableFrom(clazz)) {
plugin.getServer().getPluginManager().registerEvents((Listener) baseCommand, plugin);
}

} catch (Exception e) {
plugin.PluginPM.sendMessage(Level.SEVERE, "Issues encountered when trying to load commands.");
e.printStackTrace();
}
commands.add(baseCommand);
}
} catch (Exception e) {
plugin.PluginPM.sendMessage(Level.SEVERE, "Exception caught while loading commands.");
e.printStackTrace();
}

for (BaseCommand command : commands) {
plugin.getCommand(command.getName().toLowerCase()).setExecutor(this);
}

}

public abstract class BaseCommand {

public ItemSave plugin;

public BaseCommand(ItemSave plugin) {
this.plugin = plugin;
}

完整来源:https://github.com/zachoooo/ItemSave

这是堆栈跟踪:

19:43:10 [SEVERE] [ItemSave] Issues encountered when trying to load commands.
19:43:10 [SEVERE] java.lang.NoSuchMethodException: us.zsugano.itemsave.commands.
StoreCommand.<init>(us.zsugano.itemsave.ItemSave)
19:43:10 [SEVERE] at java.lang.Class.getConstructor0(Unknown Source)
19:43:10 [SEVERE] at java.lang.Class.getConstructor(Unknown Source)
19:43:10 [SEVERE] at us.zsugano.itemsave.commands.CommandManager.loadComma
nds(CommandManager.java:32)
19:43:10 [SEVERE] at us.zsugano.itemsave.commands.CommandManager.<init>(Co
mmandManager.java:23)
19:43:10 [SEVERE] at us.zsugano.itemsave.ItemSave.onEnable(ItemSave.java:1
9)
19:43:10 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
in.java:217)
19:43:10 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
JavaPluginLoader.java:457)
19:43:10 [SEVERE] at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
mplePluginManager.java:381)
19:43:10 [SEVERE] at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin
(CraftServer.java:282)
19:43:10 [SEVERE] at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlug
ins(CraftServer.java:264)
19:43:10 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.l(Minecr
aftServer.java:313)
19:43:10 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.f(Minecr
aftServer.java:290)
19:43:10 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.a(Minecr
aftServer.java:250)
19:43:10 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServer.init(Ded
icatedServer.java:151)
19:43:10 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.run(Mine
craftServer.java:391)
19:43:10 [SEVERE] at net.minecraft.server.v1_6_R2.ThreadServerApplication.
run(SourceFile:582)

最佳答案

在 StoreCommand.java 中我看到这个包的私有(private)构造函数:

StoreCommand(ItemSave plugin) {
super(plugin);
}

来自 getConstructor 的 API 文档(强调我的):

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

公开构造函数或使用 getDeclaredConstructor() 并设置然后执行 setAccessible(true)

关于获取构造函数时出现Java NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18069081/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com