From cc4d53545865b05c84adda297ed6eb62a15eadae Mon Sep 17 00:00:00 2001 From: Andreas Hufler Date: Fri, 27 Sep 2024 16:18:16 +0200 Subject: [PATCH] fix raw types --- .../transformer/QueryTransformer.java | 9 ++--- .../transformer/TupleTransformer.java | 35 +++++++++---------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/QueryTransformer.java b/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/QueryTransformer.java index 037a91a..1e6994f 100644 --- a/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/QueryTransformer.java +++ b/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/QueryTransformer.java @@ -4,7 +4,6 @@ import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.Query; import jakarta.persistence.TypedQuery; -import lombok.SneakyThrows; import org.hibernate.query.NativeQuery; import org.hibernate.transform.ResultTransformer; import org.springframework.data.domain.Page; @@ -14,12 +13,11 @@ import java.util.List; import java.util.Optional; -@SuppressWarnings("rawtypes") public final class QueryTransformer { private final EntityManager entityManager; private final TupleTransformer tupleTransformer; - private org.hibernate.query.Query unwrappedQuery; + private org.hibernate.query.Query unwrappedQuery; private boolean isNative = false; private QueryTransformer(EntityManager entityManager, Class outputClass) { @@ -71,7 +69,7 @@ public T asSingleResultOrFail() { @SuppressWarnings({"deprecation", "unchecked"}) private List asList(Integer pageNumber, Integer pageSize) { unwrappedQuery.setResultTransformer( - (ResultTransformer) (objects, aliases) -> tupleTransformer.transform(objects) + (ResultTransformer) (objects, aliases) -> tupleTransformer.transform(objects) ); if (pageSize != null && pageNumber != null) { @@ -80,10 +78,9 @@ private List asList(Integer pageNumber, Integer pageSize) { .setFirstResult(pageSize * pageNumber); } - return unwrappedQuery.getResultList(); + return (List) unwrappedQuery.getResultList(); } - @SneakyThrows private Page asPageQuery(int pageNumber, int pageSize) { var selectPattern = "(?i)select.*?[ \\t]*from "; var queryString = unwrappedQuery.getQueryString().trim().replaceAll("\\R", " "); diff --git a/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/TupleTransformer.java b/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/TupleTransformer.java index 21b92d0..865993a 100644 --- a/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/TupleTransformer.java +++ b/src/main/java/it/aboutbits/springboot/toolbox/persistence/transformer/TupleTransformer.java @@ -12,11 +12,10 @@ import java.time.ZoneId; import java.util.Arrays; -@SuppressWarnings("rawtypes") public class TupleTransformer { private final Class outputClass; private Constructor outputClassConstructor = null; - private Class[] outputClassFieldClasses = null; + private Class[] outputClassFieldClasses = null; private final Mode mode; @@ -60,18 +59,6 @@ public TupleTransformer(Class outputClass) { } } - private static boolean isSimpleType(Class outputClass) { - return String.class.isAssignableFrom(outputClass) - || Float.class.isAssignableFrom(outputClass) - || Double.class.isAssignableFrom(outputClass) - || Short.class.isAssignableFrom(outputClass) - || Integer.class.isAssignableFrom(outputClass) - || Long.class.isAssignableFrom(outputClass) - || Character.class.isAssignableFrom(outputClass) - || Byte.class.isAssignableFrom(outputClass) - || Boolean.class.isAssignableFrom(outputClass); - } - @SuppressWarnings("unchecked") public T transform(Object[] objects) { try { @@ -134,9 +121,9 @@ public T transform(Object[] objects) { } // Converter: Instant to OffsetDateTime - if (objects[i] instanceof Instant && outputClassFieldClasses[i].isAssignableFrom(OffsetDateTime.class)) { + if (objects[i] instanceof Instant instant && outputClassFieldClasses[i].isAssignableFrom(OffsetDateTime.class)) { objects[i] = OffsetDateTime.ofInstant( - (Instant) objects[i], + instant, ZoneId.systemDefault() ); continue; @@ -144,7 +131,7 @@ public T transform(Object[] objects) { // Converter: to Records that wrap exactly one value (CustomType) if (CustomType.class.isAssignableFrom(outputClassFieldClasses[i])) { - objects[i] = toCustomType(objects[i], outputClassFieldClasses[i]); + objects[i] = toCustomType(objects[i], (Class>) outputClassFieldClasses[i]); continue; } @@ -177,7 +164,19 @@ public T transform(Object[] objects) { } } - private > X toCustomType( + private static boolean isSimpleType(Class outputClass) { + return String.class.isAssignableFrom(outputClass) + || Float.class.isAssignableFrom(outputClass) + || Double.class.isAssignableFrom(outputClass) + || Short.class.isAssignableFrom(outputClass) + || Integer.class.isAssignableFrom(outputClass) + || Long.class.isAssignableFrom(outputClass) + || Character.class.isAssignableFrom(outputClass) + || Byte.class.isAssignableFrom(outputClass) + || Boolean.class.isAssignableFrom(outputClass); + } + + private static > X toCustomType( Object actualValue, Class targetType ) throws InvocationTargetException, InstantiationException, IllegalAccessException {