英文:
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("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.
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<? 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;
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论