Red Hat Application Migration Toolkit
package org.dozer.factory;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.dozer.BeanFactory;
import org.dozer.MappingException;
import org.dozer.config.BeanContainer;
import org.dozer.factory.BeanCreationDirective;
import org.dozer.factory.BeanCreationStrategy;
import org.dozer.factory.XMLBeanFactory;
import org.dozer.util.DozerClassLoader;
import org.dozer.util.MappingUtils;
import org.dozer.util.ReflectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ConstructionStrategies {
private static final BeanCreationStrategy byCreateMethod = new ConstructionStrategies.ByCreateMethod();
private static final BeanCreationStrategy byGetInstance = new ConstructionStrategies.ByGetInstance();
private static final BeanCreationStrategy byInterface = new ConstructionStrategies.ByInterface();
private static final BeanCreationStrategy xmlBeansBased = new ConstructionStrategies.XMLBeansBased();
private static final BeanCreationStrategy constructorBased = new ConstructionStrategies.ByConstructor();
private static final ConstructionStrategies.ByFactory byFactory = new ConstructionStrategies.ByFactory();
private static final BeanCreationStrategy xmlGregorianCalendar = new ConstructionStrategies.XmlGregorian();
public static BeanCreationStrategy byCreateMethod() {
return byCreateMethod;
}
public static BeanCreationStrategy byGetInstance() {
return byGetInstance;
}
public static BeanCreationStrategy byInterface() {
return byInterface;
}
public static BeanCreationStrategy xmlBeansBased() {
return xmlBeansBased;
}
public static BeanCreationStrategy byConstructor() {
return constructorBased;
}
public static ConstructionStrategies.ByFactory byFactory() {
return byFactory;
}
public static BeanCreationStrategy xmlGregorianCalendar() {
return xmlGregorianCalendar;
}
// $FF: synthetic class
static class SyntheticClass_1 {
}
private static class XmlGregorian implements BeanCreationStrategy {
private XmlGregorian() {
}
public boolean isApplicable(BeanCreationDirective directive) {
Class actualClass = directive.getActualClass();
return XMLGregorianCalendar.class.isAssignableFrom(actualClass);
}
public Object create(BeanCreationDirective directive) {
DatatypeFactory dataTypeFactory;
try {
dataTypeFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException var4) {
throw new MappingException(var4);
}
return dataTypeFactory.newXMLGregorianCalendar();
}
// $FF: synthetic method
XmlGregorian(ConstructionStrategies.SyntheticClass_1 x0) {
this();
}
}
static class ByConstructor implements BeanCreationStrategy {
public boolean isApplicable(BeanCreationDirective directive) {
return true;
}
public Object create(BeanCreationDirective directive) {
Class classToCreate = directive.getActualClass();
try {
return newInstance(classToCreate);
} catch (Exception var4) {
if(directive.getAlternateClass() != null) {
return newInstance(directive.getAlternateClass());
} else {
MappingUtils.throwMappingException(var4);
return null;
}
}
}
private static Object newInstance(Class clazz) {
Constructor constructor = null;
try {
constructor = clazz.getDeclaredConstructor((Class[])null);
} catch (SecurityException var8) {
MappingUtils.throwMappingException(var8);
} catch (NoSuchMethodException var9) {
MappingUtils.throwMappingException(var9);
}
if(constructor == null) {
MappingUtils.throwMappingException("Could not create a new instance of the dest object: " + clazz + ". Could not find a no-arg constructor for this class.");
}
if(!constructor.isAccessible()) {
constructor.setAccessible(true);
}
Object result = null;
try {
result = constructor.newInstance((Object[])null);
} catch (IllegalArgumentException var4) {
MappingUtils.throwMappingException(var4);
} catch (InstantiationException var5) {
MappingUtils.throwMappingException(var5);
} catch (IllegalAccessException var6) {
MappingUtils.throwMappingException(var6);
} catch (InvocationTargetException var7) {
MappingUtils.throwMappingException(var7);
}
return result;
}
}
static class XMLBeansBased implements BeanCreationStrategy {
final BeanFactory xmlBeanFactory;
boolean xmlBeansAvailable;
private Class xmlObjectType;
XMLBeansBased() {
this(new XMLBeanFactory());
}
XMLBeansBased(XMLBeanFactory xmlBeanFactory) {
this.xmlBeanFactory = xmlBeanFactory;
try {
this.xmlObjectType = Class.forName("org.apache.xmlbeans.XmlObject");
this.xmlBeansAvailable = true;
} catch (ClassNotFoundException var3) {
this.xmlBeansAvailable = false;
}
}
public boolean isApplicable(BeanCreationDirective directive) {
if(!this.xmlBeansAvailable) {
return false;
} else {
Class actualClass = directive.getActualClass();
return this.xmlObjectType.isAssignableFrom(actualClass);
}
}
public Object create(BeanCreationDirective directive) {
Class classToCreate = directive.getActualClass();
String factoryBeanId = directive.getFactoryId();
String beanId = !MappingUtils.isBlankOrNull(factoryBeanId)?factoryBeanId:classToCreate.getName();
return this.xmlBeanFactory.createBean(directive.getSrcObject(), directive.getSrcClass(), beanId);
}
}
static class ByInterface implements BeanCreationStrategy {
public boolean isApplicable(BeanCreationDirective directive) {
Class actualClass = directive.getActualClass();
return Map.class.equals(actualClass) || List.class.equals(actualClass) || Set.class.equals(actualClass);
}
public Object create(BeanCreationDirective directive) {
Class actualClass = directive.getActualClass();
if(Map.class.equals(actualClass)) {
return new HashMap();
} else if(List.class.equals(actualClass)) {
return new ArrayList();
} else if(Set.class.equals(actualClass)) {
return new HashSet();
} else {
throw new IllegalStateException("Type not expected : " + actualClass);
}
}
}
static class ByFactory implements BeanCreationStrategy {
private static final Logger log = LoggerFactory.getLogger(ConstructionStrategies.ByFactory.class);
private final ConcurrentMap factoryCache = new ConcurrentHashMap();
public boolean isApplicable(BeanCreationDirective directive) {
String factoryName = directive.getFactoryName();
return !MappingUtils.isBlankOrNull(factoryName);
}
public Object create(BeanCreationDirective directive) {
Class classToCreate = directive.getActualClass();
String factoryName = directive.getFactoryName();
String factoryBeanId = directive.getFactoryId();
String beanId = !MappingUtils.isBlankOrNull(factoryBeanId)?factoryBeanId:classToCreate.getName();
BeanFactory factory = (BeanFactory)this.factoryCache.get(factoryName);
if(factory == null) {
Class result = MappingUtils.loadClass(factoryName);
if(!BeanFactory.class.isAssignableFrom(result)) {
MappingUtils.throwMappingException("Custom bean factory must implement " + BeanFactory.class.getName() + " interface : " + result);
}
factory = (BeanFactory)ReflectionUtils.newInstance(result);
this.factoryCache.put(factoryName, factory);
}
Object result1 = factory.createBean(directive.getSrcObject(), directive.getSrcClass(), beanId);
log.debug("Bean instance created with custom factory -->\n Bean Type: {}\n Factory Name: {}", result1.getClass().getName(), factoryName);
if(!classToCreate.isAssignableFrom(result1.getClass())) {
MappingUtils.throwMappingException("Custom bean factory (" + factory.getClass() + ") did not return correct type of destination data object. Expected : " + classToCreate + ", Actual : " + result1.getClass());
}
return result1;
}
public void setStoredFactories(Map factories) {
this.factoryCache.putAll(factories);
}
}
static class ByGetInstance extends ConstructionStrategies.ByCreateMethod {
public boolean isApplicable(BeanCreationDirective directive) {
Class actualClass = directive.getActualClass();
return Calendar.class.isAssignableFrom(actualClass) || DateFormat.class.isAssignableFrom(actualClass);
}
public Object create(BeanCreationDirective directive) {
directive.setCreateMethod("getInstance");
return super.create(directive);
}
}
static class ByCreateMethod implements BeanCreationStrategy {
public boolean isApplicable(BeanCreationDirective directive) {
String createMethod = directive.getCreateMethod();
return !MappingUtils.isBlankOrNull(createMethod);
}
public Object create(BeanCreationDirective directive) {
Class actualClass = directive.getActualClass();
String createMethod = directive.getCreateMethod();
Method method;
if(createMethod.contains(".")) {
String methodName = createMethod.substring(createMethod.lastIndexOf(".") + 1, createMethod.length());
String typeName = createMethod.substring(0, createMethod.lastIndexOf("."));
DozerClassLoader loader = BeanContainer.getInstance().getClassLoader();
Class type = loader.loadClass(typeName);
method = this.findMethod(type, methodName);
} else {
method = this.findMethod(actualClass, createMethod);
}
return ReflectionUtils.invoke(method, (Object)null, (Object[])null);
}
private Method findMethod(Class actualClass, String createMethod) {
Method method = null;
try {
method = ReflectionUtils.getMethod(actualClass, createMethod, (Class[])null);
} catch (NoSuchMethodException var5) {
MappingUtils.throwMappingException(var5);
}
return method;
}
}
}