可以获取方法调用中带注释的参数的值吗?

huangapple go评论75阅读模式
英文:

Can I get the value of an annotated parameter of a method call?

问题

以下是您提供的代码的翻译部分:

我想要获取方法调用中使用的带注解参数的值

public class Launch {

  public static void main(String[] args) {
    System.out.println("hello");
    testAnn(15);
  }

  private static void testAnn(@IntRange(minValue = 1, maxValue = 10) int babyAge) {
    System.out.println("babyAge is :" + babyAge);
  }
}

我所尝试的是创建一个自定义注解来验证整数值的范围该注解接受 `min``max`因此如果有人使用超出此范围的整数值调用此函数将会出现错误消息并提供一些关于出错原因的提示

我正在编写一个 Java 注解处理器以获取该值并将其与 `@IntRange` 中包含的 `max/min` 进行比较

这是我得到的代码

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   for (TypeElement annotation : annotations) {
       Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(IntRange.class);
       for (Element element : annotatedElements) {

           Executable methodElement = (Executable) element.asType();
           ExecutableType methodExecutableType = (ExecutableType) element.asType();
           String elementParamClassName = methodElement.getParameterTypes()[0].getCanonicalName();
           if (!elementParamClassName.equals(PARAM_TYPE_NAME)) {
               messager.printMessage(Diagnostic.Kind.ERROR, "参数类型应为 int,而不是 " + elementParamClassName);
           } else {
               IntRange rangeAnno = element.getAnnotation(IntRange.class);
               int maxValue = rangeAnno.maxValue();
               int minValue = rangeAnno.minValue();
               // 用于获取传递给带有注解参数(@IntRange(..))的函数的参数的代码

           }
       }
   }
   return true;
}

请注意,上述翻译仅包含您提供的代码部分,不包括额外的回答或解释。如果您需要进一步的翻译或有其他问题,请随时提问。

英文:

I want to get the value of an annotated parameter used in a method invocation:

public class Launch {
public static void main(String[] args) {
System.out.println(&quot;hello&quot;);
testAnn(15);
}
private static void testAnn(@IntRange(minValue = 1,maxValue = 10)int babyAge) {
System.out.println(&quot;babyAge is :&quot;+babyAge);
}
}

What I'm trying is create a custom annotation to validate an integer value range, the annotation takes min and max value, so if anyone calls this function using an integer value outside this range, a message error will occur with some hints about what is going wrong.

I'm working in a Java Annotation Process to get the value and compare it with the included max/min ones in @IntRange

This is what I got:

 @Override
public boolean process(Set&lt;? extends TypeElement&gt; annotations, RoundEnvironment roundEnv) {
for(TypeElement annotaion: annotations) {
Set&lt;? extends Element&gt; annotatedElements = roundEnv.getElementsAnnotatedWith(IntRange.class);
for (Element element : annotatedElements) {
Executable methodElement= (Executable) element.asType();
ExecutableType methodExcutableType = (ExecutableType) element.asType();
String elementParamClassName = methodElement.getParameterTypes()[0].getCanonicalName();
if(!elementParamClassName.equals(PARAM_TYPE_NAME)) {
messager.printMessage(Diagnostic.Kind.ERROR,&quot;Parameter type should be int not &quot;+elementParamClassName);
}else {
IntRange rangeAnno = element.getAnnotation(IntRange.class);
int maxValue = rangeAnno.maxValue();
int minValue = rangeAnno.minValue();
//code to retrive argument passed to the function with
//annotated parameter (@IntRange(..))
}
}
}
return true;
}

答案1

得分: 0

因为Java注解处理只在编译时起作用,所以您无法获取方法参数的当前值,只能获取@IntRange注解提供的值。更多信息请参考这里

您实际需要的是自定义验证器,您可以在这里找到如何实现。

英文:

You cannot use a Java Annotation Processing because it works only at compile time, so you will be able to get the provided values of @IntRange annotation but not the current one of the method parameter. More information here

What you really need is a custom validator and you can find how to do it here

huangapple
  • 本文由 发表于 2020年9月20日 21:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63979313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定