mercredi 27 décembre 2017

How do I get all classes within a package? [duplicate]

This question already has an answer here:

I have a function:foo.register("string", new Class("string"); An example of this would be: foo.register("emp1", new Stu("jack"); //Stu being a class in a random package, for this instance "com.sof.people"

The problem is I have to repeat this function for every class within in package . For example in "com.sof.people" their is Stu.class, Stu2.class, Stu3.class, exc. Is it possible to simply search the package and call the function based on the classes within the package.

In a perfect world it would work something like this:

public static void registerAll(String string1, String string2) {
    for(Class c: Class.fromPackage("com.sof.people") {
        foo.register(string1, new c(string2);
      }
}





Aucun commentaire:

Enregistrer un commentaire