struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [2/5] struts git commit: WW-4210 Supports defining converter as a class in the annotation
Date Sat, 22 Apr 2017 15:42:24 GMT
WW-4210 Supports defining converter as a class in the annotation


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

Branch: refs/heads/master
Commit: 3e76564d3ce8d5b4a3938efd3a76c521eb045f38
Parents: 47552bd
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Thu Apr 20 08:29:24 2017 +0200
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Thu Apr 20 08:29:24 2017 +0200

----------------------------------------------------------------------
 .../xwork2/conversion/TypeConverterCreator.java |  9 +++++
 .../DefaultConversionAnnotationProcessor.java   | 36 +++++++++++++++-----
 .../impl/DefaultTypeConverterCreator.java       | 10 +++++-
 .../xwork2/conversion/impl/XWorkConverter.java  |  4 +--
 4 files changed, 48 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/3e76564d/core/src/main/java/com/opensymphony/xwork2/conversion/TypeConverterCreator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/conversion/TypeConverterCreator.java
b/core/src/main/java/com/opensymphony/xwork2/conversion/TypeConverterCreator.java
index 738bbe8..438d31c 100644
--- a/core/src/main/java/com/opensymphony/xwork2/conversion/TypeConverterCreator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/conversion/TypeConverterCreator.java
@@ -14,4 +14,13 @@ public interface TypeConverterCreator {
      */
     TypeConverter createTypeConverter(String className) throws Exception;
 
+    /**
+     * Creates {@link TypeConverter} from given class
+     *
+     * @param clazz convert class
+     * @return instance of {@link TypeConverter}
+     * @throws Exception when cannot create/cast to {@link TypeConverter}
+     */
+    TypeConverter createTypeConverter(Class<?> clazz) throws Exception;
+
 }

http://git-wip-us.apache.org/repos/asf/struts/blob/3e76564d/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultConversionAnnotationProcessor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultConversionAnnotationProcessor.java
b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultConversionAnnotationProcessor.java
index c3faae3..5396712 100644
--- a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultConversionAnnotationProcessor.java
+++ b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultConversionAnnotationProcessor.java
@@ -8,6 +8,7 @@ import com.opensymphony.xwork2.conversion.annotations.ConversionRule;
 import com.opensymphony.xwork2.conversion.annotations.ConversionType;
 import com.opensymphony.xwork2.conversion.annotations.TypeConversion;
 import com.opensymphony.xwork2.inject.Inject;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -34,28 +35,43 @@ public class DefaultConversionAnnotationProcessor implements ConversionAnnotatio
     }
 
     public void process(Map<String, Object> mapping, TypeConversion tc, String key)
{
-        LOG.debug("TypeConversion [{}] with key: [{}]", tc.converter(), key);
+        LOG.debug("TypeConversion [{}/{}] with key: [{}]", tc.converter(), tc.converterClass(),
key);
         if (key == null) {
             return;
         }
         try {
             if (tc.type() == ConversionType.APPLICATION) {
-                converterHolder.addDefaultMapping(key, converterCreator.createTypeConverter(tc.converter()));
+                if (StringUtils.isNoneEmpty(tc.converter())) {
+                    converterHolder.addDefaultMapping(key, converterCreator.createTypeConverter(tc.converter()));
+                } else {
+                    converterHolder.addDefaultMapping(key, converterCreator.createTypeConverter(tc.converterClass()));
+                }
             } else {
                 if (tc.rule() == ConversionRule.KEY_PROPERTY || tc.rule() == ConversionRule.CREATE_IF_NULL)
{
                     mapping.put(key, tc.value());
                 }
                 //for properties of classes
                 else if (tc.rule() != ConversionRule.ELEMENT || tc.rule() == ConversionRule.KEY
|| tc.rule() == ConversionRule.COLLECTION) {
-                    mapping.put(key, converterCreator.createTypeConverter(tc.converter()));
+                    if (StringUtils.isNoneEmpty(tc.converter())) {
+                        mapping.put(key, converterCreator.createTypeConverter(tc.converter()));
+                    } else {
+                        mapping.put(key, converterCreator.createTypeConverter(tc.converterClass()));
+                    }
                 }
                 //for keys of Maps
                 else if (tc.rule() == ConversionRule.KEY) {
-                    Class converterClass = Thread.currentThread().getContextClassLoader().loadClass(tc.converter());
+                    Class<?> converterClass;
+                    if (StringUtils.isNoneEmpty(tc.converter())) {
+                        converterClass = Thread.currentThread().getContextClassLoader().loadClass(tc.converter());
+                        //check if the converter is a type converter if it is one
+                        //then just put it in the map as is. Otherwise
+                        //put a value in for the type converter of the class
+                    } else {
+                        converterClass = tc.converterClass();
+                    }
+
                     LOG.debug("Converter class: [{}]", converterClass);
-                    //check if the converter is a type converter if it is one
-                    //then just put it in the map as is. Otherwise
-                    //put a value in for the type converter of the class
+
                     if (converterClass.isAssignableFrom(TypeConverter.class)) {
                         mapping.put(key, converterCreator.createTypeConverter(tc.converter()));
                     } else {
@@ -65,7 +81,11 @@ public class DefaultConversionAnnotationProcessor implements ConversionAnnotatio
                 }
                 //elements(values) of maps / lists
                 else {
-                    mapping.put(key, Thread.currentThread().getContextClassLoader().loadClass(tc.converter()));
+                    if (StringUtils.isNoneEmpty(tc.converter())) {
+                        mapping.put(key, Thread.currentThread().getContextClassLoader().loadClass(tc.converter()));
+                    } else {
+                        mapping.put(key, tc.converterClass());
+                    }
                 }
             }
         } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/struts/blob/3e76564d/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultTypeConverterCreator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultTypeConverterCreator.java
b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultTypeConverterCreator.java
index ed6ac22..d9613b2 100644
--- a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultTypeConverterCreator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultTypeConverterCreator.java
@@ -19,8 +19,16 @@ public class DefaultTypeConverterCreator implements TypeConverterCreator
{
     }
 
     public TypeConverter createTypeConverter(String className) throws Exception {
-        // type converters are used across users
         Object obj = objectFactory.buildBean(className, null);
+        return getTypeConverter(obj);
+    }
+
+    public TypeConverter createTypeConverter(Class<?> clazz) throws Exception {
+        Object obj = objectFactory.buildBean(clazz, null);
+        return getTypeConverter(obj);
+    }
+
+    protected TypeConverter getTypeConverter(Object obj) {
         if (obj instanceof TypeConverter) {
             return (TypeConverter) obj;
 

http://git-wip-us.apache.org/repos/asf/struts/blob/3e76564d/core/src/main/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java
b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java
index 4257411..0d55613 100644
--- a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java
+++ b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java
@@ -487,9 +487,9 @@ public class XWorkConverter extends DefaultTypeConverter {
                     }
                     if (LOG.isDebugEnabled()) {
                         if (StringUtils.isEmpty(tc.key())) {
-                            LOG.debug("WARNING! key of @TypeConversion [{}] applied to [{}]
is empty!", tc.converter(), clazz.getName());
+                            LOG.debug("WARNING! key of @TypeConversion [{}/{}] applied to
[{}] is empty!", tc.converter(), tc.converterClass(), clazz.getName());
                         } else {
-                            LOG.debug("TypeConversion [{}] with key: [{}]", tc.converter(),
tc.key());
+                            LOG.debug("TypeConversion [{}/{}] with key: [{}]", tc.converter(),
tc.converterClass(), tc.key());
                         }
                     }
                     annotationProcessor.process(mapping, tc, tc.key());


Mime
View raw message