获取方法的参数名字以及参数类型(接上一篇自定义注解)

1.测试类参数

public JsonResult<String> test(@SocketParam("a") String a) {//自定义注解的参数和方法的参数名字相同
  JsonResult<String> reSocket = new JsonResult<>();
  reSocket.setResult("dddd");
  reSocket.setStatus(JsonResult.SUCCESS);
  System.out.println(a);
  
  return reSocket;
 }

 

2.获取参数名字

  Method[] methods = cla.getMethods();//获取类下的所有方法 cla是该方法的类
     
     for(Method method:methods){
       Annotation[][] parameterAnnotations = method.getParameterAnnotations();
       
       if (parameterAnnotations != null && parameterAnnotations.length > 0) { 
        String[] parameterNames = new String[parameterAnnotations.length];
        int m = 0;
        
        for (Annotation[] parameterAnnotation : parameterAnnotations) { 
         for (Annotation annotation : parameterAnnotation) { 
          if (annotation instanceof SocketParam) { 
           SocketParam param = (SocketParam) annotation; 
           parameterNames[m++] = param.value(); 
          } 
         } 
        }

    system.out.println(Arrays.toString(parameterNames));
         }  
       
      }
     }

类型 method.getParameterTypes();

posted @ 2016-06-23 17:40  joe立  阅读(6381)  评论(0编辑  收藏  举报