zoukankan      html  css  js  c++  java
  • 获取方法的参数名字以及参数类型(接上一篇自定义注解)

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

  • 相关阅读:
    Java基础教程:Java内存区域
    Java基础教程:多线程基础——线程池
    微服务实践:服务治理
    微服务实践:服务设计
    微服务实践:什么是微服务
    SpringBoot学习笔记:读取配置文件
    Java进阶教程:使用Lombok提升开发效率
    Sagas模式
    执行力:Just Do It
    执行力:Just Do It
  • 原文地址:https://www.cnblogs.com/difme/p/5611630.html
Copyright © 2011-2022 走看看