获取构造函数时的Java NoSuchMethodException

前端之家收集整理的这篇文章主要介绍了获取构造函数时的Java NoSuchMethodException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用反射来加载类的实例.当我尝试这样做时,我得到了一个没有这种方法的例外.我检查并检查并重新检查.那个构造函数显然确实存在.有没有人有任何想法?我之前已成功使用过这个代码基本相同的另一个项目,所以我不确定我搞砸了.来源可以在这里找到:
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中,我看到这个包私有构造函数
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.

要么将构造函数设为public,要么使用getDeclaredConstructor()并设置setAccesible(true)

原文链接:https://www.f2er.com/java/120607.html

猜你在找的Java相关文章