Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java NoSuchMethodException when getting constructor

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;
}

Full Source: https://github.com/zachoooo/ItemSave

And here is the Stack Trace:

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)
like image 432
Zach Sugano Avatar asked Aug 05 '13 22:08

Zach Sugano


1 Answers

In StoreCommand.java I see this package private constructor:

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

from the API docs of getConstructor (emphasis mine):

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

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

like image 89
Gus Avatar answered Oct 20 '22 01:10

Gus