mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4210 Supports defining converter as a class in the annotation
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
+28
-8
@@ -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) {
|
||||
|
||||
+9
-1
@@ -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;
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user