dimanche 3 décembre 2017

how to print the parameter types correctly?

the output should be as below:

foo (A, T1, T2) -> void

bar (A, T1, T2, T3) -> int

doo () -> double

public static class A  {
    void foo(int T1, double T2) { }
    int bar(int T1, double T2, char T3) { return 1; }
    static double doo() { return 1; }
}

static void displayMethodInfo(Object obj)
{
    Class<?> a = obj.getClass();
    Method[] methods = a.getDeclaredMethods();
    for (Method y : methods)    //print methods
    {
        System.out.print(y.getName() + "(" );    // + y.getDeclaringClass().getSimpleName());
        Type[] types = y.getGenericParameterTypes();    //get parameter types

        if (!(Modifier.isStatic((y.getModifiers()))))
        {
            //non-static method, output this class namr as the 1st argument
            System.out.print(y.getName());  //display
            if (types.length > 0)
            {
                //put a comma and space
                System.out.print(", ");
            }
        }
        /*
        //print parameter of the method 
        int i = 0;
        for (; i < types.length-1; i++)
        {
            System.out.print(removeClassFromName (types[i].toString()) + ", ");
        }
        if (types.length > 0)   //print last parameter
        {
            System.out.print(removeClassFromName(types[i].toString()));
            //print return type
            System.out.println( " ) " + " -> " + removeClassFromName(y.getGenericReturnType().toString()));
        } */

        for (Type z : types)
            System.out.print(", " + z.toString());
            System.out.println( ") " + " -> " + y.getGenericReturnType().toString());  //*/
    }
}

with my code after I run the code, it outputs the code as below, it does not print out the type correctly. how should I fixed and have that output correctly?

foo(foo, , int, double) -> void

doo() -> double

bar(bar, , int, double, char) -> int





Aucun commentaire:

Enregistrer un commentaire