Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -31,13 +36,24 @@ public void setAdditionalTypePackages(String[] additionalTypePackages) {
log.info("CustomTypeConfiguration enabled. Scanning: {}", Arrays.toString(packageNamesToScan));
var classScanner = ClassScannerUtil.getScannerForPackages(packageNamesToScan);

this.relevantTypes = findAllCustomTypeRecords(classScanner);
this.relevantTypes = findAllCustomTypes(classScanner);
}

@SuppressWarnings("rawtypes")
public static Set<Class<? extends CustomType>> findAllCustomTypeRecords(ClassScannerUtil.ClassScanner classScanner) {
public static Set<Class<? extends CustomType>> findAllCustomTypes(ClassScannerUtil.ClassScanner classScanner) {
return classScanner.getSubTypesOf(CustomType.class).stream()
.filter(Record.class::isAssignableFrom)
.filter(item ->
!item.isInterface()
&& !item.isAnonymousClass()
&& !Modifier.isAbstract(item.getModifiers())
&& !item.isAnnotationPresent(DisableCustomTypeConfiguration.class)
)
.collect(Collectors.toSet());
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DisableCustomTypeConfiguration {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.reflection.util.CustomTypeReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;

Expand All @@ -22,7 +22,14 @@ public class CustomTypeDeserializer<T extends CustomType<?>> extends JsonDeseria
public CustomTypeDeserializer(Class<T> customType) {
this.customType = customType;

this.constructor = RecordReflectionUtil.getCanonicalConstructor(customType);
try {
this.constructor = CustomTypeReflectionUtil.getCustomTypeConstructor(customType);
} catch (NoSuchMethodException e) {
throw new CustomTypeDeserializerException(
"Unable to find constructor for type: " + customType.getName(),
e
);
}

this.typeConverter = getTypeConverter(
constructor.getParameterTypes()[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -14,12 +13,16 @@
import java.sql.Types;

public abstract class WrappedBigDecimalJavaType<T extends CustomType<BigDecimal>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedBigDecimalJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(BigDecimal.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -60,7 +63,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Double doubleValue) {
return canonicalConstructor.newInstance(BigDecimal.valueOf(doubleValue));
return constructor.newInstance(BigDecimal.valueOf(doubleValue));
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -14,12 +13,16 @@
import java.sql.Types;

public abstract class WrappedBigIntegerJavaType<T extends CustomType<BigInteger>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedBigIntegerJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(BigInteger.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -60,7 +63,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Long longValue) {
return canonicalConstructor.newInstance(BigInteger.valueOf(longValue));
return constructor.newInstance(BigInteger.valueOf(longValue));
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -13,12 +12,16 @@
import java.sql.Types;

public abstract class WrappedDoubleJavaType<T extends CustomType<Double>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedDoubleJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(Double.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -59,7 +62,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Double doubleValue) {
return canonicalConstructor.newInstance(doubleValue);
return constructor.newInstance(doubleValue);
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -13,12 +12,16 @@
import java.sql.Types;

public abstract class WrappedFloatJavaType<T extends CustomType<Float>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedFloatJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(Float.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -59,7 +62,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Float floatValue) {
return canonicalConstructor.newInstance(floatValue);
return constructor.newInstance(floatValue);
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -13,12 +12,16 @@
import java.sql.Types;

public abstract class WrappedIntegerJavaType<T extends CustomType<Integer>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedIntegerJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(Integer.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -59,7 +62,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Integer integerValue) {
return canonicalConstructor.newInstance(integerValue);
return constructor.newInstance(integerValue);
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -13,12 +12,16 @@
import java.sql.Types;

public abstract class WrappedLongJavaType<T extends CustomType<Long>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedLongJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(Long.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -59,7 +62,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Long longValue) {
return canonicalConstructor.newInstance(longValue);
return constructor.newInstance(longValue);
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import lombok.SneakyThrows;
Expand All @@ -14,12 +13,16 @@
import java.sql.Types;

public abstract class WrappedScaledBigDecimalJavaType<T extends CustomType<ScaledBigDecimal>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedScaledBigDecimalJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(ScaledBigDecimal.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -60,7 +63,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Double doubleValue) {
return canonicalConstructor.newInstance(new ScaledBigDecimal(doubleValue));
return constructor.newInstance(new ScaledBigDecimal(doubleValue));
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -13,12 +12,16 @@
import java.sql.Types;

public abstract class WrappedShortJavaType<T extends CustomType<Short>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedShortJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(Short.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -59,7 +62,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof Short shortValue) {
return canonicalConstructor.newInstance(shortValue);
return constructor.newInstance(shortValue);
}

throw unknownWrap(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.aboutbits.springboot.toolbox.persistence.javatype.base;

import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import lombok.SneakyThrows;
import org.hibernate.type.descriptor.WrapperOptions;
Expand All @@ -13,12 +12,16 @@
import java.sql.Types;

public abstract class WrappedStringJavaType<T extends CustomType<String>> extends AbstractClassJavaType<T> {
private final transient Constructor<T> canonicalConstructor;
private final transient Constructor<T> constructor;

protected WrappedStringJavaType(Class<T> type) {
super(type);

this.canonicalConstructor = RecordReflectionUtil.getCanonicalConstructor(type);
try {
this.constructor = type.getConstructor(String.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No constructor found for " + type.getName(), e);
}
}

@Override
Expand Down Expand Up @@ -59,7 +62,7 @@ public <X> T wrap(X value, WrapperOptions wrapperOptions) {
return (T) value;
}
if (value instanceof String stringValue) {
return canonicalConstructor.newInstance(stringValue);
return constructor.newInstance(stringValue);
}

throw unknownWrap(value.getClass());
Expand Down
Loading