@Test 运行 Method xxx should be void、Method xxx should have no parameters

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在使用@MethodParameters注解时,需要先定义注解类型,例如: ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface MethodParameters { String[] value(); } ``` 在上面的代码中,@MethodParameters注解被定义为一个注解类型,它只能用于方法参数上,且保留到运行时。注解中包含一个value属性,用于存储参数名称。 接下来,在使用@MethodParameters注解时,需要在方法参数上添加注解,并指定参数名称,例如: ```java public void testMethod(@MethodParameters({"paramName1", "paramName2"}) String arg1, int arg2) { //... } ``` 在上面的代码中,testMethod方法的第一个参数上使用了@MethodParameters注解,并指定了参数名称为paramName1和paramName2。 最后,在程序运行时,可以通过反射机制获取方法参数的信息,例如: ```java Method method = TestClass.class.getMethod("testMethod", String.class, int.class); Parameter[] parameters = method.getParameters(); for (Parameter parameter : parameters) { MethodParameters methodParameters = parameter.getAnnotation(MethodParameters.class); if (methodParameters != null) { String[] parameterNames = methodParameters.value(); for (String parameterName : parameterNames) { System.out.println("parameter name: " + parameterName); } } } ``` 在上面的代码中,首先获取testMethod方法的参数信息,然后遍历参数数组,如果参数上使用了@MethodParameters注解,则获取注解中的参数名称信息,并输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值