bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject [01/15] bval git commit: CDI integration updates
Date Wed, 15 Nov 2017 22:54:06 GMT
Repository: bval
Updated Branches:
  refs/heads/bv2 [created] a43c0b0c7


CDI integration updates


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

Branch: refs/heads/bv2
Commit: b21b35aeeb81eedbe0decc112c7fcefee94ba958
Parents: b53e4a0
Author: Matt Benson <mbenson@apache.org>
Authored: Wed Nov 15 15:00:48 2017 -0600
Committer: Matt Benson <mbenson@apache.org>
Committed: Wed Nov 15 16:53:42 2017 -0600

----------------------------------------------------------------------
 .../java/org/apache/bval/cdi/AnyLiteral.java    |  3 +-
 .../org/apache/bval/cdi/BValAnnotatedType.java  | 33 ++++++--------------
 .../java/org/apache/bval/cdi/BValExtension.java | 18 ++---------
 .../org/apache/bval/cdi/BValInterceptor.java    | 28 +++++++++--------
 .../apache/bval/cdi/BValInterceptorBean.java    |  6 ++--
 .../org/apache/bval/cdi/DefaultLiteral.java     |  3 +-
 .../apache/bval/cdi/EmptyAnnotationLiteral.java | 24 +++++++-------
 .../java/org/apache/bval/cdi/ValidatorBean.java |  6 ++--
 .../apache/bval/cdi/ValidatorFactoryBean.java   |  6 ++--
 9 files changed, 50 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java b/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java
index 2b62c1e..1e14f1e 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/AnyLiteral.java
@@ -19,7 +19,6 @@
 package org.apache.bval.cdi;
 
 import javax.enterprise.inject.Any;
-import javax.enterprise.util.AnnotationLiteral;
 
 public class AnyLiteral extends EmptyAnnotationLiteral<Any> implements Any {
     private static final long serialVersionUID = 1L;
@@ -31,7 +30,7 @@ public class AnyLiteral extends EmptyAnnotationLiteral<Any> implements
Any {
 
     @Override
     public String toString() {
-        return "@javax.enterprise.inject.Any()";
+        return String.format("@%s()", Any.class.getName());
     }
 
 }

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java b/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java
index a143a5b..2b08fe6 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/BValAnnotatedType.java
@@ -18,17 +18,16 @@
  */
 package org.apache.bval.cdi;
 
-import javax.enterprise.inject.spi.AnnotatedConstructor;
-import javax.enterprise.inject.spi.AnnotatedField;
-import javax.enterprise.inject.spi.AnnotatedMethod;
-import javax.enterprise.inject.spi.AnnotatedType;
-import javax.enterprise.util.AnnotationLiteral;
-
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.HashSet;
 import java.util.Set;
 
+import javax.enterprise.inject.spi.AnnotatedConstructor;
+import javax.enterprise.inject.spi.AnnotatedField;
+import javax.enterprise.inject.spi.AnnotatedMethod;
+import javax.enterprise.inject.spi.AnnotatedType;
+
 public class BValAnnotatedType<A> implements AnnotatedType<A> {
     private final AnnotatedType<A> delegate;
     private final Set<Annotation> annotations;
@@ -36,8 +35,7 @@ public class BValAnnotatedType<A> implements AnnotatedType<A>
{
     public BValAnnotatedType(final AnnotatedType<A> annotatedType) {
         delegate = annotatedType;
 
-        annotations = new HashSet<Annotation>(annotatedType.getAnnotations().size());
-        annotations.addAll(annotatedType.getAnnotations());
+        annotations = new HashSet<>(annotatedType.getAnnotations());
         annotations.add(BValBindingLiteral.INSTANCE);
     }
 
@@ -73,14 +71,8 @@ public class BValAnnotatedType<A> implements AnnotatedType<A>
{
 
     @Override
     public <T extends Annotation> T getAnnotation(final Class<T> annotationType)
{
-        for (final Annotation ann : annotations) {
-            if (ann.annotationType().equals(annotationType)) {
-                @SuppressWarnings("unchecked")
-                final T result = (T) ann;
-                return result;
-            }
-        }
-        return null;
+        return annotations.stream().filter(ann -> ann.annotationType().equals(annotationType)).map(annotationType::cast)
+            .findFirst().orElse(null);
     }
 
     @Override
@@ -90,12 +82,7 @@ public class BValAnnotatedType<A> implements AnnotatedType<A>
{
 
     @Override
     public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
{
-        for (final Annotation ann : annotations) {
-            if (ann.annotationType().equals(annotationType)) {
-                return true;
-            }
-        }
-        return false;
+        return annotations.stream().anyMatch(ann -> ann.annotationType().equals(annotationType));
     }
 
     public static class BValBindingLiteral extends EmptyAnnotationLiteral<BValBinding>
implements BValBinding {
@@ -105,7 +92,7 @@ public class BValAnnotatedType<A> implements AnnotatedType<A>
{
 
         @Override
         public String toString() {
-            return "@org.apache.bval.cdi.BValBinding()";
+            return String.format("@%s()", BValBinding.class.getName());
         }
 
     }

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java b/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
index f84074e..e10a9ba 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
@@ -59,13 +59,7 @@ import org.apache.bval.util.Validate;
 public class BValExtension implements Extension {
     private static final Logger LOGGER = Logger.getLogger(BValExtension.class.getName());
 
-    private static final AnnotatedTypeFilter DEFAULT_ANNOTATED_TYPE_FILTER = new AnnotatedTypeFilter()
{
-
-        @Override
-        public boolean accept(AnnotatedType<?> annotatedType) {
-            return !annotatedType.getJavaClass().getName().startsWith("org.apache.bval.");
-        }
-    };
+    private static final AnnotatedTypeFilter DEFAULT_ANNOTATED_TYPE_FILTER = annotatedType
-> !annotatedType.getJavaClass().getName().startsWith("org.apache.bval.");
 
     private static AnnotatedTypeFilter annotatedTypeFilter = DEFAULT_ANNOTATED_TYPE_FILTER;
 
@@ -183,8 +177,7 @@ public class BValExtension implements Extension {
                                 && !classConstraints.getConstrainedMethods(MethodType.NON_GETTER).isEmpty()
                             || validGetterMethods
                                 && !classConstraints.getConstrainedMethods(MethodType.GETTER).isEmpty()))
{
-                        final BValAnnotatedType<A> bValAnnotatedType = new BValAnnotatedType<A>(annotatedType);
-                        pat.setAnnotatedType(bValAnnotatedType);
+                        pat.setAnnotatedType(new BValAnnotatedType<A>(annotatedType));
                     }
                 } catch (final NoClassDefFoundError ncdfe) {
                     // skip
@@ -199,12 +192,7 @@ public class BValExtension implements Extension {
 
     private static <A> boolean hasValidationAnnotation(
         final Collection<? extends AnnotatedCallable<? super A>> methods) {
-        for (final AnnotatedCallable<? super A> m : methods) {
-            if (m.isAnnotationPresent(ValidateOnExecution.class)) {
-                return true;
-            }
-        }
-        return false;
+        return methods.stream().anyMatch(m -> m.isAnnotationPresent(ValidateOnExecution.class));
     }
 
     public <A> void processBean(final @Observes ProcessBean<A> processBeanEvent)
{

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptor.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptor.java b/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptor.java
index 3a7f000..7bf7709 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptor.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptor.java
@@ -18,8 +18,7 @@
  */
 package org.apache.bval.cdi;
 
-import org.apache.bval.jsr.util.ClassHelper;
-import org.apache.bval.jsr.util.Proxies;
+import static java.util.Arrays.asList;
 
 import java.io.Serializable;
 import java.lang.reflect.Constructor;
@@ -33,6 +32,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+
 import javax.annotation.Priority;
 import javax.enterprise.inject.spi.AnnotatedConstructor;
 import javax.enterprise.inject.spi.AnnotatedMethod;
@@ -53,18 +53,20 @@ import javax.validation.executable.ValidateOnExecution;
 import javax.validation.metadata.ConstructorDescriptor;
 import javax.validation.metadata.MethodDescriptor;
 
-import static java.util.Arrays.asList;
+import org.apache.bval.jsr.util.ClassHelper;
+import org.apache.bval.jsr.util.Proxies;
 
 /**
  * Interceptor class for the {@link BValBinding} {@link InterceptorBinding}.
  */
+@SuppressWarnings("serial")
 @Interceptor
 @BValBinding
 @Priority(4800)
 // TODO: maybe add it through ASM to be compliant with CDI 1.0 containers using simply this
class as a template to
 // generate another one for CDI 1.1 impl
 public class BValInterceptor implements Serializable {
-    private transient volatile Map<Method, Boolean> methodConfiguration = new ConcurrentHashMap<Method,
Boolean>();
+    private transient volatile Map<Method, Boolean> methodConfiguration = new ConcurrentHashMap<>();
     private transient volatile Set<ExecutableType> classConfiguration;
     private transient volatile Boolean constructorValidated;
 
@@ -204,7 +206,7 @@ public class BValInterceptor implements Serializable {
                 methodConfig = methodConfiguration.get(method);
                 if (methodConfig == null) {
                     final List<Class<?>> classHierarchy =
-                        ClassHelper.fillFullClassHierarchyAsList(new LinkedList<Class<?>>(),
targetClass);
+                        ClassHelper.fillFullClassHierarchyAsList(new LinkedList<>(),
targetClass);
                     Collections.reverse(classHierarchy);
 
                     // search on method @ValidateOnExecution
@@ -213,19 +215,19 @@ public class BValInterceptor implements Serializable {
                     for (final Class<?> c : classHierarchy) {
                         final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(c);
                         AnnotatedMethod<?> annotatedMethod = null;
+
                         for (final AnnotatedMethod<?> m : annotatedType.getMethods())
{
-                            if (!m.getJavaMember().getName().equals(method.getName())
-                                || !asList(method.getGenericParameterTypes())
+                            if (m.getJavaMember().getName().equals(method.getName())
+                                && asList(method.getGenericParameterTypes())
                                     .equals(asList(m.getJavaMember().getGenericParameterTypes())))
{
-                                continue;
+                                annotatedMethod = m;
+                                break;
                             }
-                            annotatedMethod = m;
-                            break;
+                        }
+                        if (annotatedMethod == null) {
+                            continue;
                         }
                         try {
-                            if (annotatedMethod == null) {
-                                continue;
-                            }
                             if (validateOnExecutionType == null) {
                                 final ValidateOnExecution vat = annotatedType.getAnnotation(ValidateOnExecution.class);
                                 if (vat != null) {

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java b/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java
index 8a0c95e..e8f7e1d 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/BValInterceptorBean.java
@@ -43,12 +43,12 @@ public class BValInterceptorBean implements Bean<BValInterceptor>,
PassivationCa
     private final InjectionTarget<BValInterceptor> injectionTarget;
 
     public BValInterceptorBean(final BeanManager bm) {
-        final Set<Type> t = new HashSet<Type>();
+        final Set<Type> t = new HashSet<>();
         t.add(BValInterceptor.class);
         t.add(Object.class);
         types = Collections.unmodifiableSet(t);
 
-        final Set<Annotation> q = new HashSet<Annotation>();
+        final Set<Annotation> q = new HashSet<>();
         q.add(DefaultLiteral.INSTANCE);
         q.add(AnyLiteral.INSTANCE);
         qualifiers = Collections.unmodifiableSet(q);
@@ -120,7 +120,7 @@ public class BValInterceptorBean implements Bean<BValInterceptor>,
PassivationCa
 
     @Override
     public String getId() {
-        return "BValInterceptor-" + hashCode();
+        return String.format("%s-%d",BValInterceptor.class.getSimpleName(), hashCode());
     }
 
     private static class BValInterceptorInjectionPoint implements InjectionPoint {

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java b/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java
index 07b2246..e86db31 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/DefaultLiteral.java
@@ -19,7 +19,6 @@
 package org.apache.bval.cdi;
 
 import javax.enterprise.inject.Default;
-import javax.enterprise.util.AnnotationLiteral;
 
 public class DefaultLiteral extends EmptyAnnotationLiteral<Default> implements Default
{
     private static final long serialVersionUID = 1L;
@@ -28,6 +27,6 @@ public class DefaultLiteral extends EmptyAnnotationLiteral<Default>
implements D
 
     @Override
     public String toString() {
-        return "@javax.enterprise.inject.Default()";
+        return String.format("@%s()", Default.class.getName());
     }
 }

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java b/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java
index 6866551..e9e9f1a 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/EmptyAnnotationLiteral.java
@@ -29,6 +29,7 @@ import javax.enterprise.util.AnnotationLiteral;
  * Taken from Apache OpenWebBeans.
  * @param <T>
  */
+@SuppressWarnings("serial")
 public abstract class EmptyAnnotationLiteral<T extends Annotation> extends AnnotationLiteral<T>
{
     private Class<T> annotationType;
 
@@ -77,11 +78,10 @@ public abstract class EmptyAnnotationLiteral<T extends Annotation>
extends Annot
     private Class<T> getAnnotationType(Class<?> definedClazz) {
         Type superClazz = definedClazz.getGenericSuperclass();
 
-        Class<T> clazz = null;
-
-        if (superClazz.equals(Object.class)) {
-            throw new RuntimeException("Super class must be parametrized type!");
-        } else if (superClazz instanceof ParameterizedType) {
+        if (Object.class.equals(superClazz)) {
+            throw new RuntimeException("Super class must be parameterized type!");
+        }
+        if (superClazz instanceof ParameterizedType) {
             ParameterizedType paramType = (ParameterizedType) superClazz;
             Type[] actualArgs = paramType.getActualTypeArguments();
 
@@ -89,17 +89,15 @@ public abstract class EmptyAnnotationLiteral<T extends Annotation>
extends Annot
                 //Actual annotation type
                 Type type = actualArgs[0];
 
-                if (type instanceof Class) {
-                    clazz = (Class<T>) type;
+                if (type instanceof Class<?>) {
+                    @SuppressWarnings("unchecked")
+                    Class<T> clazz = (Class<T>) type;
                     return clazz;
-                } else {
-                    throw new RuntimeException("Not class type!");
                 }
-            } else {
-                throw new RuntimeException("More than one parametric type!");
+                throw new RuntimeException("Not class type!");
             }
-        } else {
-            return getAnnotationType((Class<?>) superClazz);
+            throw new RuntimeException("More than one parametric type!");
         }
+        return getAnnotationType((Class<?>) superClazz);
     }
 }

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
index c551d72..cd17fae 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
@@ -44,12 +44,12 @@ public class ValidatorBean implements Bean<Validator>, PassivationCapable
{
         this.factory = factory;
         this.instance = validator;
 
-        final Set<Type> t = new HashSet<Type>();
+        final Set<Type> t = new HashSet<>();
         t.add(Validator.class);
         t.add(Object.class);
         types = Collections.unmodifiableSet(t);
 
-        final Set<Annotation> q = new HashSet<Annotation>();
+        final Set<Annotation> q = new HashSet<>();
         q.add(DefaultLiteral.INSTANCE);
         q.add(AnyLiteral.INSTANCE);
         qualifiers = Collections.unmodifiableSet(q);
@@ -119,6 +119,6 @@ public class ValidatorBean implements Bean<Validator>, PassivationCapable
{
 
     @Override
     public String getId() {
-        return "BValValidator-" + hashCode();
+        return String.format("BVal%s-%d", Validator.class.getSimpleName(), hashCode());
     }
 }

http://git-wip-us.apache.org/repos/asf/bval/blob/b21b35ae/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
index aeffd81..0a8c2e5 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
@@ -41,12 +41,12 @@ public class ValidatorFactoryBean implements Bean<ValidatorFactory>,
Passivation
     public ValidatorFactoryBean(final ValidatorFactory validatorFactory) {
         this.instance = validatorFactory;
 
-        final Set<Type> t = new HashSet<Type>();
+        final Set<Type> t = new HashSet<>();
         t.add(ValidatorFactory.class);
         t.add(Object.class);
         types = Collections.unmodifiableSet(t);
 
-        final Set<Annotation> q = new HashSet<Annotation>();
+        final Set<Annotation> q = new HashSet<>();
         q.add(DefaultLiteral.INSTANCE);
         q.add(AnyLiteral.INSTANCE);
         qualifiers = Collections.unmodifiableSet(q);
@@ -109,6 +109,6 @@ public class ValidatorFactoryBean implements Bean<ValidatorFactory>,
Passivation
 
     @Override
     public String getId() {
-        return "BValValidatorFactory-" + hashCode();
+        return String.format("BVal%s-%d", ValidatorFactory.class.getSimpleName(), hashCode());
     }
 }


Mime
View raw message