From 73b24b6f7be9d3958df23764cb85200cef0ea2fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 26 Dec 2024 16:21:17 +0100 Subject: [PATCH] Refine null-safety in the spring-orm module Closes gh-34159 --- .../orm/hibernate5/HibernateJdbcException.java | 4 ++-- .../orm/hibernate5/HibernateQueryException.java | 4 ++-- .../jpa/persistenceunit/DefaultPersistenceUnitManager.java | 2 +- ...PersistenceManagedTypesBeanRegistrationAotProcessor.java | 6 +++--- .../jpa/support/PersistenceAnnotationBeanPostProcessor.java | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateJdbcException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateJdbcException.java index fad5469844bf..0f0a8315ac62 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateJdbcException.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateJdbcException.java @@ -42,7 +42,7 @@ public HibernateJdbcException(JDBCException ex) { /** * Return the underlying SQLException. */ - @SuppressWarnings("NullAway") + @SuppressWarnings("NullAway") // JDBCException instances always have a non null cause public SQLException getSQLException() { return ((JDBCException) getCause()).getSQLException(); } @@ -50,7 +50,7 @@ public SQLException getSQLException() { /** * Return the SQL that led to the problem. */ - @SuppressWarnings("NullAway") + @SuppressWarnings("NullAway") // JDBCException instances always have a non null cause public @Nullable String getSql() { return ((JDBCException) getCause()).getSQL(); } diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateQueryException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateQueryException.java index 68fa2a3e63ad..bbae833ab4fc 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateQueryException.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateQueryException.java @@ -39,9 +39,9 @@ public HibernateQueryException(QueryException ex) { /** * Return the HQL query string that was invalid. */ - @SuppressWarnings("NullAway") public @Nullable String getQueryString() { - return ((QueryException) getCause()).getQueryString(); + QueryException cause = (QueryException) getCause(); + return cause == null ? null : cause.getQueryString(); } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java index f1c0b1711061..821d55da50b0 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java @@ -435,7 +435,7 @@ public void afterPropertiesSet() { * @see #obtainDefaultPersistenceUnitInfo() * @see #obtainPersistenceUnitInfo(String) */ - @SuppressWarnings("NullAway") + @SuppressWarnings("NullAway") // Dataflow analysis limitation public void preparePersistenceUnitInfos() { this.persistenceUnitInfoNames.clear(); this.persistenceUnitInfos.clear(); diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java index 3af979ec7ecb..6c42cba4c842 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java @@ -238,13 +238,13 @@ private void contributeHibernateHints(RuntimeHints hints, @Nullable ClassLoader } } - @SuppressWarnings("NullAway") + @SuppressWarnings("NullAway") // Not null assertion performed in ReflectionHints.registerType private void registerForReflection(ReflectionHints reflection, @Nullable Annotation annotation, String attribute) { if (annotation == null) { return; } - Class embeddableInstantiatorClass = (Class) AnnotationUtils.getAnnotationAttributes(annotation).get(attribute); - reflection.registerType(embeddableInstantiatorClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); + Class type = (Class) AnnotationUtils.getAnnotationAttributes(annotation).get(attribute); + reflection.registerType(type, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); } } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java index e163fdde9c46..b653aa1c7cfc 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java @@ -848,7 +848,7 @@ private CodeBlock generateResourceToInjectCode( return CodeBlock.of("$L($L)", generatedMethod.getName(), REGISTERED_BEAN_PARAMETER); } - @SuppressWarnings("NullAway") + @SuppressWarnings("NullAway") // Dataflow analysis limitation private void generateGetEntityManagerMethod(MethodSpec.Builder method, PersistenceElement injectedElement) { String unitName = injectedElement.unitName; Properties properties = injectedElement.properties;