获取构造函数时的Java NoSuchMethodException [英] Java NoSuchMethodException when getting constructor

查看:138
本文介绍了获取构造函数时的Java NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use reflections to load an instance of a class. I am getting a no such method exception when I try and do it. I have checked and checked and re-checked. That constructor clearly does exist. Does anyone have any ideas? I have successfully used this before on another project with essentially identical code so I'm not sure where I screwed up. Source can be found here:

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)


推荐答案

In StoreCommand.java我看到这个包私有构造函数:

In StoreCommand.java I see this package private constructor:

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

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

from the API docs of getConstructor (emphasis mine):


返回一个Constructor对象,该对象反映此Class对象所代表的类的指定 public
构造函数。

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

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

Either make the constructor public or use getDeclaredConstructor() and set then do setAccesible(true)

这篇关于获取构造函数时的Java NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆