camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From astefanu...@apache.org
Subject [2/2] camel git commit: Camel CDI: annotation type predicate
Date Wed, 20 Apr 2016 10:30:17 GMT
Camel CDI: annotation type predicate


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0fb50157
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0fb50157
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0fb50157

Branch: refs/heads/master
Commit: 0fb50157c52589ed56c7ec2451d7a2984eff0c1f
Parents: 8004521
Author: Antonin Stefanutti <antonin@stefanutti.fr>
Authored: Wed Apr 20 11:23:58 2016 +0200
Committer: Antonin Stefanutti <antonin@stefanutti.fr>
Committed: Wed Apr 20 12:30:08 2016 +0200

----------------------------------------------------------------------
 .../org/apache/camel/cdi/AnnotatedDelegate.java | 22 ++++++++++++--------
 .../apache/camel/cdi/CamelContextProducer.java  |  7 ++++++-
 .../java/org/apache/camel/cdi/CdiSpiHelper.java |  7 +++++++
 .../apache/camel/cdi/SyntheticAnnotated.java    |  6 ++++--
 .../camel/cdi/SyntheticBeanAttributes.java      |  4 +++-
 5 files changed, 33 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0fb50157/components/camel-cdi/src/main/java/org/apache/camel/cdi/AnnotatedDelegate.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/AnnotatedDelegate.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/AnnotatedDelegate.java
index ccd4989..c9c9777 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/AnnotatedDelegate.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/AnnotatedDelegate.java
@@ -22,6 +22,8 @@ import java.util.HashSet;
 import java.util.Set;
 import javax.enterprise.inject.spi.Annotated;
 
+import static org.apache.camel.cdi.CdiSpiHelper.isAnnotationType;
+
 class AnnotatedDelegate implements Annotated {
 
     private final Annotated delegate;
@@ -39,13 +41,12 @@ class AnnotatedDelegate implements Annotated {
     }
 
     @Override
-    public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
-        for (Annotation annotation : annotations) {
-            if (annotation.annotationType().equals(annotationType)) {
-                return annotationType.cast(annotation);
-            }
-        }
-        return null;
+    public <T extends Annotation> T getAnnotation(Class<T> type) {
+        return annotations.stream()
+            .filter(isAnnotationType(type))
+            .findAny()
+            .map(type::cast)
+            .orElse(null);
     }
 
     @Override
@@ -64,8 +65,11 @@ class AnnotatedDelegate implements Annotated {
     }
 
     @Override
-    public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
{
-        return getAnnotation(annotationType) != null;
+    public boolean isAnnotationPresent(Class<? extends Annotation> type) {
+        return annotations.stream()
+            .filter(isAnnotationType(type))
+            .findAny()
+            .isPresent();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/0fb50157/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelContextProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelContextProducer.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelContextProducer.java
index 03602da..ac226f1 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelContextProducer.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CamelContextProducer.java
@@ -19,6 +19,7 @@ package org.apache.camel.cdi;
 import  java.beans.Introspector;
 import java.lang.annotation.Annotation;
 import java.util.Set;
+import java.util.stream.Collectors;
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.InjectionException;
 import javax.enterprise.inject.spi.Annotated;
@@ -38,6 +39,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static org.apache.camel.cdi.AnyLiteral.ANY;
+import static org.apache.camel.cdi.CdiSpiHelper.isAnnotationType;
 import static org.apache.camel.cdi.DefaultLiteral.DEFAULT;
 
 final class CamelContextProducer<T extends CamelContext> extends DelegateProducer<T>
{
@@ -77,7 +79,10 @@ final class CamelContextProducer<T extends CamelContext> extends
DelegateProduce
         }
 
         // Add event notifier if at least one observer is present
-        Set<Annotation> qualifiers = CdiSpiHelper.excludeElementOfTypes(CdiSpiHelper.getQualifiers(annotated,
manager), Named.class);
+        Set<Annotation> qualifiers = annotated.getAnnotations().stream()
+            .filter(isAnnotationType(Named.class).negate()
+                .and(q -> manager.isQualifier(q.annotationType())))
+            .collect(Collectors.toSet());
         qualifiers.add(ANY);
         if (qualifiers.size() == 1) {
             qualifiers.add(DEFAULT);

http://git-wip-us.apache.org/repos/asf/camel/blob/0fb50157/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiSpiHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiSpiHelper.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiSpiHelper.java
index 341fefa..44e2b67 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiSpiHelper.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiSpiHelper.java
@@ -29,8 +29,10 @@ import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Objects;
 import java.util.Set;
 import java.util.StringJoiner;
+import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import javax.enterprise.inject.spi.Annotated;
@@ -84,6 +86,11 @@ final class CdiSpiHelper {
         return set;
     }
 
+    static Predicate<Annotation> isAnnotationType(Class<? extends Annotation>
clazz) {
+        Objects.requireNonNull(clazz);
+        return annotation -> clazz.equals(annotation.annotationType());
+    }
+
     static Class<?> getRawType(Type type) {
         if (type instanceof Class<?>) {
             return Class.class.cast(type);

http://git-wip-us.apache.org/repos/asf/camel/blob/0fb50157/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticAnnotated.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticAnnotated.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticAnnotated.java
index 803e41c..1c7f00a 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticAnnotated.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticAnnotated.java
@@ -25,6 +25,8 @@ import java.util.HashSet;
 import java.util.Set;
 import javax.enterprise.inject.spi.Annotated;
 
+import static org.apache.camel.cdi.CdiSpiHelper.isAnnotationType;
+
 @Vetoed
 final class SyntheticAnnotated implements Annotated {
 
@@ -66,7 +68,7 @@ final class SyntheticAnnotated implements Annotated {
     @Override
     public <T extends Annotation> T getAnnotation(Class<T> type) {
         return annotations.stream()
-            .filter(a -> a.annotationType().equals(type))
+            .filter(isAnnotationType(type))
             .findAny()
             .map(type::cast)
             .orElse(null);
@@ -75,7 +77,7 @@ final class SyntheticAnnotated implements Annotated {
     @Override
     public boolean isAnnotationPresent(Class<? extends Annotation> type) {
         return annotations.stream()
-            .filter(a -> a.annotationType().equals(type))
+            .filter(isAnnotationType(type))
             .findAny()
             .isPresent();
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/0fb50157/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticBeanAttributes.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticBeanAttributes.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticBeanAttributes.java
index f59caf1..e6f407c 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticBeanAttributes.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/SyntheticBeanAttributes.java
@@ -25,6 +25,8 @@ import javax.enterprise.context.Dependent;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Named;
 
+import static org.apache.camel.cdi.CdiSpiHelper.isAnnotationType;
+
 class SyntheticBeanAttributes<T> {
 
     private final BeanManager manager;
@@ -56,7 +58,7 @@ class SyntheticBeanAttributes<T> {
 
     public String getName() {
         return annotated.getAnnotations().stream()
-            .filter(a -> Named.class.equals(a.annotationType()))
+            .filter(isAnnotationType(Named.class))
             .map(Named.class::cast)
             .map(Named::value)
             .findFirst()


Mime
View raw message