1

I want to return the name of a specific method with the parameter from a class.

I have a program that is also returning the method name but including the class name and package name. The code is:

import java.lang.reflect.*;

public class ClassDemo {
   public static void main(String[] args) {
     ClassDemo cls = new ClassDemo();
     Class c = cls.getClass();

     try {                
        // parameter type is null
        Method m = c.getMethod("show", null);
        System.out.println("method = " + m.toString());        
     }
     catch(NoSuchMethodException e) {
        System.out.println(e.toString());
     }
     try {
        // method Long
        Class[] cArg = new Class[1];
        cArg[0] = Long.class;
        Method lMethod = c.getMethod("showLong", cArg);
        System.out.println("method = " + lMethod.toString());
     }
     catch(NoSuchMethodException e) {
        System.out.println(e.toString());
     }
   }

   public Integer show() {
      return 1;
   }

   public void showLong(Long l) {
      this.l = l;
   }
   long l = 78655;
} 

And the result is:

method = public java.lang.Integer ClassDemo.show()
method = public void ClassDemo.showLong(java.lang.Long)

My question is there any way where I could get the method name with its associated parameter but without the class name and package name?

I mean in that case the result will be :

method = show()
method = showLong(Long)

I saw the question Getting the name of the current executing method but its not what I want. Can anybody give me any solution?

Community
  • 1
  • 1
Nitu08
  • 47
  • 8

3 Answers3

1
System.out.print(m.getName() + "(");
Class<?>[] params = m.getParameterTypes();
for (int i = 0; i < params.length; i++) {
    if (i > 0) {
        System.out.print(", ");
    }
    System.out.print(params[i].getSimpleName());
}
System.out.println(")");
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • Thanks for the answer. But I don't want to print the methods. what if I need to return each method with their parameters within a method. so that I could use each method in some other place. – Nitu08 May 09 '16 at 12:53
  • @Nitu08 in your question you were printing. You can very easily adapt the same logic to concatenate and return a string. – shmosel May 09 '16 at 15:39
  • Yes I did that.Thanks. – Nitu08 May 10 '16 at 14:57
1

To get the method name:

System.out.println(method.getName());

To get the method parameter type names:

Class<?>[] paramTypes = method.getParameterTypes();
for(Class<?> paramType : paramTypes) {
    System.out.println(paramType.getSimpleName());
}
cowls
  • 24,013
  • 8
  • 48
  • 78
0

I have tried to write a method that will return each method with its parameter:

public String getparameter(Method method){

        String  m = method.getName();         
        String str = "";           
             Class<?>[] params = method.getParameterTypes();
             for (int i = 0; i < params.length; i++) {
                 if (i > 0) {
                     //System.out.print(", ");
                 }          
                 str += m+"(" +params[i].getSimpleName()+ ")";
                System.out.println(str);
             }
          return str;
    }

But the problem here is, its returning the methods with only one pacameter. How can I also show the methods that has more than one parameter??

Nitu08
  • 47
  • 8