@@ -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 ();
0 commit comments