Search in sources :

Example 1 with Repeatable

use of java.lang.annotation.Repeatable in project junit5 by junit-team.

the class AnnotationUtils method findRepeatableAnnotations.

/**
 * @see org.junit.platform.commons.support.AnnotationSupport#findRepeatableAnnotations(AnnotatedElement, Class)
 */
public static <A extends Annotation> List<A> findRepeatableAnnotations(AnnotatedElement element, Class<A> annotationType) {
    Preconditions.notNull(annotationType, "annotationType must not be null");
    Repeatable repeatable = annotationType.getAnnotation(Repeatable.class);
    Preconditions.notNull(repeatable, () -> 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));
}
Also used : Repeatable(java.lang.annotation.Repeatable) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with Repeatable

use of java.lang.annotation.Repeatable in project openj9 by eclipse.

the class Class method internalGetDeclaredAnnotationsByType.

private <A extends Annotation> ArrayList<A> internalGetDeclaredAnnotationsByType(Class<A> annotationClass) {
    AnnotationCache currentAnnotationCache = getAnnotationCache();
    ArrayList<A> annotationsList = new ArrayList<>();
    LinkedHashMap<Class<? extends Annotation>, Annotation> map = currentAnnotationCache.directAnnotationMap;
    if (map != null) {
        Repeatable repeatable = annotationClass.getDeclaredAnnotation(Repeatable.class);
        if (repeatable == null) {
            A annotation = (A) map.get(annotationClass);
            if (annotation != null) {
                annotationsList.add(annotation);
            }
        } else {
            Class<? extends Annotation> containerType = repeatable.value();
            // if the annotation and its container are both present, the order must be maintained
            for (Map.Entry<Class<? extends Annotation>, Annotation> entry : map.entrySet()) {
                Class<? extends Annotation> annotationType = entry.getKey();
                if (annotationType == annotationClass) {
                    annotationsList.add((A) entry.getValue());
                } else if (annotationType == containerType) {
                    A[] containedAnnotations = (A[]) getAnnotationsArrayFromValue(entry.getValue(), containerType, annotationClass);
                    if (containedAnnotations != null) {
                        annotationsList.addAll(Arrays.asList(containedAnnotations));
                    }
                }
            }
        }
    }
    return annotationsList;
}
Also used : Repeatable(java.lang.annotation.Repeatable) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Repeatable

use of java.lang.annotation.Repeatable in project graal by oracle.

the class AnnotationTypeFeature method duringAnalysis.

@Override
public void duringAnalysis(DuringAnalysisAccess access) {
    DuringAnalysisAccessImpl accessImpl = (DuringAnalysisAccessImpl) access;
    AnalysisUniverse universe = accessImpl.getUniverse();
    /*
         * JDK implementation of repeatable annotations always instantiates an array of a requested
         * annotation. We need to mark arrays of all reachable annotations as in heap.
         */
    universe.getTypes().stream().filter(AnalysisType::isAnnotation).filter(AnalysisType::isInTypeCheck).map(type -> universe.lookup(type.getWrapped()).getArrayClass()).filter(annotationArray -> !annotationArray.isInstantiated()).forEach(annotationArray -> {
        accessImpl.registerAsInHeap(annotationArray);
        access.requireAnalysisIteration();
    });
    Stream<AnnotatedElement> allElements = Stream.concat(Stream.concat(universe.getFields().stream(), universe.getMethods().stream()), universe.getTypes().stream());
    Stream<AnnotatedElement> newElements = allElements.filter(visitedElements::add);
    newElements.forEach(this::reportAnnotation);
}
Also used : AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) ImageSingletons(org.graalvm.nativeimage.ImageSingletons) Feature(org.graalvm.nativeimage.Feature) AutomaticFeature(com.oracle.svm.core.annotate.AutomaticFeature) AnalysisUniverse(com.oracle.graal.pointsto.meta.AnalysisUniverse) DuringAnalysisAccessImpl(com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl) EconomicSet(org.graalvm.collections.EconomicSet) AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) AnnotationTypeSupport(com.oracle.svm.core.hub.AnnotationTypeSupport) Objects(java.util.Objects) Stream(java.util.stream.Stream) Repeatable(java.lang.annotation.Repeatable) AfterRegistrationAccessImpl(com.oracle.svm.hosted.FeatureImpl.AfterRegistrationAccessImpl) Annotation(java.lang.annotation.Annotation) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) DuringAnalysisAccessImpl(com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl) AnalysisUniverse(com.oracle.graal.pointsto.meta.AnalysisUniverse)

Example 4 with Repeatable

use of java.lang.annotation.Repeatable in project strimzi by strimzi.

the class StrimziRunner method annotations.

/**
 * Get the (possibly @Repeatable) annotations on the given element.
 * @param element
 * @param annotationType
 * @param <A>
 * @return
 */
<A extends Annotation> List<A> annotations(Annotatable element, Class<A> annotationType) {
    final List<A> list;
    A c = element.getAnnotation(annotationType);
    if (c != null) {
        list = singletonList(c);
    } else {
        Repeatable r = annotationType.getAnnotation(Repeatable.class);
        if (r != null) {
            Class<? extends Annotation> ra = r.value();
            Annotation container = element.getAnnotation(ra);
            if (container != null) {
                try {
                    Method value = ra.getDeclaredMethod("value");
                    list = asList((A[]) value.invoke(container));
                } catch (ReflectiveOperationException e) {
                    throw new RuntimeException(e);
                }
            } else {
                list = emptyList();
            }
        } else {
            list = emptyList();
        }
    }
    return list;
}
Also used : Repeatable(java.lang.annotation.Repeatable) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Annotation(java.lang.annotation.Annotation)

Example 5 with Repeatable

use of java.lang.annotation.Repeatable in project mule by mulesoft.

the class MuleExtensionAnnotationParser method parseRepeatableAnnotation.

public static <T extends Annotation> List<AnnotationValueFetcher<T>> parseRepeatableAnnotation(Type extensionType, Class<T> annotation, Function<Annotation, T[]> containerConsumer) {
    List<AnnotationValueFetcher<T>> annotationDeclarations = ImmutableList.of();
    Repeatable repeatableContainer = annotation.getAnnotation(Repeatable.class);
    if (repeatableContainer != null) {
        Optional<? extends AnnotationValueFetcher<? extends Annotation>> container = extensionType.getValueFromAnnotation(repeatableContainer.value());
        if (container.isPresent()) {
            annotationDeclarations = container.get().getInnerAnnotations((Function) containerConsumer);
        }
    }
    Optional<AnnotationValueFetcher<T>> singleDeclaration = extensionType.getValueFromAnnotation(annotation);
    if (singleDeclaration.isPresent()) {
        annotationDeclarations = Collections.singletonList(singleDeclaration.get());
    }
    return annotationDeclarations;
}
Also used : Repeatable(java.lang.annotation.Repeatable) Function(java.util.function.Function) AnnotationValueFetcher(org.mule.runtime.module.extension.api.loader.java.type.AnnotationValueFetcher)

Aggregations

Repeatable (java.lang.annotation.Repeatable)9 Annotation (java.lang.annotation.Annotation)4 AnalysisType (com.oracle.graal.pointsto.meta.AnalysisType)2 AnalysisUniverse (com.oracle.graal.pointsto.meta.AnalysisUniverse)2 AutomaticFeature (com.oracle.svm.core.annotate.AutomaticFeature)2 AnnotationTypeSupport (com.oracle.svm.core.hub.AnnotationTypeSupport)2 AfterRegistrationAccessImpl (com.oracle.svm.hosted.FeatureImpl.AfterRegistrationAccessImpl)2 DuringAnalysisAccessImpl (com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 Method (java.lang.reflect.Method)2 Objects (java.util.Objects)2 Stream (java.util.stream.Stream)2 EconomicSet (org.graalvm.collections.EconomicSet)2 Feature (org.graalvm.nativeimage.Feature)2 ImageSingletons (org.graalvm.nativeimage.ImageSingletons)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1