下面列出了java.lang.reflect.AnnotatedElement#getAnnotationsByType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public StringValidator(AnnotatedElement ae) {
required = ae.isAnnotationPresent(Required.class);
minLength = ae.isAnnotationPresent(MinLength.class)
? ae.getAnnotation(MinLength.class).value() : null;
maxLength = ae.isAnnotationPresent(MaxLength.class)
? ae.getAnnotation(MaxLength.class).value() : null;
pattern = ae.isAnnotationPresent(Pattern.class)
? java.util.regex.Pattern.compile(ae.getAnnotation(Pattern.class).value()) : null;
enums = ae.isAnnotationPresent(EnumString.class)
? new HashSet<String>(Arrays.asList(ae.getAnnotation(EnumString.class).value())) : null;
Format[] formatAnnos = ae.getAnnotationsByType(Format.class);
formats = new StringFormat[formatAnnos.length];
for (int i=0; i<formatAnnos.length; i++) {
formats[i] = newStringFormat(formatAnnos[i].value());
}
}
public StringValidator(AnnotatedElement ae) {
required = ae.isAnnotationPresent(Required.class);
minLength = ae.isAnnotationPresent(MinLength.class)
? ae.getAnnotation(MinLength.class).value() : null;
maxLength = ae.isAnnotationPresent(MaxLength.class)
? ae.getAnnotation(MaxLength.class).value() : null;
pattern = ae.isAnnotationPresent(Pattern.class)
? java.util.regex.Pattern.compile(ae.getAnnotation(Pattern.class).value()) : null;
enums = ae.isAnnotationPresent(EnumString.class)
? new HashSet<String>(Arrays.asList(ae.getAnnotation(EnumString.class).value())) : null;
Format[] formatAnnos = ae.getAnnotationsByType(Format.class);
formats = new StringFormat[formatAnnos.length];
for (int i=0; i<formatAnnos.length; i++) {
formats[i] = newStringFormat(formatAnnos[i].value());
}
}
private List<Class<? extends MethodInterceptor>> collectAnno(AnnotatedElement element) {
Aop[] annoSet = element.getAnnotationsByType(Aop.class);
if (annoSet == null) {
annoSet = new Aop[0];
}
return Arrays.stream(annoSet).flatMap(//
(Function<Aop, Stream<Class<? extends MethodInterceptor>>>) aop -> Arrays.stream(aop.value())//
).collect(Collectors.toList());
}
/**
* Asserts that {@link AnnotatedElement#getAnnotationsByType(Class)} returns the
* expected result. The result is specified using a String. See
* {@link AnnotatedElementTestSupport} for the string syntax.
*/
static void assertGetAnnotationsByType(AnnotatedElement annotatedElement,
Class<? extends Annotation> annotationType, String[] expectedAnnotationStrings)
throws Exception {
Annotation[] annotations = annotatedElement.getAnnotationsByType(annotationType);
assertAnnotationsMatch(annotations, expectedAnnotationStrings);
}