简体   繁体   English

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

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

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("hello");
    testAnn(15);
  }

  private static void testAnn(@IntRange(minValue = 1,maxValue = 10)int babyAge) {
    System.out.println("babyAge is :"+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.我正在尝试创建一个自定义注释来验证 integer 值范围,该注释采用minmax ,因此如果有人使用此范围之外的 integer 值调用此 function,则会出现一条消息错误,并提供一些提示出错了。

I'm working in a Java Annotation Process to get the value and compare it with the included max/min ones in @IntRange我在 Java 注释过程中工作以获取值并将其与@IntRange中包含的max/min进行比较

This is what I got:这就是我得到的:

 @Override
 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    for(TypeElement annotaion: annotations) {
        Set<? extends Element> 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,"Parameter type should be int not "+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;
}

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.您不能使用 Java 注释处理,因为它仅在编译时起作用,因此您将能够获取@IntRange注释提供的值,但不能获取方法参数的当前值。 More information here更多信息在这里

What you really need is a custom validator and you can find how to do it here您真正需要的是自定义验证器,您可以在此处找到如何操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AspectJ - 获取带注释的方法参数的值 - AspectJ - Get value of annotated method parameter 此方法调用为非空方法参数传递空值。 将该参数注释为应始终为非null的参数 - This method call passes a null value for a nonnull method parameter. Either the parameter is annotated as a parameter that should always be nonnull 如何在Java中获取带注释的值? - How can I get the annotated value in java? IntelliJ:如果我调用以@VisibleForTesting注释的方法,则会收到警告 - IntelliJ: get a warning if I call a method annotated with @VisibleForTesting 带注释的方法之后,可以使用注释来调用方法吗? - Can I use annotation to call method after the annotated one? 使用AspectJ设置带注释的方法参数的值 - Using AspectJ to set the value of an annotated method parameter 如何获取带注释的方法参数及其注释 - How to get annotated method parameter and his annotation 我可以在用@PostConstruct 注释的方法中获取所有bean吗? - Can I get all the beans in a method annotated with @PostConstruct? 使用 Spring AOP 从带注释的方法中获取带注释的参数值 - Get annotated param value from annotated method using Spring AOP 如何在带注释的方法的每个方法调用上执行某些操作? - How do I perform something on each method call of an annotated method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM