java 确定字段是否使用给定的注释进行注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15210588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine whether field is annotated with a given annotations
提问by Dave
I don't know if you managed to figure out what i am trying to do just from the title so I'll try to explain with example
我不知道你是否设法从标题中弄清楚我想要做什么,所以我会尝试用例子来解释
Lets suppose I have created my own annotation @VerifySomething
假设我已经创建了自己的注释 @VerifySomething
And I have created test class for that annotation to make sure it works.
我已经为该注释创建了测试类以确保它有效。
Now lets suppose that I have class SomeClass
with field something
anoted with annotation @VerifySomething
现在让我们假设我有一个带有注释的SomeClass
字段的类something
@VerifySomething
class SomeClass {
@VerifySomething
String something;
}
So far so good, but now I want to create test class
for my SomeClass
however I don't see any point in verifying all the test cases of something as I have already checked that @VerifySomething
works as it should in class which tests that annotation, however I need to make sure that field something actually has @VerifySomething
annotation without copy pasting all these test cases.
到目前为止一切顺利,但现在我想test class
为我创建,SomeClass
但是我认为验证某些东西的所有测试用例没有任何意义,因为我已经检查@VerifySomething
过它在测试该注释的课堂上应该正常工作,但是我需要确保该字段实际上具有@VerifySomething
注释,而无需复制粘贴所有这些测试用例。
So my question is, is there a way to check if some field has one or more required annotation without writing test cases I have already written in annotation test class to make sure it works as it should.
所以我的问题是,有没有一种方法可以检查某个字段是否有一个或多个必需的注释,而无需编写我已经在注释测试类中编写的测试用例,以确保它按预期工作。
回答by Pavla Nováková
You can use getAnnotation
to determine if there is a specific Annotation on the field, which takes the annotation class as a parameter:
可以getAnnotation
用来判断字段上是否有特定的Annotation,以注解类为参数:
field.getAnnotation( SomeAnnotation.class );
Here is a method you can use to verify that a class has a field annotated by given annotation:
下面是一种方法,可用于验证类是否具有由给定注释注释的字段:
import java.lang.reflect.Field;
import javax.validation.constraints.NotNull;
import org.junit.Test;
import org.springframework.util.Assert;
public class TestHasAnnotatedField {
@Test
public void testHasFieldsWithAnnotation() throws SecurityException, NoSuchFieldException {
Class<?>[] classesToVerify = new Class[] {MyClass1.class, MyClass2.class};
for (Class<?> clazz : classesToVerify) {
Field field = clazz.getDeclaredField("something");
Assert.notNull(field.getAnnotation(NotNull.class));
}
}
static class MyClass1 { @NotNull String something; }
static class MyClass2 { @NotNull String something; }
}
回答by Hardy
I am not sure I am following, but in case you want to verify whether a class and or its properties have a given Bean Validation constraint, you can use the meta data api. The entry point is Validator#getConstraintsForClass(SomeClass.class). You get a BeanDescriptor. From there you can do _beanDescriptor#getConstraintsForProperty("something") which gives you a PropertyDescriptor. Via propertyDescriptor#getConstraintDescriptors()you get then a set of ConstraintDescriptorswhich you can iterate to verify that a given constraint annotation was used.
我不确定我是否在关注,但如果您想验证类及其属性是否具有给定的 Bean 验证约束,您可以使用元数据 api。入口点是Validator#getConstraintsForClass(SomeClass.class)。你得到一个BeanDescriptor。从那里你可以做 _beanDescriptor#getConstraintsForProperty("something") 这给你一个PropertyDescriptor。通过propertyDescriptor#getConstraintDescriptors(),您将获得一组ConstraintDescriptors,您可以对其进行迭代以验证是否使用了给定的约束注释。
Note, this is a Bean Validation specific solution compared to generic reflection as in the answer above, but it depends what you are really after. To be honest I don't quite understand your use case yet.
请注意,与上面的答案中的通用反射相比,这是一个 Bean 验证特定的解决方案,但这取决于您真正想要的是什么。老实说,我还不太了解您的用例。