@Override @SuppressWarnings("unchecked") @Nullable public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { for (Annotation ann : annotations) { if (ann.annotationType() == annotationClass) { return (T) ann; } } return null; } @Override
/** * Construct a new {@code DefaultAnnotationAttributeExtractor}. * @param annotation the annotation to synthesize; never {@code null} * @param annotatedElement the element that is annotated with the supplied * annotation; may be {@code null} if unknown */ DefaultAnnotationAttributeExtractor(Annotation annotation, @Nullable Object annotatedElement) { super(annotation.annotationType(), annotatedElement, annotation); }
private static boolean isNullable(Annotation[] annotations) { for (Annotation annotation : annotations) { if (NULLABLE_ANNOTATION_SIMPLE_NAMES.contains(annotation.annotationType().getSimpleName())) { return true; } } return false; }
private AspectJAnnotationType determineAnnotationType(A annotation) { AspectJAnnotationType type = annotationTypeMap.get(annotation.annotationType()); if (type != null) { return type; } throw new IllegalStateException("Unknown annotation type: " + annotation); }
@Override public boolean hasAnnotation(String annotationName) { for (Annotation ann : this.annotations) { if (ann.annotationType().getName().equals(annotationName)) { return true; } } return false; }
@Override public Set<String> getAnnotationTypes() { Set<String> types = new LinkedHashSet<>(); for (Annotation ann : this.annotations) { types.add(ann.annotationType().getName()); } return types; }
private void addAnnotationsToMap( Map<Class<? extends Annotation>, Annotation> annotationMap, @Nullable AnnotatedElement object) { if (object != null) { for (Annotation annotation : object.getAnnotations()) { annotationMap.put(annotation.annotationType(), annotation); } } }
private static Set<? extends Annotation> jsonAnnotations(Annotation[] annotations) { Set<Annotation> result = null; for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(JsonQualifier.class)) { if (result == null) result = new LinkedHashSet<>(); result.add(annotation); } } return result != null ? unmodifiableSet(result) : Collections.<Annotation>emptySet(); } }
@Override @Nullable public Object process(@Nullable AnnotatedElement annotatedElement, Annotation annotation, int metaDepth) { types.add(annotation.annotationType().getName()); return CONTINUE; } }, new HashSet<>(), 1);
@Override public @Nullable Converter<ResponseBody, ?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { for (Annotation annotation : annotations) { Converter.Factory factory = factories.get(annotation.annotationType()); if (factory != null) { return factory.responseBodyConverter(type, annotations, retrofit); } } return null; }
@Override public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { for (Annotation annotation : parameterAnnotations) { Converter.Factory factory = factories.get(annotation.annotationType()); if (factory != null) { return factory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); } } return null; } }
/** * Determine if the supplied {@link Annotation} is defined in the core JDK * {@code java.lang.annotation} package. * @param annotation the annotation to check * @return {@code true} if the annotation is in the {@code java.lang.annotation} package */ public static boolean isInJavaLangAnnotationPackage(@Nullable Annotation annotation) { return (annotation != null && isInJavaLangAnnotationPackage(annotation.annotationType())); }
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { for (Annotation annotation : getAnnotations()) { if (annotation.annotationType() == annotationClass) { return true; } } return false; }
@Override @Nullable @SuppressWarnings("unchecked") public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { for (Annotation annotation : getAnnotations()) { if (annotation.annotationType() == annotationClass) { return (T) annotation; } } return null; }
@Override public String toString() { StringBuilder builder = new StringBuilder(); for (Annotation ann : getAnnotations()) { builder.append("@").append(ann.annotationType().getName()).append(' '); } builder.append(getResolvableType().toString()); return builder.toString(); }
private String formatAnnotation(Annotation annotation) { Map<String, Object> map = AnnotationUtils.getAnnotationAttributes(annotation); map.forEach((key, value) -> { if (value.equals(DEFAULT_VALUE_NONE)) { map.put(key, "NONE"); } }); return annotation.annotationType().getName() + map; }
private String formatAnnotation(Annotation annotation) { Map<String, Object> map = AnnotationUtils.getAnnotationAttributes(annotation); map.forEach((key, value) -> { if (value.equals(DEFAULT_VALUE_NONE)) { map.put(key, "NONE"); } }); return annotation.annotationType().getName() + map; }
@Test public void getDefaultValueFromNonPublicAnnotation() { Annotation[] declaredAnnotations = NonPublicAnnotatedClass.class.getDeclaredAnnotations(); assertEquals(1, declaredAnnotations.length); Annotation annotation = declaredAnnotations[0]; assertNotNull(annotation); assertEquals("NonPublicAnnotation", annotation.annotationType().getSimpleName()); assertEquals(-1, getDefaultValue(annotation, VALUE)); assertEquals(-1, getDefaultValue(annotation)); }
@Test @SuppressWarnings("unchecked") public void findAnnotationDescriptorForTypesForClassWithMetaAnnotatedInterface() { Component rawAnnotation = AnnotationUtils.findAnnotation(ClassWithMetaAnnotatedInterface.class, Component.class); UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes( ClassWithMetaAnnotatedInterface.class, Service.class, Component.class, Order.class, Transactional.class); assertNotNull(descriptor); assertEquals(ClassWithMetaAnnotatedInterface.class, descriptor.getRootDeclaringClass()); assertEquals(Meta1.class, descriptor.getDeclaringClass()); assertEquals(rawAnnotation, descriptor.getAnnotation()); assertEquals(Meta1.class, descriptor.getComposedAnnotation().annotationType()); }
@Test public void findAnnotationDescriptorForClassWithMetaAnnotatedInterface() { Component rawAnnotation = AnnotationUtils.findAnnotation(ClassWithMetaAnnotatedInterface.class, Component.class); AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(ClassWithMetaAnnotatedInterface.class, Component.class); assertNotNull(descriptor); assertEquals(ClassWithMetaAnnotatedInterface.class, descriptor.getRootDeclaringClass()); assertEquals(Meta1.class, descriptor.getDeclaringClass()); assertEquals(rawAnnotation, descriptor.getAnnotation()); assertEquals(Meta1.class, descriptor.getComposedAnnotation().annotationType()); }