lundi 1 août 2016

How to get a list of classes in package/file in Java?

Please help! What I need is to get a list of classes which are in my file.

Here is my code of _main_class:



public static PrintStream out = System.out;
public static Scanner in = new Scanner(System.in);

public static List<Class> getListOfClasses() {
    List<Class> list = null;
    // TODO: Fill list with classes (in my case: _main_class,summation,multiplication,division)
    return list;
}

public static List<Method> getListOfMethods(Class _class) {
    List<Method> list = null;
    // TODO: Fill list with methods (in case class summation: sum)
    return list;
}
public static void main(String[] args) {

    out.println("Choose index of class to run:");
    List<Class> classList = getListOfClasses();
    for (int i = 0;i<classList.size();i++){
        out.println(Integer.toString(i)+": "+classList.get(i).getName());
    }
    int indOfClass = in.nextInt();
    Class currClass = classList.get(indOfClass);

    out.println("Choose index of method to run in class"+ currClass.getName() +" :");
    List<Method> methodList = getListOfMethods(currClass);
    for (int i = 0;i<classList.size();i++){
        out.println(Integer.toString(i)+": "+methodList.get(i).getName());
    }

    int indOfMethod = in.nextInt();
    Method currMethod = methodList.get(indOfMethod);

    // TODO: RUN THIS METHOD (currMethod)
}







Here is another classes that are in _main_class.java file (they are not inside _main_class class):

class summation{ public static float sum(float a,float b){ return a+b; } } class multiplication{ public static float mult(float a,float b){ return a*b; } } class division{ public static float div(float a,float b){ return a/b; } }

Here is the file: http://ift.tt/2aKfGpS The idea is in using Reflection API, but i'm an extremely incompetent in Java and even less proficient in Reflection API.

Thank you!





Aucun commentaire:

Enregistrer un commentaire