獲取方法的參數名字以及參數類型(接上一篇自定義注解)


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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM