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;
}
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);
}
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);
}
}
}
Aggregations