diff --git a/checkerstubs/java.lang.astub b/checkerstubs/java.lang.astub new file mode 100644 index 0000000000..54608ed501 --- /dev/null +++ b/checkerstubs/java.lang.astub @@ -0,0 +1,9 @@ +// Checkerframework stubs for java.lang + +package java.lang.reflect; + +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class Method extends Executable { + void invoke(@Nullable Object obj, Object @Nullable ... args); +} diff --git a/gradle/java-module.gradle b/gradle/java-module.gradle index df0756ac2f..55613ede5d 100644 --- a/gradle/java-module.gradle +++ b/gradle/java-module.gradle @@ -525,7 +525,7 @@ checkerFramework { extraJavacArgs = [ '-AsuppressWarnings=initialization', "-Astubs=${project.rootDir}/checkerstubs", - '-AonlyDefs=^org\\.hibernate\\.(jpamodelgen|spi|pretty|stat|engine\\.profile|(action|context|bytecode)\\.spi)\\.' + '-AonlyDefs=^org\\.hibernate\\.(jpamodelgen|spi|pretty|stat|engine\\.(profile|transaction)|(action|context|bytecode)\\.spi)\\.' ] } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceInitiator.java index d961796814..e26c7df629 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceInitiator.java @@ -12,6 +12,8 @@ import org.hibernate.service.Service; import org.hibernate.service.spi.ServiceInitiator; import org.hibernate.service.spi.ServiceRegistryImplementor; +import org.checkerframework.checker.nullness.qual.Nullable; + /** * Contract for an initiator of services that target the standard {@link org.hibernate.service.ServiceRegistry}. * @@ -28,5 +30,5 @@ public interface StandardServiceInitiator extends ServiceInit * * @return The initiated service. */ - R initiateService(Map configurationValues, ServiceRegistryImplementor registry); + @Nullable R initiateService(Map configurationValues, ServiceRegistryImplementor registry); } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/spi/StrategySelector.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/spi/StrategySelector.java index 61477e5f08..f88d60bdeb 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/spi/StrategySelector.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/spi/StrategySelector.java @@ -11,6 +11,8 @@ import java.util.concurrent.Callable; import org.hibernate.service.Service; +import org.checkerframework.checker.nullness.qual.Nullable; + /** * Service which acts as a registry for named strategy implementations. *

@@ -79,7 +81,7 @@ public interface StrategySelector extends Service { * * @return The strategy instance */ - T resolveStrategy(Class strategy, Object strategyReference); + T resolveStrategy(Class strategy, @Nullable Object strategyReference); /** * Resolve strategy instances. The incoming reference might be:

    diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/AbstractJtaPlatform.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/AbstractJtaPlatform.java index eeb0b6cad9..ebd223045b 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/AbstractJtaPlatform.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/AbstractJtaPlatform.java @@ -15,7 +15,9 @@ import jakarta.transaction.UserTransaction; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.jndi.spi.JndiService; +import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; +import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException; import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.spi.Configurable; @@ -31,13 +33,31 @@ public abstract class AbstractJtaPlatform private boolean cacheUserTransaction; private ServiceRegistryImplementor serviceRegistry; - private final JtaSynchronizationStrategy tmSynchronizationStrategy = new TransactionManagerBasedSynchronizationStrategy( this ); + private final JtaSynchronizationStrategy tmSynchronizationStrategy = new TransactionManagerBasedSynchronizationStrategy(); @Override public void injectServices(ServiceRegistryImplementor serviceRegistry) { this.serviceRegistry = serviceRegistry; } + private final class TransactionManagerBasedSynchronizationStrategy implements JtaSynchronizationStrategy { + + @Override + public void registerSynchronization(Synchronization synchronization) { + try { + AbstractJtaPlatform.this.getTransactionManager().getTransaction().registerSynchronization( synchronization ); + } + catch (Exception e) { + throw new JtaPlatformException( "Could not access JTA Transaction to register synchronization", e ); + } + } + + @Override + public boolean canRegisterSynchronization() { + return JtaStatusHelper.isActive( AbstractJtaPlatform.this.getTransactionManager() ); + } + } + protected ServiceRegistry serviceRegistry() { return serviceRegistry; } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/JtaPlatformInitiator.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/JtaPlatformInitiator.java index 4a0ff04545..e5abb95f9e 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/JtaPlatformInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/JtaPlatformInitiator.java @@ -18,6 +18,8 @@ import org.hibernate.service.spi.ServiceRegistryImplementor; import org.jboss.logging.Logger; +import org.checkerframework.checker.nullness.qual.Nullable; + /** * Standard initiator for the standard {@link JtaPlatform} * @@ -34,7 +36,7 @@ public class JtaPlatformInitiator implements StandardServiceInitiator configurationValues, ServiceRegistryImplementor registry) { + public @Nullable JtaPlatform initiateService(Map configurationValues, ServiceRegistryImplementor registry) { final Object setting = configurationValues.get( AvailableSettings.JTA_PLATFORM ); JtaPlatform platform = registry.getService( StrategySelector.class ).resolveStrategy( JtaPlatform.class, setting ); @@ -57,7 +59,7 @@ public class JtaPlatformInitiator implements StandardServiceInitiator configurationValues, ServiceRegistryImplementor registry) { + protected @Nullable JtaPlatform getFallbackProvider(Map configurationValues, ServiceRegistryImplementor registry) { return null; } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/NoJtaPlatform.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/NoJtaPlatform.java index 56e4155b4b..da0386ba4a 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/NoJtaPlatform.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/NoJtaPlatform.java @@ -13,6 +13,8 @@ import jakarta.transaction.Transaction; import jakarta.transaction.TransactionManager; import jakarta.transaction.UserTransaction; +import org.checkerframework.checker.nullness.qual.Nullable; + import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; /** @@ -24,17 +26,17 @@ public class NoJtaPlatform implements JtaPlatform { public static final NoJtaPlatform INSTANCE = new NoJtaPlatform(); @Override - public TransactionManager retrieveTransactionManager() { + public @Nullable TransactionManager retrieveTransactionManager() { return null; } @Override - public UserTransaction retrieveUserTransaction() { + public @Nullable UserTransaction retrieveUserTransaction() { return null; } @Override - public Object getTransactionIdentifier(Transaction transaction) { + public @Nullable Object getTransactionIdentifier(Transaction transaction) { return null; } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/TransactionManagerBasedSynchronizationStrategy.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/TransactionManagerBasedSynchronizationStrategy.java deleted file mode 100644 index dd7dd6ce66..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/TransactionManagerBasedSynchronizationStrategy.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.engine.transaction.jta.platform.internal; - -import jakarta.transaction.Synchronization; - -import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; -import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException; - -/** - * Implementation of the {@link JtaSynchronizationStrategy} contract based on using a - * {@link jakarta.transaction.TransactionManager} - * - * @author Steve Ebersole - */ -public class TransactionManagerBasedSynchronizationStrategy implements JtaSynchronizationStrategy { - private final TransactionManagerAccess transactionManagerAccess; - - public TransactionManagerBasedSynchronizationStrategy(TransactionManagerAccess transactionManagerAccess) { - this.transactionManagerAccess = transactionManagerAccess; - } - - @Override - public void registerSynchronization(Synchronization synchronization) { - try { - transactionManagerAccess.getTransactionManager().getTransaction().registerSynchronization( synchronization ); - } - catch (Exception e) { - throw new JtaPlatformException( "Could not access JTA Transaction to register synchronization", e ); - } - } - - @Override - public boolean canRegisterSynchronization() { - return JtaStatusHelper.isActive( transactionManagerAccess.getTransactionManager() ); - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereExtendedJtaPlatform.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereExtendedJtaPlatform.java index fd7c7d0825..4f0ad48dcc 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereExtendedJtaPlatform.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereExtendedJtaPlatform.java @@ -17,6 +17,9 @@ import jakarta.transaction.SystemException; import jakarta.transaction.Transaction; import jakarta.transaction.TransactionManager; import jakarta.transaction.UserTransaction; + +import org.checkerframework.checker.nullness.qual.Nullable; + import javax.transaction.xa.XAResource; import org.hibernate.HibernateException; @@ -147,7 +150,7 @@ public class WebSphereExtendedJtaPlatform extends AbstractJtaPlatform { final InvocationHandler ih = new InvocationHandler() { @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + public @Nullable Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ( "afterCompletion".equals( method.getName() ) ) { int status = args[2].equals(Boolean.TRUE) ? Status.STATUS_COMMITTED : @@ -186,7 +189,7 @@ public class WebSphereExtendedJtaPlatform extends AbstractJtaPlatform { } @Override - public boolean equals(Object other) { + public boolean equals(@Nullable Object other) { if ( !(other instanceof TransactionAdapter) ) { return false; } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereJtaPlatform.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereJtaPlatform.java index 3066a2f03a..056f8c6b95 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereJtaPlatform.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereJtaPlatform.java @@ -9,8 +9,10 @@ package org.hibernate.engine.transaction.jta.platform.internal; import java.lang.reflect.Method; import jakarta.transaction.TransactionManager; import jakarta.transaction.UserTransaction; +import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException; +import org.hibernate.internal.util.NullnessUtil; import org.jboss.logging.Logger; @@ -46,7 +48,7 @@ public class WebSphereJtaPlatform extends AbstractJtaPlatform { throw new JtaPlatformException( "Could not locate WebSphere TransactionManager access class" ); } - this.transactionManagerAccessClass = tmAccessClass; + this.transactionManagerAccessClass = NullnessUtil.castNonNull( tmAccessClass ); this.webSphereEnvironment = webSphereEnvironment; } @@ -56,7 +58,7 @@ public class WebSphereJtaPlatform extends AbstractJtaPlatform { } @Override - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked"}) protected TransactionManager locateTransactionManager() { try { final Method method = transactionManagerAccessClass.getMethod( "getTransactionManager" ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereLibertyJtaPlatform.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereLibertyJtaPlatform.java index 8ea76dc57c..1237bcf5b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereLibertyJtaPlatform.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/internal/WebSphereLibertyJtaPlatform.java @@ -16,6 +16,7 @@ import jakarta.transaction.UserTransaction; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException; +import org.hibernate.internal.util.NullnessUtil; /** * JTA platform implementation intended for use with WebSphere Liberty and OpenLiberty @@ -48,6 +49,7 @@ public class WebSphereLibertyJtaPlatform extends AbstractJtaPlatform { return (UserTransaction) jndiService().locate( UT_NAME ); } + @Override public boolean canRegisterSynchronization() { try { return getCurrentStatus() == Status.STATUS_ACTIVE; @@ -57,17 +59,20 @@ public class WebSphereLibertyJtaPlatform extends AbstractJtaPlatform { } } + @Override public int getCurrentStatus() throws SystemException { - return retrieveTransactionManager().getStatus(); + return NullnessUtil.castNonNull( retrieveTransactionManager() ).getStatus(); } + @Override public Object getTransactionIdentifier(Transaction transaction) { return transaction; } + @Override public void registerSynchronization(Synchronization synchronization) { try { - retrieveTransactionManager().getTransaction().registerSynchronization(synchronization); + NullnessUtil.castNonNull( retrieveTransactionManager() ).getTransaction().registerSynchronization(synchronization); } catch ( RollbackException | SystemException x ) { throw new RuntimeException(x); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/spi/JtaPlatform.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/spi/JtaPlatform.java index f047f54939..7cb8c2d25b 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/spi/JtaPlatform.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/jta/platform/spi/JtaPlatform.java @@ -11,6 +11,7 @@ import jakarta.transaction.SystemException; import jakarta.transaction.Transaction; import jakarta.transaction.TransactionManager; import jakarta.transaction.UserTransaction; +import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.service.Service; @@ -37,7 +38,7 @@ public interface JtaPlatform extends Service { * * @return The {@link TransactionManager} */ - TransactionManager retrieveTransactionManager(); + @Nullable TransactionManager retrieveTransactionManager(); /** * Locate the {@link UserTransaction}. @@ -48,7 +49,7 @@ public interface JtaPlatform extends Service { * * @return The {@link UserTransaction} */ - UserTransaction retrieveUserTransaction(); + @Nullable UserTransaction retrieveUserTransaction(); /** * Determine an identifier for the given transaction appropriate for use in caching/lookup usages. @@ -59,7 +60,7 @@ public interface JtaPlatform extends Service { * @param transaction The transaction to be identified. * @return An appropriate identifier */ - Object getTransactionIdentifier(Transaction transaction); + @Nullable Object getTransactionIdentifier(Transaction transaction); /** * Can we currently register a {@link Synchronization}? diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/JtaPlatformInaccessibleImpl.java b/hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/JtaPlatformInaccessibleImpl.java index a1ef93f723..db35600528 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/JtaPlatformInaccessibleImpl.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/JtaPlatformInaccessibleImpl.java @@ -12,6 +12,7 @@ import jakarta.transaction.SystemException; import jakarta.transaction.Transaction; import jakarta.transaction.TransactionManager; import jakarta.transaction.UserTransaction; +import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; @@ -28,7 +29,7 @@ public class JtaPlatformInaccessibleImpl implements JtaPlatform { } @Override - public TransactionManager retrieveTransactionManager() { + public @Nullable TransactionManager retrieveTransactionManager() { if ( preferExceptions ) { throw new JtaPlatformInaccessibleException(); } @@ -36,7 +37,7 @@ public class JtaPlatformInaccessibleImpl implements JtaPlatform { } @Override - public UserTransaction retrieveUserTransaction() { + public @Nullable UserTransaction retrieveUserTransaction() { if ( preferExceptions ) { throw new JtaPlatformInaccessibleException(); } @@ -44,7 +45,7 @@ public class JtaPlatformInaccessibleImpl implements JtaPlatform { } @Override - public Object getTransactionIdentifier(Transaction transaction) { + public @Nullable Object getTransactionIdentifier(Transaction transaction) { if ( preferExceptions ) { throw new JtaPlatformInaccessibleException(); }