You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2019/12/03 16:49:42 UTC

[myfaces] branch master updated: MYFACES-4311 ustom Converters Do Not Support Generics

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

tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/master by this push:
     new c9efdc8  MYFACES-4311 ustom Converters Do Not Support Generics
c9efdc8 is described below

commit c9efdc82bbc8dfb8a33857afedfa0a41d92996e4
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Tue Dec 3 17:49:34 2019 +0100

    MYFACES-4311 ustom Converters Do Not Support Generics
---
 .../java/org/apache/myfaces/cdi/util/CDIUtils.java | 15 +++++++++++
 .../cdi/wrapper/FacesConverterCDIWrapper.java      | 30 +++++++++++++++++-----
 .../cdi/wrapper/FacesValidatorCDIWrapper.java      | 19 +++++++++++---
 3 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/cdi/util/CDIUtils.java b/impl/src/main/java/org/apache/myfaces/cdi/util/CDIUtils.java
index e22387b..934d068 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/util/CDIUtils.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/util/CDIUtils.java
@@ -99,6 +99,21 @@ public class CDIUtils
         }
     }
 
+    public static <T> T get(BeanManager beanManager, Type type, boolean create, Annotation... qualifiers)
+    {
+        try
+        {
+            Set<Bean<?>> beans = beanManager.getBeans(type, qualifiers);
+            Bean<T> bean = (Bean<T>) beanManager.resolve(beans);
+
+            return (bean != null) ? get(beanManager, bean, type, create) : null;
+        }
+        catch (ContextNotActiveException e)
+        {
+            return null;
+        }
+    }
+
     public static <T> T get(BeanManager beanManager, Bean<T> bean, Type type, boolean create)
     {
         if (create)
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesConverterCDIWrapper.java b/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesConverterCDIWrapper.java
index 9a2fa23..2183080 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesConverterCDIWrapper.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesConverterCDIWrapper.java
@@ -19,6 +19,9 @@
 
 package org.apache.myfaces.cdi.wrapper;
 
+import java.lang.reflect.Type;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.util.TypeLiteral;
 import javax.faces.FacesWrapper;
 import javax.faces.component.PartialStateHolder;
 import javax.faces.component.UIComponent;
@@ -32,6 +35,10 @@ import org.apache.myfaces.cdi.util.CDIUtils;
  */
 public class FacesConverterCDIWrapper implements PartialStateHolder, Converter, FacesWrapper<Converter>
 {
+    private static final Type CONVERTER_TYPE = new TypeLiteral<Converter<?>>() { 
+        private static final long serialVersionUID = 1L; 
+    }.getType(); 
+
     private transient Converter delegate;
 
     private Class<?> forClass;
@@ -65,17 +72,28 @@ public class FacesConverterCDIWrapper implements PartialStateHolder, Converter,
     {
         if (delegate == null)
         {
+            BeanManager bm = CDIUtils.getBeanManager(FacesContext.getCurrentInstance().getExternalContext());
+
             if (converterId != null)
             {
-                delegate = (Converter) CDIUtils.get(CDIUtils.getBeanManager(
-                    FacesContext.getCurrentInstance().getExternalContext()), 
-                        Converter.class, true, new FacesConverterAnnotationLiteral(Object.class, converterId));
+                FacesConverterAnnotationLiteral literal =
+                        new FacesConverterAnnotationLiteral(Object.class, converterId);
+                delegate = (Converter) CDIUtils.get(bm, CONVERTER_TYPE, true, literal);
+
+                if (delegate == null)
+                {
+                    delegate = (Converter) CDIUtils.get(bm, Converter.class, true, literal);
+                }
             }
             else if (forClass != null)
             {
-                delegate = (Converter) CDIUtils.get(CDIUtils.getBeanManager(
-                    FacesContext.getCurrentInstance().getExternalContext()), 
-                        Converter.class, true, new FacesConverterAnnotationLiteral(forClass, ""));
+                FacesConverterAnnotationLiteral literal = new FacesConverterAnnotationLiteral(forClass, "");
+                delegate = (Converter) CDIUtils.get(bm, CONVERTER_TYPE, true, literal);
+
+                if (delegate == null)
+                {
+                    delegate = (Converter) CDIUtils.get(bm, Converter.class, true, literal);
+                }
             }
         }
         return delegate;
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesValidatorCDIWrapper.java b/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesValidatorCDIWrapper.java
index 9a959b3..5f9b653 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesValidatorCDIWrapper.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/wrapper/FacesValidatorCDIWrapper.java
@@ -19,6 +19,9 @@
 
 package org.apache.myfaces.cdi.wrapper;
 
+import java.lang.reflect.Type;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.util.TypeLiteral;
 import javax.faces.FacesWrapper;
 import javax.faces.component.PartialStateHolder;
 import javax.faces.component.UIComponent;
@@ -29,6 +32,11 @@ import org.apache.myfaces.cdi.util.CDIUtils;
 
 public class FacesValidatorCDIWrapper implements PartialStateHolder, Validator, FacesWrapper<Validator>
 {
+    private static final Type VALIDATOR_TYPE = new TypeLiteral<Validator<?>>() 
+    { 
+        private static final long serialVersionUID = 1L; 
+    }.getType();
+
     private transient Validator delegate;
     
     private String validatorId;
@@ -55,9 +63,14 @@ public class FacesValidatorCDIWrapper implements PartialStateHolder, Validator,
     {
         if (delegate == null)
         {
-            delegate = (Validator) CDIUtils.get(CDIUtils.getBeanManager(
-                FacesContext.getCurrentInstance().getExternalContext()), 
-                    Validator.class, true, new FacesValidatorAnnotationLiteral(validatorId));
+            BeanManager bm = CDIUtils.getBeanManager(FacesContext.getCurrentInstance().getExternalContext());
+            FacesValidatorAnnotationLiteral literal = new FacesValidatorAnnotationLiteral(validatorId);
+            delegate = (Validator) CDIUtils.get(bm, VALIDATOR_TYPE, true, literal);
+            
+            if (delegate == null)
+            {
+                delegate = (Validator) CDIUtils.get(bm, Validator.class, true, literal);
+            }
         }
         return delegate;
     }