Skip to content

Commit

Permalink
HHH-16515 - Add o.h.engine.transaction to nullness checking
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
  • Loading branch information
jrenaat committed Jun 26, 2023
1 parent e7d0bd0 commit a3abac9
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 62 deletions.
9 changes: 9 additions & 0 deletions checkerstubs/java.lang.astub
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion gradle/java-module.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)\\.'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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}.
*
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.hibernate.service.Service;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Service which acts as a registry for named strategy implementations.
* <p>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.jboss.logging.Logger;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Standard initiator for the standard {@link JtaPlatform}
*
Expand All @@ -34,7 +36,7 @@ public Class<JtaPlatform> getServiceInitiated() {
}

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

Expand All @@ -57,7 +59,7 @@ public JtaPlatform initiateService(Map<String, Object> configurationValues, Serv
return platform;
}

protected JtaPlatform getFallbackProvider(Map<?,?> configurationValues, ServiceRegistryImplementor registry) {
protected @Nullable JtaPlatform getFallbackProvider(Map<?,?> configurationValues, ServiceRegistryImplementor registry) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import jakarta.transaction.TransactionManager;
import jakarta.transaction.UserTransaction;

import org.checkerframework.checker.nullness.qual.Nullable;

import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;

/**
Expand All @@ -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;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
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;
Expand Down Expand Up @@ -147,7 +150,7 @@ public void registerSynchronization(final Synchronization synchronization)
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 :
Expand Down Expand Up @@ -186,7 +189,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object other) {
public boolean equals(@Nullable Object other) {
if ( !(other instanceof TransactionAdapter) ) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
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;

Expand Down Expand Up @@ -46,7 +48,7 @@ public WebSphereJtaPlatform() {
throw new JtaPlatformException( "Could not locate WebSphere TransactionManager access class" );
}

this.transactionManagerAccessClass = tmAccessClass;
this.transactionManagerAccessClass = NullnessUtil.castNonNull( tmAccessClass );
this.webSphereEnvironment = webSphereEnvironment;
}

Expand All @@ -56,7 +58,7 @@ public WebSphereJtaPlatform(Class transactionManagerAccessClass, WebSphereEnviro
}

@Override
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked"})
protected TransactionManager locateTransactionManager() {
try {
final Method method = transactionManagerAccessClass.getMethod( "getTransactionManager" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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
Expand Down Expand Up @@ -48,6 +49,7 @@ protected UserTransaction locateUserTransaction() {
return (UserTransaction) jndiService().locate( UT_NAME );
}

@Override
public boolean canRegisterSynchronization() {
try {
return getCurrentStatus() == Status.STATUS_ACTIVE;
Expand All @@ -57,17 +59,20 @@ public boolean canRegisterSynchronization() {
}
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.transaction.Transaction;
import jakarta.transaction.TransactionManager;
import jakarta.transaction.UserTransaction;
import org.checkerframework.checker.nullness.qual.Nullable;

import org.hibernate.service.Service;

Expand All @@ -37,7 +38,7 @@ public interface JtaPlatform extends Service {
*
* @return The {@link TransactionManager}
*/
TransactionManager retrieveTransactionManager();
@Nullable TransactionManager retrieveTransactionManager();

/**
* Locate the {@link UserTransaction}.
Expand All @@ -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.
Expand All @@ -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}?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;

Expand All @@ -28,23 +29,23 @@ public JtaPlatformInaccessibleImpl(boolean preferExceptions) {
}

@Override
public TransactionManager retrieveTransactionManager() {
public @Nullable TransactionManager retrieveTransactionManager() {
if ( preferExceptions ) {
throw new JtaPlatformInaccessibleException();
}
return null;
}

@Override
public UserTransaction retrieveUserTransaction() {
public @Nullable UserTransaction retrieveUserTransaction() {
if ( preferExceptions ) {
throw new JtaPlatformInaccessibleException();
}
return null;
}

@Override
public Object getTransactionIdentifier(Transaction transaction) {
public @Nullable Object getTransactionIdentifier(Transaction transaction) {
if ( preferExceptions ) {
throw new JtaPlatformInaccessibleException();
}
Expand Down

0 comments on commit a3abac9

Please sign in to comment.