how to get only the specific return type and method names without fully qualified return types and method names in java

Viewed 123

By using the following way,

Class c = Class.forName("java.lang.Double");
Method[] m = c.getDeclaredMethods();
for(Method m1 : m)
System.out.println(m1);

the output is:

public boolean java.lang.Double.equals(java.lang.Object)
public static java.lang.String java.lang.Double.toString(double)
public java.lang.String java.lang.Double.toString()
public int java.lang.Double.hashCode()
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------

Where I need to make changes to get return type and method names without package name to get the desired output as:

public boolean equals(Object)
public static String toString(double)
public String toString()
public int hashCode()
--------------------------------------------
--------------------------------------------
--------------------------------------------

Got the solution, by breaking the whole method into several parts, taking modifier, return type, method name and parameter name separately.

Method[] m = c.getDeclaredMethods();
    Parameter[] param;
    int paramLen;
    for(Method m1 : m){
      System.out.print(Modifier.toString(m1.getModifiers()) + " ");
      System.out.print(m1.getReturnType().getSimpleName());
      System.out.print(" ");
      System.out.print(m1.getName() + "(");
      param = m1.getParameters();
      paramLen = param.length;
      for(Parameter param1 : param){
        System.out.print(param1.getType().getSimpleName() + " " + param1.getName());
        if(--paramLen > 0)
        System.out.print(", ");
      }
    System.out.println(")");
1 Answers

There’s no built-in toString method for methods using simple names. The closest, I could find, is MethodType.toString():

Returns a string representation of the method type, of the form "(PT0,PT1...)RT". The string representation of a method type is a parenthesis enclosed, comma separated list of type names, followed immediately by the return type.

Each type is represented by its simple name.

So, this would still need adaptation to be used for a method description.

Note you should use Modifier.toString(m1.getModifiers() & Modifier.methodModifiers()) as suggested in the documentation of Modifier.toString(…), to filter out inappropriate bits.

Further, limit the scope of variables as far as possible, i.e. don’t declare the variables param and paramLen outside the loop but at the place of their first use. But you can also simplify the code using StringJoiner:

for(Method m: c.getDeclaredMethods()) {
    StringJoiner j = new StringJoiner(" ");
    String access = Modifier.toString(m.getModifiers() & Modifier.methodModifiers());
    if(!access.isEmpty()) j.add(access);
    j.add(m.getReturnType().getSimpleName());
    StringJoiner argJ = new StringJoiner(", ");
    for(Class<?> cl: m.getParameterTypes()) argJ.add(cl.getSimpleName());
    System.out.println(j.add(m.getName()+"("+argJ+")"));
}

We can achieve the same but utilizing the toString() method of MethodType mentioned at the beginning:

for(Method m: c.getDeclaredMethods()) {
    StringJoiner j = new StringJoiner(" ");
    String access = Modifier.toString(m.getModifiers() & Modifier.methodModifiers());
    if(!access.isEmpty()) j.add(access);
    String shortSig = MethodType.methodType(m.getReturnType(), m.getParameterTypes())
                                .toString();
    int split = shortSig.lastIndexOf(')') + 1;
    j.add(shortSig.substring(split)).add(m.getName()+shortSig.substring(0, split));
    System.out.println(j);
}

Both variants only print type names, just as Method’s toString() method does. If you want to print parameter names, as your variant does, the first of this answer’s variants can be adapted:

for(Method m: c.getDeclaredMethods()) {
    StringJoiner j = new StringJoiner(" ");
    String access = Modifier.toString(m.getModifiers() & Modifier.methodModifiers());
    if(!access.isEmpty()) j.add(access);
    j.add(m.getReturnType().getSimpleName());
    StringJoiner argJ = new StringJoiner(", ");
    for(Parameter p: m.getParameters())
        argJ.add(p.getType().getSimpleName()+' '+p.getName());
    System.out.println(j.add(m.getName()+"("+argJ+")"));
}
Related