[转]获取java类中所有方法及其参数

获取java类中所有方法及其参数

4064人阅读 评论(0) 收藏 举报
分类:
  1.     private void getReflectAllMethod( Class <?> mLocalClass){  
  2.       
  3.     Class<?> c;  
  4.     c = mLocalClass;  
  5. try {  
  6. do{  
  7.   
  8.   
  9.   
  10.      Method m[] = c.getDeclaredMethods(); // 取得全部的方法  
  11.      for (int i = 0; i < m.length; i++) {  
  12.       String mod = Modifier.toString(m[i].getModifiers()); // 取得访问权限  
  13.       String metName = m[i].getName(); // 取得方法名称  
  14.       Class<?> ret = m[i].getReturnType();// 取得返回值类型  
  15.       Class<?> param[] = m[i].getParameterTypes(); // 得到全部的参数类型  
  16.       Class<?> exc[] = m[i].getExceptionTypes(); // 得到全部的异常  
  17.       System.out.print(mod + " ");//输出每一方法的访问权限  
  18.       System.out.print(ret + " ");//输出每一方法的返回值类型  
  19.       System.out.print(metName + " (");//输出每一方法的名字  
  20.       for (int x = 0; x < param.length; x++) {//循环输出每一方法的参数的类型  
  21.        System.out.print(param[x] + "arg-" + x);  
  22.        if (x < param.length - 1) {  
  23.         System.out.print(",");  
  24.        }  
  25.       }  
  26.       System.out.print(") ");  
  27.       if (exc.length > 0) {// 有异常抛出  
  28.        System.out.print("throws ");  
  29.        for (int x = 0; x < exc.length; x++) {//循环输出每一方法所抛出的异常名字  
  30.         System.out.print(exc[x].getName());  
  31.         if (x < param.length - 1) {  
  32.          System.out.print(",");  
  33.         }  
  34.        }  
  35.       }  
  36.       System.out.println();  
  37.      }  
  38.      c=c.getSuperclass();  
  39. }while(c!=null);  
  40. } catch (Exception e) {  
  41. // TODO Auto-generated catch block  
  42. e.printStackTrace();  
  43. }  
  44.     }  
    private void getReflectAllMethod( Class <?> mLocalClass){
    
    Class<?> c;
    c = mLocalClass;
try {
do{



     Method m[] = c.getDeclaredMethods(); // 取得全部的方法
     for (int i = 0; i < m.length; i++) {
      String mod = Modifier.toString(m[i].getModifiers()); // 取得访问权限
      String metName = m[i].getName(); // 取得方法名称
      Class<?> ret = m[i].getReturnType();// 取得返回值类型
      Class<?> param[] = m[i].getParameterTypes(); // 得到全部的参数类型
      Class<?> exc[] = m[i].getExceptionTypes(); // 得到全部的异常
      System.out.print(mod + " ");//输出每一方法的访问权限
      System.out.print(ret + " ");//输出每一方法的返回值类型
      System.out.print(metName + " (");//输出每一方法的名字
      for (int x = 0; x < param.length; x++) {//循环输出每一方法的参数的类型
       System.out.print(param[x] + "arg-" + x);
       if (x < param.length - 1) {
        System.out.print(",");
       }
      }
      System.out.print(") ");
      if (exc.length > 0) {// 有异常抛出
       System.out.print("throws ");
       for (int x = 0; x < exc.length; x++) {//循环输出每一方法所抛出的异常名字
        System.out.print(exc[x].getName());
        if (x < param.length - 1) {
         System.out.print(",");
        }
       }
      }
      System.out.println();
     }
     c=c.getSuperclass();
}while(c!=null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }

 

 
0
1
 

 

 

posted @ 2017-07-26 17:54  sudenbutcher  阅读(1017)  评论(0)    收藏  举报