6

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

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)
4

1 回答 1

12

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

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

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

返回一个 Constructor 对象,该对象反映 此 Class 对象表示的类的指定公共构造函数。

要么公开构造函数,要么使用getDeclaredConstructor()并设置然后做setAccesible(true)

于 2013-08-05T23:10:57.493 回答