HHH-16515 - Add o.h.engine.transaction to nullness checking

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2023-06-02 22:29:12 +02:00 committed by Jan Schatteman
parent e7d0bd0955
commit a3abac9065
13 changed files with 70 additions and 62 deletions

View File

@ -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);
}

View File

@ -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)\\.'
]
}

View File

@ -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<R extends Service> extends ServiceInit
*
* @return The initiated service.
*/
R initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry);
@Nullable R initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry);
}

View File

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

View File

@ -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;
}

View File

@ -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<JtaPlatfor
}
@Override
public JtaPlatform initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry) {
public @Nullable JtaPlatform initiateService(Map<String, Object> 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<JtaPlatfor
return platform;
}
protected JtaPlatform getFallbackProvider(Map<?,?> configurationValues, ServiceRegistryImplementor registry) {
protected @Nullable JtaPlatform getFallbackProvider(Map<?,?> configurationValues, ServiceRegistryImplementor registry) {
return null;
}
}

View File

@ -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;
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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() );
}
}

View File

@ -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;
}

View File

@ -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" );

View File

@ -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);

View File

@ -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}?

View File

@ -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();
}