Search in sources :

Example 1 with MappingException

use of org.jooq.exception.MappingException in project jOOQ by jOOQ.

the class DefaultBinding method typeMap.

@SuppressWarnings("unchecked")
static final Map<String, Class<?>> typeMap(Class<?> type, Configuration configuration, Map<String, Class<?>> result) {
    try {
        if (UDTRecord.class.isAssignableFrom(type)) {
            Class<UDTRecord<?>> t = (Class<UDTRecord<?>>) type;
            result.put(getMappedUDTName(configuration, t), t);
            UDTRecord<?> r = t.newInstance();
            for (Field<?> field : r.getUDT().fields()) typeMap(field.getType(), configuration, result);
        }
    } catch (Exception e) {
        throw new MappingException("Error while collecting type map", e);
    }
    return result;
}
Also used : UDTRecord(org.jooq.UDTRecord) DataTypeException(org.jooq.exception.DataTypeException) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) MappingException(org.jooq.exception.MappingException) SQLException(java.sql.SQLException) MappingException(org.jooq.exception.MappingException)

Example 2 with MappingException

use of org.jooq.exception.MappingException in project jOOQ by jOOQ.

the class DefaultRecordMapper method init.

private final void init(E instance) {
    // Arrays can be mapped easily
    if (type.isArray()) {
        delegate = new ArrayMapper(instance);
        return;
    }
    if (Stream.class.isAssignableFrom(type)) {
        DefaultRecordMapper<R, Object[]> local = new DefaultRecordMapper<>(rowType, Object[].class, configuration);
        delegate = r -> (E) Stream.of(local.map(r));
        return;
    }
    //                 types for convenience
    if (type.isPrimitive() || DefaultDataType.types().contains(type) || Enum.class.isAssignableFrom(type)) {
        delegate = new ValueTypeMapper();
        return;
    }
    // [#1470] Return a proxy if the supplied type is an interface
    if (Modifier.isAbstract(type.getModifiers())) {
        delegate = new ProxyMapper();
        return;
    }
    // [#2989] [#2836] Records are mapped
    if (AbstractRecord.class.isAssignableFrom(type)) {
        delegate = (RecordMapper<R, E>) new RecordToRecordMapper();
        return;
    }
    // [#1340] Allow for using non-public default constructors
    try {
        delegate = new MutablePOJOMapper(type.getDeclaredConstructor(), instance);
        return;
    } catch (NoSuchMethodException ignore) {
    }
    // [#1336] If no default constructor is present, check if there is a
    // "matching" constructor with the same number of fields as this record
    Constructor<E>[] constructors = (Constructor<E>[]) type.getDeclaredConstructors();
    // arguments
    for (Constructor<E> constructor : constructors) {
        ConstructorProperties properties = constructor.getAnnotation(ConstructorProperties.class);
        if (properties != null) {
            delegate = new ImmutablePOJOMapperWithConstructorProperties(constructor, properties);
            return;
        }
    }
    // argument length
    for (Constructor<E> constructor : constructors) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        // Match the first constructor by parameter length
        if (parameterTypes.length == fields.length) {
            delegate = new ImmutablePOJOMapper(constructor, parameterTypes);
            return;
        }
    }
    throw new MappingException("No matching constructor found on type " + type + " for record " + this);
}
Also used : ConstructorProperties(java.beans.ConstructorProperties) Constructor(java.lang.reflect.Constructor) MappingException(org.jooq.exception.MappingException)

Example 3 with MappingException

use of org.jooq.exception.MappingException in project jOOQ by jOOQ.

the class Tools method enums.

static <E extends EnumType> EnumType[] enums(Class<? extends E> type) {
    // Java implementation
    if (Enum.class.isAssignableFrom(type)) {
        return type.getEnumConstants();
    } else // [#4427] Scala implementation
    {
        try {
            // There's probably a better way to do this:
            // http://stackoverflow.com/q/36068089/521799
            Class<?> companionClass = Thread.currentThread().getContextClassLoader().loadClass(type.getName() + "$");
            java.lang.reflect.Field module = companionClass.getField("MODULE$");
            Object companion = module.get(companionClass);
            return (EnumType[]) companionClass.getMethod("values").invoke(companion);
        } catch (Exception e) {
            throw new MappingException("Error while looking up Scala enum", e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) MappingException(org.jooq.exception.MappingException) DataAccessException(org.jooq.exception.DataAccessException) TooManyRowsException(org.jooq.exception.TooManyRowsException) MappingException(org.jooq.exception.MappingException)

Aggregations

MappingException (org.jooq.exception.MappingException)3 SQLException (java.sql.SQLException)2 ConstructorProperties (java.beans.ConstructorProperties)1 Constructor (java.lang.reflect.Constructor)1 UDTRecord (org.jooq.UDTRecord)1 DataAccessException (org.jooq.exception.DataAccessException)1 DataTypeException (org.jooq.exception.DataTypeException)1 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1 TooManyRowsException (org.jooq.exception.TooManyRowsException)1