1

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

        /*
        //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()));
        } */


    }
}

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?

bar(int, double, char) -> int

doo() -> double

foo(int, double) -> void

And every time when I recompiled and run it, the output is having the different order.

Zoe
  • 11
  • 5

1 Answers1

0

@Zoe as @Sami Sarraj said, your code is doing what is supposed to.

About the order, it is not possible to get the order it was wrote by the getDeclaredMethods() call, as you can see here.

About getting the parameters names you can find a hint here.

Lucas Gonzaga
  • 86
  • 1
  • 3