From 97ad7f047d6a2b3de0989201ce2239c960f07f52 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 29 Aug 2025 08:59:43 +1000 Subject: [PATCH] minor cleanups to exception SPI package --- .../ConstraintViolationException.java | 1 + .../exception/GenericJDBCException.java | 1 + .../exception/JDBCConnectionException.java | 1 + .../exception/SQLGrammarException.java | 1 + .../StandardSQLExceptionConverter.java | 4 +- ...platedViolatedConstraintNameExtractor.java | 40 ++++++++----------- 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/exception/ConstraintViolationException.java b/hibernate-core/src/main/java/org/hibernate/exception/ConstraintViolationException.java index e5e77cba273c..49e9fa05c236 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/ConstraintViolationException.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/ConstraintViolationException.java @@ -3,6 +3,7 @@ * Copyright Red Hat Inc. and Hibernate Authors */ package org.hibernate.exception; + import java.sql.SQLException; import org.hibernate.JDBCException; diff --git a/hibernate-core/src/main/java/org/hibernate/exception/GenericJDBCException.java b/hibernate-core/src/main/java/org/hibernate/exception/GenericJDBCException.java index 86c970144421..05d0863e112a 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/GenericJDBCException.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/GenericJDBCException.java @@ -3,6 +3,7 @@ * Copyright Red Hat Inc. and Hibernate Authors */ package org.hibernate.exception; + import java.sql.SQLException; import org.hibernate.JDBCException; diff --git a/hibernate-core/src/main/java/org/hibernate/exception/JDBCConnectionException.java b/hibernate-core/src/main/java/org/hibernate/exception/JDBCConnectionException.java index a77f938f2291..09a1bc0a6132 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/JDBCConnectionException.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/JDBCConnectionException.java @@ -3,6 +3,7 @@ * Copyright Red Hat Inc. and Hibernate Authors */ package org.hibernate.exception; + import java.sql.SQLException; import org.hibernate.JDBCException; diff --git a/hibernate-core/src/main/java/org/hibernate/exception/SQLGrammarException.java b/hibernate-core/src/main/java/org/hibernate/exception/SQLGrammarException.java index 9ae3c82971b7..be80ac5adb7b 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/SQLGrammarException.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/SQLGrammarException.java @@ -3,6 +3,7 @@ * Copyright Red Hat Inc. and Hibernate Authors */ package org.hibernate.exception; + import java.sql.SQLException; import org.hibernate.JDBCException; diff --git a/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java b/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java index 450a46559ad3..e2c1efbf2dc9 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java @@ -30,8 +30,8 @@ public StandardSQLExceptionConverter(SQLExceptionConversionDelegate... delegates @Override public JDBCException convert(SQLException sqlException, String message, String sql) { - for ( SQLExceptionConversionDelegate delegate : delegates ) { - final JDBCException jdbcException = delegate.convert( sqlException, message, sql ); + for ( var delegate : delegates ) { + final var jdbcException = delegate.convert( sqlException, message, sql ); if ( jdbcException != null ) { return jdbcException; } diff --git a/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java b/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java index e95c82ab3863..3f17549f09e3 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java @@ -7,10 +7,10 @@ import java.sql.SQLException; import java.util.function.Function; -import org.hibernate.internal.util.NullnessUtil; import org.checkerframework.checker.nullness.qual.Nullable; + /** * Extracts a violated database constraint name from an error message * by matching the error message against a template. @@ -27,23 +27,18 @@ public TemplatedViolatedConstraintNameExtractor(Function ex } @Override - public @Nullable String extractConstraintName(SQLException sqle) { + public @Nullable String extractConstraintName(SQLException exception) { try { - String constraintName = null; - - // handle nested exceptions - do { - constraintName = extractConstraintName.apply(sqle); - if (sqle.getNextException() == null - || sqle.getNextException() == sqle) { - break; - } - else { - sqle = NullnessUtil.castNonNull( sqle.getNextException() ); + while (true) { + final String constraintName = extractConstraintName.apply( exception ); + final SQLException chained = exception.getNextException(); + if ( constraintName != null + || chained == null + || chained == exception ) { + return constraintName; } - } while (constraintName == null); - - return constraintName; + exception = chained; + } } catch (NumberFormatException nfe) { return null; @@ -60,18 +55,15 @@ public TemplatedViolatedConstraintNameExtractor(Function ex * @return The found constraint name, or null. */ public static @Nullable String extractUsingTemplate(String templateStart, String templateEnd, String message) { - int templateStartPosition = message.indexOf( templateStart ); + final int templateStartPosition = message.indexOf( templateStart ); if ( templateStartPosition < 0 ) { return null; } - - int start = templateStartPosition + templateStart.length(); - int end = templateEnd.equals("\n") ? -1 : message.indexOf( templateEnd, start ); - if ( end < 0 ) { - end = message.length(); + else { + final int start = templateStartPosition + templateStart.length(); + final int end = templateEnd.equals( "\n" ) ? -1 : message.indexOf( templateEnd, start ); + return end < 0 ? message.substring( start ) : message.substring( start, end ); } - - return message.substring( start, end ); } }