Skip to content

Commit c876759

Browse files
committed
implement review feedback
1 parent 4d7f568 commit c876759

3 files changed

Lines changed: 23 additions & 32 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/parameter/SortParameter.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,23 @@ private static String convertToString(Object value) {
263263
return string;
264264
}
265265

266-
// Use reflection to get the column name from a mapped jOOQ Field<?> using the Field<?>.getName() method
266+
// Use reflection to get the column name from a jOOQ Field<?> using the Field<?>.getName() method
267267
// This allows us to have jOOQ as an optional dependency and not break existing projects that use Hibernate
268268
try {
269-
var getName = findAccessibleGetNameMethod(value.getClass());
270-
if (getName == null) {
271-
getName = value.getClass().getMethod("getName");
272-
getName.setAccessible(true);
269+
var fieldClass = Class.forName("org.jooq.Field");
270+
if (!fieldClass.isInstance(value)) {
271+
throw new IllegalArgumentException(
272+
"Value must be either a String or org.jooq.Field, but was: " + value.getClass().getName()
273+
);
273274
}
274275

276+
var getName = fieldClass.getMethod("getName");
275277
return (String) getName.invoke(value);
278+
} catch (ClassNotFoundException e) {
279+
throw new IllegalArgumentException(
280+
"jOOQ library not found. Cannot process Field<?> value. Make sure jOOQ is on the classpath.",
281+
e
282+
);
276283
} catch (Exception e) {
277284
throw new IllegalArgumentException(
278285
"Cannot get the jOOQ Field name from the mapped SortMappings$Mapping column value [value.class.name=%s]".formatted(
@@ -283,33 +290,6 @@ private static String convertToString(Object value) {
283290
}
284291
}
285292

286-
private static @Nullable Method findAccessibleGetNameMethod(Class<?> clazz) {
287-
if (Modifier.isPublic(clazz.getModifiers())) {
288-
try {
289-
var method = clazz.getDeclaredMethod("getName");
290-
if (Modifier.isPublic(method.getModifiers())) {
291-
return method;
292-
}
293-
} catch (NoSuchMethodException _) {
294-
// ignored, let's search in the interfaces or superclass
295-
}
296-
}
297-
298-
for (var interFace : clazz.getInterfaces()) {
299-
var method = findAccessibleGetNameMethod(interFace);
300-
if (method != null) {
301-
return method;
302-
}
303-
}
304-
305-
var superclass = clazz.getSuperclass();
306-
if (superclass != null) {
307-
return findAccessibleGetNameMethod(superclass);
308-
}
309-
310-
return null;
311-
}
312-
313293
private Sort buildSort(Map<String, String> mapping, boolean withDefault) {
314294
if (sortFields.isEmpty()) {
315295
return withDefault ? getMappedDefaultSort(mapping) : Sort.unsorted();

src/main/java/it/aboutbits/springboot/toolbox/persistence/SortMappings.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
import java.util.HashMap;
77

8+
/// A mapping of sort parameter definitions to their corresponding database columns.
9+
///
10+
/// The values in this map are of type [Object] and are expected to be either:
11+
/// - [String] - for column names when using Hibernate/JPA
12+
/// - `org.jooq.Field<?>` - for jOOQ field objects when using jOOQ
13+
///
14+
/// @param <T> the enum type that implements [SortParameter.Definition]
815
@NullMarked
916
public class SortMappings<T extends Enum<?> & SortParameter.Definition> extends HashMap<T, Object> {
1017
SortMappings() {

src/test/java/it/aboutbits/springboot/toolbox/persistence/SortMappingsForJooqTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import it.aboutbits.springboot.toolbox.parameter.SortParameter;
44
import org.jooq.impl.DSL;
55
import org.jspecify.annotations.NullMarked;
6+
import org.junit.jupiter.api.DisplayName;
67
import org.junit.jupiter.api.Test;
78
import org.springframework.data.domain.Sort;
89

@@ -18,6 +19,7 @@ private enum ESort implements SortParameter.Definition {
1819
}
1920

2021
@Test
22+
@DisplayName("Method map should return a mapping with a jOOQ Field object")
2123
void map_shouldReturnMappingWithFieldObject() {
2224
var field = DSL.field("my_column");
2325
var mapping = SortMappingsForJooq.map(ESort.Property1, field);
@@ -27,6 +29,7 @@ void map_shouldReturnMappingWithFieldObject() {
2729
}
2830

2931
@Test
32+
@DisplayName("Method of should create a SortMappingsForJooq instance")
3033
void of_shouldCreateSortMappingsForJooq() {
3134
var field1 = DSL.field("col1");
3235
var mappings = SortMappingsForJooq.of(
@@ -38,6 +41,7 @@ void of_shouldCreateSortMappingsForJooq() {
3841
}
3942

4043
@Test
44+
@DisplayName("Should build Spring Sort with jOOQ Fields correctly")
4145
void buildSpringSortWithJooqFields() {
4246
var mappings = SortMappingsForJooq.of(
4347
SortMappingsForJooq.map(ESort.Property1, DSL.field("my_col"))

0 commit comments

Comments
 (0)