samedi 21 mai 2016

Java reflection: getting constructor fails for inner-class

I have a class, named GlowZombie, which has two contructors:

public class GlowZombie extends GlowMonster implements Zombie {

    public GlowZombie(Location loc) {
        this(loc, EntityType.ZOMBIE);
    }

    public GlowZombie(Location loc, EntityType type) {
        super(loc, type, 20);
    }

And that class has a subclass, GlowHusk, with the same constructor parameters:

    public class GlowHusk extends GlowZombie implements Zombie.Husk {

        public GlowHusk(Location loc) {
            this(loc, EntityType.HUSK);
        }

        public GlowHusk(Location loc, EntityType type) {
            super(loc, type);
        }
    }
}

Then, I need to use reflection to get the GlowHusk(Location) constructor, like such:

Constructor<T> constructor = (Constructor<T>) GlowZombie.GlowHusk.class.getConstructor(Location.class);

...Which throws exception:

java.lang.NoSuchMethodException: net.glowstone.entity.monster.GlowZombie$GlowHusk.<init>(org.bukkit.Location)

Note:

Using GlowZombie instead of GlowHusk executes correctly:

Constructor<T> constructor = (Constructor<T>) GlowZombie.class.getConstructor(Location.class);

And does not throw an exception.

The question: why does getConstructor() not work in this scenario? Would it be because it is an inner-class, thus requiring some other way of accessing it?





Aucun commentaire:

Enregistrer un commentaire