mardi 27 octobre 2015

Java - How to instantiate inner class with reflection?

I have been having problems with the instantiating the inner class using reflection. Heres an example.

public final class Cow {

    public Cow (Bufallo flying, Horse swimming, int cowID, int numCows) {
        //This is where the part I really dont know what the cow is doing
    }

    public Bull eatGrass(String grassName, AngusCattle farmName, JerseyCattle farmName){
        Ox newBreed = new Ox(australiaFarm, somewhereOutThere);
        //Something that has to do with cow eating grass
        return Bull;
    }

    private static final class Ox extends BigHorns {

        public Ox (AngusCattle farmName, ChianinaOx farmName) {
            //Something about mating
        }

    }

}

all I want is to get the constructor or just instantiate the inner class. My code so far...

CowManager cowManager = (CowManager) this.getSystemService(Context.COW_SERVICE);
final Class MainCowClass  = Class.forName(cowManager.getClass().getName());
final Class[] howManyCows = MainCowClass.getDeclaredClasses();
Class getCow = null;
for (int i=0; i < howManyCows.length; i++) {
    if (! howManyCows[i].getName().equals("Cow$Ox")) {
        continue;
    }
    getCow = Class.forName(howManyCows[i].getName());
}
Constructor createCow = getCow.getDeclaredConstructor();

as of the moment I cant seem to find the constructor of ox inside the cow





Aucun commentaire:

Enregistrer un commentaire