|
| 1 | +package it.aboutbits.springboot.toolbox.persistence.javatype.base; |
| 2 | + |
| 3 | +import it.aboutbits.springboot.toolbox.type.CustomType; |
| 4 | +import lombok.SneakyThrows; |
| 5 | +import org.hibernate.type.descriptor.WrapperOptions; |
| 6 | +import org.hibernate.type.descriptor.java.AbstractClassJavaType; |
| 7 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 8 | +import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; |
| 9 | + |
| 10 | +import java.lang.reflect.Constructor; |
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | +import java.sql.Types; |
| 13 | + |
| 14 | +public abstract class WrappedEnumJavaType<T extends CustomType<? extends Enum<?>>> extends AbstractClassJavaType<T> { |
| 15 | + private final transient Constructor<T> constructor; |
| 16 | + private final Class<? extends Enum<?>> enumClass; |
| 17 | + |
| 18 | + @SuppressWarnings("unchecked") |
| 19 | + protected WrappedEnumJavaType(Class<T> type) { |
| 20 | + super(type); |
| 21 | + |
| 22 | + try { |
| 23 | + this.enumClass = determineEnumClass(type); |
| 24 | + this.constructor = type.getConstructor(enumClass); |
| 25 | + } catch (NoSuchMethodException e) { |
| 26 | + throw new IllegalStateException("No constructor found for " + type.getName() + " with enum parameter", e); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @SuppressWarnings("unchecked") |
| 31 | + private Class<? extends Enum<?>> determineEnumClass(Class<T> type) { |
| 32 | + // Try to extract from the generic superclass (if the CustomType exposes the enum via inheritance) |
| 33 | + var genericSuperclass = type.getGenericSuperclass(); |
| 34 | + if (genericSuperclass instanceof java.lang.reflect.ParameterizedType parameterizedType) { |
| 35 | + var actualTypeArguments = parameterizedType.getActualTypeArguments(); |
| 36 | + if (actualTypeArguments.length > 0 && actualTypeArguments[0] instanceof Class<?> enumType) { |
| 37 | + if (enumType.isEnum()) { |
| 38 | + return (Class<? extends Enum<?>>) enumType; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + // Fallback: inspect constructors and use the single-arg enum constructor (works for records and classes) |
| 43 | + for (var ctor : type.getDeclaredConstructors()) { |
| 44 | + var params = ctor.getParameterTypes(); |
| 45 | + if (params.length == 1 && params[0].isEnum()) { |
| 46 | + return (Class<? extends Enum<?>>) params[0]; |
| 47 | + } |
| 48 | + } |
| 49 | + throw new IllegalStateException("Could not determine enum type for " + type.getName()); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) { |
| 54 | + return indicators.getTypeConfiguration() |
| 55 | + .getJdbcTypeRegistry() |
| 56 | + .getDescriptor(Types.VARCHAR); |
| 57 | + } |
| 58 | + |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + @Override |
| 61 | + public <X> X unwrap(T id, Class<X> aClass, WrapperOptions wrapperOptions) { |
| 62 | + var javaTypeClass = getJavaTypeClass(); |
| 63 | + |
| 64 | + if (id == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + if (javaTypeClass.isAssignableFrom(aClass)) { |
| 68 | + return (X) id; |
| 69 | + } |
| 70 | + if (String.class.isAssignableFrom(aClass)) { |
| 71 | + return (X) id.value().name(); |
| 72 | + } |
| 73 | + if (enumClass.isAssignableFrom(aClass)) { |
| 74 | + return (X) id.value(); |
| 75 | + } |
| 76 | + |
| 77 | + throw unknownUnwrap(aClass); |
| 78 | + } |
| 79 | + |
| 80 | + @SuppressWarnings("unchecked") |
| 81 | + @SneakyThrows({InstantiationException.class, IllegalAccessException.class, InvocationTargetException.class}) |
| 82 | + @Override |
| 83 | + public <X> T wrap(X value, WrapperOptions wrapperOptions) { |
| 84 | + var clazz = getJavaTypeClass(); |
| 85 | + |
| 86 | + if (value == null) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + if (clazz.isInstance(value)) { |
| 90 | + return (T) value; |
| 91 | + } |
| 92 | + if (value instanceof String stringValue) { |
| 93 | + var enumValue = Enum.valueOf((Class<? extends Enum>) enumClass, stringValue); |
| 94 | + return constructor.newInstance(enumValue); |
| 95 | + } |
| 96 | + if (enumClass.isInstance(value)) { |
| 97 | + return constructor.newInstance(value); |
| 98 | + } |
| 99 | + |
| 100 | + throw unknownWrap(value.getClass()); |
| 101 | + } |
| 102 | +} |
0 commit comments