This is an automated email from the ASF dual-hosted git repository.

arne pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans-meecrowave.git


The following commit(s) were added to refs/heads/master by this push:
     new 3f95fa8  MEECROWAVE-252: Use AnnotationUtils.findAnnotation
3f95fa8 is described below

commit 3f95fa85af8cd640b2d41d27df0cc05675998515
Author: arne <[email protected]>
AuthorDate: Wed Jul 15 19:33:02 2020 +0200

    MEECROWAVE-252: Use AnnotationUtils.findAnnotation
---
 .../meecrowave/junit5/MeecrowaveExtension.java     | 40 +++-------------------
 1 file changed, 4 insertions(+), 36 deletions(-)

diff --git 
a/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
 
b/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
index fd91ac7..18aaeea 100644
--- 
a/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
+++ 
b/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
@@ -18,23 +18,13 @@
  */
 package org.apache.meecrowave.junit5;
 
-import static java.util.Arrays.asList;
-import static java.util.Arrays.stream;
-import static java.util.Collections.emptyList;
 import static java.util.Optional.ofNullable;
-import static java.util.stream.Collectors.toSet;
+import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
 
 import java.io.File;
 import java.lang.annotation.Annotation;
-import java.lang.annotation.Documented;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
 import java.util.Optional;
 
 import javax.enterprise.context.spi.CreationalContext;
@@ -53,16 +43,14 @@ public class MeecrowaveExtension extends BaseLifecycle
     
        private static final ExtensionContext.Namespace NAMESPACE = 
ExtensionContext.Namespace.create(MeecrowaveExtension.class.getName());
        
-       private static final List<Class<? extends Annotation>> 
DEFAULT_ANNOTATION_TYPES = asList(Retention.class, Target.class, 
Documented.class);
-
     private final ScopesExtension scopes = new ScopesExtension() {
         @Override
         protected Optional<Class<? extends Annotation>[]> getScopes(final 
ExtensionContext context) {
             return context.getElement()
-                          .map(e -> 
getConfigAnnotation(asList(e.getAnnotations()))
+                          .map(e -> findAnnotation(context.getElement(), 
MeecrowaveConfig.class)
                                   .orElseGet(() -> context.getParent()
                                                           
.flatMap(ExtensionContext::getElement)
-                                                          .flatMap(it -> 
getConfigAnnotation(asList(it.getAnnotations())))
+                                                          .flatMap(it -> 
findAnnotation(it, MeecrowaveConfig.class))
                                                           .orElse(null)))
                           .map(MeecrowaveConfig::scopes)
                           .filter(s -> s.length > 0);
@@ -72,8 +60,7 @@ public class MeecrowaveExtension extends BaseLifecycle
     @Override
     public void beforeAll(final ExtensionContext context) {
         final Meecrowave.Builder builder = new Meecrowave.Builder();
-        final Optional<MeecrowaveConfig> meecrowaveConfig
-               = 
getConfigAnnotation(context.getElement().map(AnnotatedElement::getAnnotations).map(Arrays::asList).orElse(emptyList()));
+        final Optional<MeecrowaveConfig> meecrowaveConfig = 
findAnnotation(context.getElement(), MeecrowaveConfig.class);
         final String ctx;
         if (meecrowaveConfig.isPresent()) {
             final MeecrowaveConfig config = meecrowaveConfig.get();
@@ -149,25 +136,6 @@ public class MeecrowaveExtension extends BaseLifecycle
         }
     }
 
-    private static Optional<MeecrowaveConfig> 
getConfigAnnotation(Collection<Annotation> annotations) {
-       while (!annotations.isEmpty()) {
-               Optional<MeecrowaveConfig> config = annotations.stream()
-                               .filter(a -> 
a.annotationType().equals(MeecrowaveConfig.class))
-                               .map(a -> MeecrowaveConfig.class.cast(a))
-                               .findFirst();
-               if (config.isPresent()) {
-                       return config;
-               }
-               annotations = annotations
-                               .stream()
-                               .map(Annotation::annotationType)
-                               .flatMap(a -> stream(a.getAnnotations()))
-                               .filter(a -> 
!DEFAULT_ANNOTATION_TYPES.contains(a.annotationType()))
-                               .collect(toSet());
-       }
-       return Optional.empty();
-    }
-
        private void doRelease(final ExtensionContext context) {
         ofNullable(context.getStore(NAMESPACE).get(CreationalContext.class, 
CreationalContext.class))
                 .ifPresent(CreationalContext::release);

Reply via email to