Skip to content

Commit

Permalink
fix raw types
Browse files Browse the repository at this point in the history
  • Loading branch information
SirCotare committed Sep 27, 2024
1 parent 389cf69 commit cc4d535
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,12 +13,11 @@
import java.util.List;
import java.util.Optional;

@SuppressWarnings("rawtypes")
public final class QueryTransformer<T> {

private final EntityManager entityManager;
private final TupleTransformer<T> tupleTransformer;
private org.hibernate.query.Query unwrappedQuery;
private org.hibernate.query.Query<?> unwrappedQuery;
private boolean isNative = false;

private QueryTransformer(EntityManager entityManager, Class<T> outputClass) {
Expand Down Expand Up @@ -71,7 +69,7 @@ public T asSingleResultOrFail() {
@SuppressWarnings({"deprecation", "unchecked"})
private List<T> asList(Integer pageNumber, Integer pageSize) {
unwrappedQuery.setResultTransformer(
(ResultTransformer) (objects, aliases) -> tupleTransformer.transform(objects)
(ResultTransformer<?>) (objects, aliases) -> tupleTransformer.transform(objects)
);

if (pageSize != null && pageNumber != null) {
Expand All @@ -80,10 +78,9 @@ private List<T> asList(Integer pageNumber, Integer pageSize) {
.setFirstResult(pageSize * pageNumber);
}

return unwrappedQuery.getResultList();
return (List<T>) unwrappedQuery.getResultList();
}

@SneakyThrows
private Page<T> asPageQuery(int pageNumber, int pageSize) {
var selectPattern = "(?i)select.*?[ \\t]*from ";
var queryString = unwrappedQuery.getQueryString().trim().replaceAll("\\R", " ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import java.time.ZoneId;
import java.util.Arrays;

@SuppressWarnings("rawtypes")
public class TupleTransformer<T> {
private final Class<T> outputClass;
private Constructor<T> outputClassConstructor = null;
private Class[] outputClassFieldClasses = null;
private Class<?>[] outputClassFieldClasses = null;

private final Mode mode;

Expand Down Expand Up @@ -60,18 +59,6 @@ public TupleTransformer(Class<T> outputClass) {
}
}

private static <T> boolean isSimpleType(Class<T> 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 {
Expand Down Expand Up @@ -134,17 +121,17 @@ 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;
}

// 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<? extends CustomType<?>>) outputClassFieldClasses[i]);
continue;
}

Expand Down Expand Up @@ -177,7 +164,19 @@ public T transform(Object[] objects) {
}
}

private <X extends CustomType<?>> X toCustomType(
private static <T> boolean isSimpleType(Class<T> 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 extends CustomType<?>> X toCustomType(
Object actualValue,
Class<X> targetType
) throws InvocationTargetException, InstantiationException, IllegalAccessException {
Expand Down

0 comments on commit cc4d535

Please sign in to comment.