com.cvent.dropwizard.mybatis.objectFactories.ImmutablesFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dropwizard-mybatis Show documentation
Show all versions of dropwizard-mybatis Show documentation
An open source bridge layer so that you can use mybatis with dropwizard + other useful utility methods often used within an enterprise.
package com.cvent.dropwizard.mybatis.objectFactories;
import org.apache.ibatis.reflection.ReflectionException;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
/**
* Custom MyBatis Object Factory so we can handle Immutables.
*
* We only need to provide the create methods, the rest of them should be ok from
* DefaultObjectFactory.
*
* Checks for the Immutable prefix, which is default for the Immutables library.
*
* When using Immutables in MyBatis mappers, always use constructor mapping.
*
*/
public class ImmutablesFactory extends DefaultObjectFactory {
// The prefix for immutable classes
private static final String IMMUTABLE_CLASS_PREFIX = "Immutable";
// The constructor method for immutables
private static final String IMMUTABLE_CONSTRUCTOR_METHOD = "of";
private static final Logger LOGGER = LoggerFactory.getLogger(ImmutablesFactory.class);
/**
* This is the default call when not using the constructor configuration. We should never
* call this when dealing with immutable types from MyBatis.
*
* @param type
* @param
* @return
*/
public T create(Class type) {
if (type.getSimpleName().startsWith(IMMUTABLE_CLASS_PREFIX)) {
throw new RuntimeException(
"Invalid attempt to call default create in MyBatis Object Factory for " + type.getSimpleName());
}
return super.create(type);
}
/**
* This is the call MyBatis uses when constructor is used in the mapping.
*
* @param type
* @param constructorArgTypes
* @param constructorArgs
* @param
* @return
*/
public T create(Class type, List> constructorArgTypes, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy