Search in sources :

Example 1 with ReflectionException

use of org.apache.ibatis.reflection.ReflectionException in project mybatis-3 by mybatis.

the class DefaultObjectFactory method instantiateClass.

<T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    try {
        Constructor<T> constructor;
        if (constructorArgTypes == null || constructorArgs == null) {
            constructor = type.getDeclaredConstructor();
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            return constructor.newInstance();
        }
        constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
    } catch (Exception e) {
        StringBuilder argTypes = new StringBuilder();
        if (constructorArgTypes != null && !constructorArgTypes.isEmpty()) {
            for (Class<?> argType : constructorArgTypes) {
                argTypes.append(argType.getSimpleName());
                argTypes.append(",");
            }
            // remove trailing ,
            argTypes.deleteCharAt(argTypes.length() - 1);
        }
        StringBuilder argValues = new StringBuilder();
        if (constructorArgs != null && !constructorArgs.isEmpty()) {
            for (Object argValue : constructorArgs) {
                argValues.append(String.valueOf(argValue));
                argValues.append(",");
            }
            // remove trailing ,
            argValues.deleteCharAt(argValues.length() - 1);
        }
        throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
    }
}
Also used : ReflectionException(org.apache.ibatis.reflection.ReflectionException) ReflectionException(org.apache.ibatis.reflection.ReflectionException)

Example 2 with ReflectionException

use of org.apache.ibatis.reflection.ReflectionException in project mybatis-3 by mybatis.

the class BeanWrapper method instantiatePropertyValue.

@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
    MetaObject metaValue;
    Class<?> type = getSetterType(prop.getName());
    try {
        Object newObject = objectFactory.create(type);
        metaValue = MetaObject.forObject(newObject, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(), metaObject.getReflectorFactory());
        set(prop, newObject);
    } catch (Exception e) {
        throw new ReflectionException("Cannot set value of property '" + name + "' because '" + name + "' is null and cannot be instantiated on instance of " + type.getName() + ". Cause:" + e.toString(), e);
    }
    return metaValue;
}
Also used : ReflectionException(org.apache.ibatis.reflection.ReflectionException) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) ReflectionException(org.apache.ibatis.reflection.ReflectionException)

Example 3 with ReflectionException

use of org.apache.ibatis.reflection.ReflectionException in project mybatis-3 by mybatis.

the class BeanWrapper method setBeanProperty.

private void setBeanProperty(PropertyTokenizer prop, Object object, Object value) {
    try {
        Invoker method = metaClass.getSetInvoker(prop.getName());
        Object[] params = { value };
        try {
            method.invoke(object, params);
        } catch (Throwable t) {
            throw ExceptionUtil.unwrapThrowable(t);
        }
    } catch (Throwable t) {
        throw new ReflectionException("Could not set property '" + prop.getName() + "' of '" + object.getClass() + "' with value '" + value + "' Cause: " + t.toString(), t);
    }
}
Also used : ReflectionException(org.apache.ibatis.reflection.ReflectionException) Invoker(org.apache.ibatis.reflection.invoker.Invoker) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 4 with ReflectionException

use of org.apache.ibatis.reflection.ReflectionException in project mybatis-3 by mybatis.

the class CustomObjectFactory method instantiateClass.

private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    try {
        Constructor<T> constructor;
        if (constructorArgTypes == null || constructorArgs == null) {
            constructor = type.getDeclaredConstructor();
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            return constructor.newInstance();
        }
        constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
    } catch (Exception e) {
        StringBuilder argTypes = new StringBuilder();
        if (constructorArgTypes != null) {
            for (Class<?> argType : constructorArgTypes) {
                argTypes.append(argType.getSimpleName());
                argTypes.append(",");
            }
        }
        StringBuilder argValues = new StringBuilder();
        if (constructorArgs != null) {
            for (Object argValue : constructorArgs) {
                argValues.append(String.valueOf(argValue));
                argValues.append(",");
            }
        }
        throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
    }
}
Also used : ReflectionException(org.apache.ibatis.reflection.ReflectionException) ReflectionException(org.apache.ibatis.reflection.ReflectionException)

Aggregations

ReflectionException (org.apache.ibatis.reflection.ReflectionException)4 MetaObject (org.apache.ibatis.reflection.MetaObject)2 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)2 Invoker (org.apache.ibatis.reflection.invoker.Invoker)1