java.lang.annotation.Repeatable Java Examples

The following examples show how to use java.lang.annotation.Repeatable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RepeatableContainers.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static Object computeRepeatedAnnotationsMethod(Class<? extends Annotation> annotationType) {
	AttributeMethods methods = AttributeMethods.forAnnotationType(annotationType);
	if (methods.isOnlyValueAttribute()) {
		Method method = methods.get("value");
		if (method == null) {
			return NONE;
		}
		Class<?> returnType = method.getReturnType();
		if (returnType.isArray()) {
			Class<?> componentType = returnType.getComponentType();
			if (Annotation.class.isAssignableFrom(componentType) &&
					componentType.isAnnotationPresent(Repeatable.class)) {
				return method;
			}
		}
	}
	return NONE;
}
 
Example #2
Source File: AnnotationUtil.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all annotations of the {@code annotationType} searching from the specified {@code element}.
 * The search range depends on the specified {@link FindOption}s.
 *
 * @param element the {@link AnnotatedElement} to find annotations
 * @param annotationType the type of the annotation to find
 * @param findOptions the options to be applied when finding annotations
 */
static <T extends Annotation> List<T> find(AnnotatedElement element, Class<T> annotationType,
                                           EnumSet<FindOption> findOptions) {
    requireNonNull(element, "element");
    requireNonNull(annotationType, "annotationType");

    final Builder<T> builder = new Builder<>();

    // Repeatable is not a repeatable. So the length of the returning array is 0 or 1.
    final Repeatable[] repeatableAnnotations = annotationType.getAnnotationsByType(Repeatable.class);
    final Class<? extends Annotation> containerType =
            repeatableAnnotations.length > 0 ? repeatableAnnotations[0].value() : null;

    for (final AnnotatedElement e : resolveTargetElements(element, findOptions)) {
        for (final Annotation annotation : e.getDeclaredAnnotations()) {
            if (findOptions.contains(FindOption.LOOKUP_META_ANNOTATIONS)) {
                findMetaAnnotations(builder, annotation, annotationType, containerType);
            }
            collectAnnotations(builder, annotation, annotationType, containerType);
        }
    }
    return builder.build();
}
 
Example #3
Source File: Utils.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static List<? extends Annotation> getAnnotation(Annotation a) {
    Class<?> annotated = a.annotationType();
    Method valueMethod = getValueMethod(annotated);
    if (valueMethod != null) {
        Class<?> returnType = valueMethod.getReturnType();
        if (returnType.isArray()) {
            Class<?> candidate = returnType.getComponentType();
            Repeatable r = candidate.getAnnotation(Repeatable.class);
            if (r != null) {
                Class<?> repeatClass = r.value();
                if (annotated == repeatClass) {
                    return getAnnotationValues(a, valueMethod);
                }
            }
        }
    }
    List<Annotation> annos = new ArrayList<>();
    annos.add(a);
    return annos;
}
 
Example #4
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static List<? extends Annotation> getAnnotation(Annotation a) {
    Class<?> annotated = a.annotationType();
    Method valueMethod = getValueMethod(annotated);
    if (valueMethod != null) {
        Class<?> returnType = valueMethod.getReturnType();
        if (returnType.isArray()) {
            Class<?> candidate = returnType.getComponentType();
            Repeatable r = candidate.getAnnotation(Repeatable.class);
            if (r != null) {
                Class<?> repeatClass = r.value();
                if (annotated == repeatClass) {
                    return getAnnotationValues(a, valueMethod);
                }
            }
        }
    }
    List<Annotation> annos = new ArrayList<>();
    annos.add(a);
    return annos;
}
 
Example #5
Source File: Utils.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static List<? extends Annotation> getAnnotation(Annotation a) {
    Class<?> annotated = a.annotationType();
    Method valueMethod = getValueMethod(annotated);
    if (valueMethod != null) {
        Class<?> returnType = valueMethod.getReturnType();
        if (returnType.isArray()) {
            Class<?> candidate = returnType.getComponentType();
            Repeatable r = candidate.getAnnotation(Repeatable.class);
            if (r != null) {
                Class<?> repeatClass = r.value();
                if (annotated == repeatClass) {
                    return getAnnotationValues(a, valueMethod);
                }
            }
        }
    }
    List<Annotation> annos = new ArrayList<>();
    annos.add(a);
    return annos;
}
 
Example #6
Source File: AuthCheckerHandlerInterceptor.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void scanCheckAnnos(AnnotatedElement annotatedElement, TagArrayList<Annotation> checkAnnos) {
    Annotation[] annotations = AnnotationUtils.getAnnotations(annotatedElement);
    if (CollectionUtil.isNotEmpty(annotations)) {
        for (Annotation annotation : annotations) {
            //在anno上查找,是否有anno标注为Check
            Check check = AnnotationUtils.getAnnotation(annotation, Check.class);
            if (check != null) {
                Repeatable repeatable = AnnotationUtils.getAnnotation(annotation, Repeatable.class);
                if (repeatable != null) {
                    Class<? extends Annotation> realCheckAnnoClazz = repeatable.value();
                    Set<? extends Annotation> realCheckAnnos = AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement,
                        realCheckAnnoClazz);
                    checkAnnos.addAll(realCheckAnnos);
                } else {
                    checkAnnos.add(annotation);
                }
            }
        }
    }
}
 
Example #7
Source File: AnnotationExpander.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 繰り返されたアノテーションかどうか判定する。
 * <p>属性「value」に、繰り返しのアノテーション{@link Repeatable}が付与されている
 *    アノテーションの配列を保持しているかどうかで判定する。</p>
 * @param targetAnno
 * @return
 */
private boolean isRepeated(final Annotation targetAnno) {
    
    try {
        final Method method = targetAnno.getClass().getMethod("value");
        
        // 値のクラスタイプがアノテーションの配列かどうかのチェック
        final Class<?> returnType = method.getReturnType();
        if(!(returnType.isArray() && Annotation.class.isAssignableFrom(returnType.getComponentType()))) {
            return false;
        }
        
        final Annotation[] annos = (Annotation[]) method.invoke(targetAnno);
        if(annos.length == 0) {
            return false;
        }
        
        // @Repetableアノテーションが付与されているかどうか
        if(annos[0].annotationType().getAnnotation(Repeatable.class) != null) {
            return true;
        }
        
    } catch (Exception e) {
        
    }
    
    return false;
    
}
 
Example #8
Source File: MetadataWriter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private Expression createContainerAnnotation(List<Annotation> annotations) {
  DeclaredType annotationType = annotations.get(0).getAnnotationMirror().getAnnotationType();
  ArrayType arrayType = typeUtil.getArrayType(annotationType);
  DeclaredType containerType = (DeclaredType) ElementUtil.getAnnotationValue(
      ElementUtil.getAnnotation(annotationType.asElement(), Repeatable.class), "value");
  TypeElement containerElement = (TypeElement) containerType.asElement();
  FunctionElement element = new FunctionElement(
      "create_" + nameTable.getFullName(containerElement), containerType, containerElement);
  FunctionInvocation invocation = new FunctionInvocation(element, containerType);
  element.addParameters(arrayType);
  List<Expression> array = annotations.stream().map(Annotation::getAnnotationMirror)
      .map(translationUtil::createAnnotation).collect(Collectors.toList());
  invocation.addArgument(translationUtil.createObjectArray(array, arrayType));
  return invocation;
}
 
Example #9
Source File: ReflectionsClassFinder.java    From flow with Apache License 2.0 5 votes vote down vote up
private Set<Class<?>> getAnnotatedByRepeatedAnnotation(
        AnnotatedElement annotationClass) {
    Repeatable repeatableAnnotation = annotationClass
            .getAnnotation(Repeatable.class);
    if (repeatableAnnotation != null) {
        return reflections.getTypesAnnotatedWith(
                repeatableAnnotation.value(), true);
    }
    return Collections.emptySet();
}
 
Example #10
Source File: StatementBuilder.java    From neodymium-library with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends Annotation> List<T> getDeclaredAnnotations(AnnotatedElement object, Class<T> annotationClass)
{
    List<T> annotations = new LinkedList<>();
    if (object == null || annotationClass == null)
    {
        return annotations;
    }

    // check if the annotation is repeatable
    Repeatable repeatingAnnotation = annotationClass.getAnnotation(Repeatable.class);
    Annotation annotation = (repeatingAnnotation == null) ? null : object.getDeclaredAnnotation(repeatingAnnotation.value());

    if (annotation != null)
    {
        try
        {
            annotations.addAll(Arrays.asList((T[]) annotation.getClass().getMethod("value").invoke(annotation)));
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    else
    {
        T anno = object.getDeclaredAnnotation(annotationClass);
        if (anno != null)
        {
            annotations.add(anno);
        }
    }

    return annotations;
}
 
Example #11
Source File: StatementBuilder.java    From neodymium-library with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends Annotation> List<T> getAnnotations(AnnotatedElement object, Class<T> annotationClass)
{
    List<T> annotations = new LinkedList<>();
    if (object == null || annotationClass == null)
    {
        return annotations;
    }

    // check if the annotation is repeatable
    Repeatable repeatingAnnotation = annotationClass.getAnnotation(Repeatable.class);
    Annotation annotation = (repeatingAnnotation == null) ? null : object.getAnnotation(repeatingAnnotation.value());

    if (annotation != null)
    {
        try
        {
            annotations.addAll(Arrays.asList((T[]) annotation.getClass().getMethod("value").invoke(annotation)));
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    else
    {
        T anno = object.getAnnotation(annotationClass);
        if (anno != null)
        {
            annotations.add(anno);
        }
    }

    return annotations;
}
 
Example #12
Source File: AnnoConstruct.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        Repeatable repeatable = null;
        repeatable = annoType.getAnnotation(Repeatable.class);
        return (repeatable == null) ? null : repeatable.value();
    }
    return null;
}
 
Example #13
Source File: TypeLibrary.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static List<Annotation> resolveRepeatedAnnotations(Annotation[] annotations) {
    List<Annotation> annos = new ArrayList<>(annotations.length);
    for (Annotation a : annotations) {
        boolean repeated = false;
        Method m;
        try {
            m = a.annotationType().getMethod("value");
            Class<?> returnType = m.getReturnType();
            if (returnType.isArray()) {
                Class<?> ct = returnType.getComponentType();
                if (Annotation.class.isAssignableFrom(ct) && ct.getAnnotation(Repeatable.class) != null) {
                    Object res = m.invoke(a, new Object[0]);
                    if (res != null && Annotation[].class.isAssignableFrom(res.getClass())) {
                        for (Annotation rep : (Annotation[]) m.invoke(a, new Object[0])) {
                            annos.add(rep);
                        }
                        repeated = true;
                    }
                }
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // Ignore, can't access repeatable information
        }
        if (!repeated) {
            annos.add(a);
        }
    }
    return annos;
}
 
Example #14
Source File: TypeLibrary.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static List<Annotation> resolveRepeatedAnnotations(Annotation[] annotations) {
    List<Annotation> annos = new ArrayList<>(annotations.length);
    for (Annotation a : annotations) {
        boolean repeated = false;
        Method m;
        try {
            m = a.annotationType().getMethod("value");
            Class<?> returnType = m.getReturnType();
            if (returnType.isArray()) {
                Class<?> ct = returnType.getComponentType();
                if (Annotation.class.isAssignableFrom(ct) && ct.getAnnotation(Repeatable.class) != null) {
                    Object res = m.invoke(a, new Object[0]);
                    if (res != null && Annotation[].class.isAssignableFrom(res.getClass())) {
                        for (Annotation rep : (Annotation[]) m.invoke(a, new Object[0])) {
                            annos.add(rep);
                        }
                        repeated = true;
                    }
                }
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // Ignore, can't access repeatable information
        }
        if (!repeated) {
            annos.add(a);
        }
    }
    return annos;
}
 
Example #15
Source File: TypeLibrary.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static List<Annotation> resolveRepeatedAnnotations(Annotation[] annotations) {
    List<Annotation> annos = new ArrayList<>(annotations.length);
    for (Annotation a : annotations) {
        boolean repeated = false;
        Method m;
        try {
            m = a.annotationType().getMethod("value");
            Class<?> returnType = m.getReturnType();
            if (returnType.isArray()) {
                Class<?> ct = returnType.getComponentType();
                if (Annotation.class.isAssignableFrom(ct) && ct.getAnnotation(Repeatable.class) != null) {
                    Object res = m.invoke(a, new Object[0]);
                    if (res != null && Annotation[].class.isAssignableFrom(res.getClass())) {
                        for (Annotation rep : (Annotation[]) m.invoke(a, new Object[0])) {
                            annos.add(rep);
                        }
                        repeated = true;
                    }
                }
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // Ignore, can't access repeatable information
        }
        if (!repeated) {
            annos.add(a);
        }
    }
    return annos;
}
 
Example #16
Source File: AnnotationUtils.java    From micronaut-test with Apache License 2.0 4 votes vote down vote up
/**
 * Find all <em>repeatable</em> {@linkplain Annotation annotations} of
 * {@code annotationType} that are either <em>present</em>, <em>indirectly
 * present</em>, or <em>meta-present</em> on the supplied {@link AnnotatedElement}.
 *
 * <p>This method extends the functionality of
 * {@link java.lang.reflect.AnnotatedElement#getAnnotationsByType(Class)}
 * with additional support for meta-annotations.
 *
 * <p>In addition, if the element is a class and the repeatable annotation
 * is {@link java.lang.annotation.Inherited @Inherited}, this method will
 * search on superclasses first in order to support top-down semantics.
 * The result is that this algorithm finds repeatable annotations that
 * would be <em>shadowed</em> and therefore not visible according to Java's
 * standard semantics for inherited, repeatable annotations.
 *
 * <p>If the element is a class and the repeatable annotation is not
 * discovered within the class hierarchy, this method will additionally
 * search on interfaces implemented by each class in the hierarchy.
 *
 * <p>If the supplied {@code element} is {@code null}, this method simply
 * returns an empty list.
 *
 * @param element        the element to search on, potentially {@code null}
 * @param annotationType the repeatable annotation type to search for; never {@code null}
 * @param <A>            the annotation instance
 * @return the list of all such annotations found; neither {@code null} nor mutable
 * @see java.lang.annotation.Repeatable
 * @see java.lang.annotation.Inherited
 */
public static <A extends Annotation> List<A> findRepeatableAnnotations(AnnotatedElement element,
                                                                       Class<A> annotationType) {
    if (annotationType == null) {
        throw new IllegalArgumentException("annotationType must not be null");
    }
    Repeatable repeatable = annotationType.getAnnotation(Repeatable.class);
    if (repeatable == null) {
        throw new IllegalArgumentException(annotationType.getName() + " must be @Repeatable");
    }
    Class<? extends Annotation> containerType = repeatable.value();
    boolean inherited = containerType.isAnnotationPresent(Inherited.class);

    // Short circuit the search algorithm.
    if (element == null) {
        return Collections.emptyList();
    }

    // We use a LinkedHashSet because the search algorithm may discover
    // duplicates, but we need to maintain the original order.
    Set<A> found = new LinkedHashSet<>(16);
    findRepeatableAnnotations(element, annotationType, containerType, inherited, found, new HashSet<>(16));
    // unmodifiable since returned from public, non-internal method(s)
    return Collections.unmodifiableList(new ArrayList<>(found));
}
 
Example #17
Source File: AnnotationUtilsTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void findRepeatableAnnotationOnComposedAnnotation() {
	Repeatable repeatable = findAnnotation(MyRepeatableMeta1.class, Repeatable.class);
	assertNotNull(repeatable);
	assertEquals(MyRepeatableContainer.class, repeatable.value());
}
 
Example #18
Source File: AnnotationUtils.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private static boolean isRepeatable(final Class<?> annotationType) {
    return annotationType.isAnnotationPresent(Repeatable.class);
}
 
Example #19
Source File: AnnoConstruct.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
    Repeatable repeatable = annoType.getAnnotation(Repeatable.class);
    return (repeatable == null) ? null : repeatable.value();
}
 
Example #20
Source File: AnnotationUtilsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void findRepeatableAnnotationOnComposedAnnotation() {
	Repeatable repeatable = findAnnotation(MyRepeatableMeta1.class, Repeatable.class);
	assertNotNull(repeatable);
	assertEquals(MyRepeatableContainer.class, repeatable.value());
}
 
Example #21
Source File: AnnotationUtilsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void findRepeatableAnnotation() {
	Repeatable repeatable = findAnnotation(MyRepeatable.class, Repeatable.class);
	assertNotNull(repeatable);
	assertEquals(MyRepeatableContainer.class, repeatable.value());
}
 
Example #22
Source File: RepeatableContainers.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private Class<? extends Annotation> deduceContainer(Class<? extends Annotation> repeatable) {
	Repeatable annotation = repeatable.getAnnotation(Repeatable.class);
	Assert.notNull(annotation, () -> "Annotation type must be a repeatable annotation: " +
				"failed to resolve container type for " + repeatable.getName());
	return annotation.value();
}
 
Example #23
Source File: AnnotatedElements.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Find the {@code \@Repeatable} container annotation class for an annotation class, or
 * {@code null}.
 *
 * <p>
 * Given:
 *
 * <code>
 *  @Repeatable(X.class)
 *  @interface SomeName {     <--- = annotationClass
 *  }...
 * </code>
 *
 * <p>
 * Returns {@code X.class}
 *
 * Otherwise if there was no {@code \@Repeatable} annotation, return {@code null}.
 * </p>
 */
private static <T extends Annotation> Class<? extends Annotation>
    getRepeatableAnnotationContainerClassFor(Class<T> annotationClass) {

  Repeatable repeatableAnnotation = annotationClass.getDeclaredAnnotation(Repeatable.class);
  return (repeatableAnnotation == null) ? null : repeatableAnnotation.value();
}
 
Example #24
Source File: AnnotationUtils.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Resolve the container type for the supplied repeatable {@code annotationType}.
 * <p>Automatically detects a <em>container annotation</em> declared via
 * {@link java.lang.annotation.Repeatable}. If the supplied annotation type
 * is not annotated with {@code @Repeatable}, this method simply returns
 * {@code null}.
 * @since 4.2
 */
@Nullable
static Class<? extends Annotation> resolveContainerAnnotationType(Class<? extends Annotation> annotationType) {
	Repeatable repeatable = getAnnotation(annotationType, Repeatable.class);
	return (repeatable != null ? repeatable.value() : null);
}
 
Example #25
Source File: AnnotationUtils.java    From beihu-boot with Apache License 2.0 2 votes vote down vote up
/**
 * Resolve the container type for the supplied repeatable {@code annotationType}.
 * <p>Automatically detects a <em>container annotation</em> declared via
 * {@link Repeatable}. If the supplied annotation type
 * is not annotated with {@code @Repeatable}, this method simply returns
 * {@code null}.
 *
 * @since 4.2
 */
static Class<? extends Annotation> resolveContainerAnnotationType(Class<? extends Annotation> annotationType) {
    Repeatable repeatable = getAnnotation(annotationType, Repeatable.class);
    return (repeatable != null ? repeatable.value() : null);
}