Nullability fine-tuning around declaration inconsistencies

Issue: SPR-15720
Issue: SPR-15792
This commit is contained in:
Juergen Hoeller
2017-07-19 22:22:14 +02:00
parent 68e6b148cb
commit 46eba3dbfa
186 changed files with 986 additions and 619 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.orm.hibernate5.SessionFactoryUtils;
import org.springframework.orm.hibernate5.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.context.request.AsyncWebRequestInterceptor;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.async.CallableProcessingInterceptor;
@@ -79,23 +80,31 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
protected final Log logger = LogFactory.getLog(getClass());
@Nullable
private SessionFactory sessionFactory;
/**
* Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
*/
public void setSessionFactory(SessionFactory sessionFactory) {
public void setSessionFactory(@Nullable SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
*/
@Nullable
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
private SessionFactory obtainSessionFactory() {
SessionFactory sf = getSessionFactory();
Assert.state(sf != null, "No SessionFactory set");
return sf;
}
/**
* Open a new Hibernate {@code Session} according and bind it to the thread via the
@@ -112,7 +121,7 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
}
}
if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
if (TransactionSynchronizationManager.hasResource(obtainSessionFactory())) {
// Do not modify the Session: just mark the request accordingly.
Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
int newCount = (count != null ? count + 1 : 1);
@@ -122,10 +131,10 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
Session session = openSession();
SessionHolder sessionHolder = new SessionHolder(session);
TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);
TransactionSynchronizationManager.bindResource(obtainSessionFactory(), sessionHolder);
AsyncRequestInterceptor asyncRequestInterceptor =
new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
new AsyncRequestInterceptor(obtainSessionFactory(), sessionHolder);
asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
}
@@ -143,7 +152,7 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
public void afterCompletion(WebRequest request, @Nullable Exception ex) throws DataAccessException {
if (!decrementParticipateCount(request)) {
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
(SessionHolder) TransactionSynchronizationManager.unbindResource(obtainSessionFactory());
logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
@@ -168,7 +177,7 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
@Override
public void afterConcurrentHandlingStarted(WebRequest request) {
if (!decrementParticipateCount(request)) {
TransactionSynchronizationManager.unbindResource(getSessionFactory());
TransactionSynchronizationManager.unbindResource(obtainSessionFactory());
}
}
@@ -183,7 +192,7 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
@SuppressWarnings("deprecation")
protected Session openSession() throws DataAccessResourceFailureException {
try {
Session session = getSessionFactory().openSession();
Session session = obtainSessionFactory().openSession();
session.setFlushMode(FlushMode.MANUAL);
return session;
}
@@ -199,7 +208,7 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
* of the {@code SessionFactory} instance and appends {@link #PARTICIPATE_SUFFIX}.
*/
protected String getParticipateAttributeName() {
return getSessionFactory().toString() + PARTICIPATE_SUFFIX;
return obtainSessionFactory().toString() + PARTICIPATE_SUFFIX;
}
private boolean applySessionBindingInterceptor(WebAsyncManager asyncManager, String key) {

View File

@@ -159,11 +159,12 @@ public abstract class AbstractEntityManagerFactoryBean implements
* @see javax.persistence.spi.PersistenceProvider
* @see javax.persistence.Persistence
*/
public void setPersistenceProvider(PersistenceProvider persistenceProvider) {
public void setPersistenceProvider(@Nullable PersistenceProvider persistenceProvider) {
this.persistenceProvider = persistenceProvider;
}
@Override
@Nullable
public PersistenceProvider getPersistenceProvider() {
return this.persistenceProvider;
}
@@ -175,11 +176,12 @@ public abstract class AbstractEntityManagerFactoryBean implements
* ambiguous EntityManager configurations are found.
* @see javax.persistence.Persistence#createEntityManagerFactory(String)
*/
public void setPersistenceUnitName(String persistenceUnitName) {
public void setPersistenceUnitName(@Nullable String persistenceUnitName) {
this.persistenceUnitName = persistenceUnitName;
}
@Override
@Nullable
public String getPersistenceUnitName() {
return this.persistenceUnitName;
}
@@ -240,11 +242,12 @@ public abstract class AbstractEntityManagerFactoryBean implements
* @see JpaVendorAdapter#getEntityManagerInterface()
* @see EntityManagerFactoryInfo#getEntityManagerInterface()
*/
public void setEntityManagerInterface(Class<? extends EntityManager> emInterface) {
public void setEntityManagerInterface(@Nullable Class<? extends EntityManager> emInterface) {
this.entityManagerInterface = emInterface;
}
@Override
@Nullable
public Class<? extends EntityManager> getEntityManagerInterface() {
return this.entityManagerInterface;
}
@@ -256,11 +259,12 @@ public abstract class AbstractEntityManagerFactoryBean implements
* accessors that intend to use JpaDialect functionality.
* @see EntityManagerFactoryInfo#getJpaDialect()
*/
public void setJpaDialect(JpaDialect jpaDialect) {
public void setJpaDialect(@Nullable JpaDialect jpaDialect) {
this.jpaDialect = jpaDialect;
}
@Override
@Nullable
public JpaDialect getJpaDialect() {
return this.jpaDialect;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ public class DefaultJpaDialect implements JpaDialect, Serializable {
}
@Override
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name)
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, @Nullable String name)
throws PersistenceException {
return null;

View File

@@ -140,9 +140,11 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
* @see DefaultPersistenceUnitManager#setDefaultPersistenceUnitName
*/
@Override
public void setPersistenceUnitName(String persistenceUnitName) {
public void setPersistenceUnitName(@Nullable String persistenceUnitName) {
super.setPersistenceUnitName(persistenceUnitName);
this.internalPersistenceUnitManager.setDefaultPersistenceUnitName(persistenceUnitName);
if (persistenceUnitName != null) {
this.internalPersistenceUnitManager.setDefaultPersistenceUnitName(persistenceUnitName);
}
}
/**
@@ -392,6 +394,7 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
@Override
@Nullable
public PersistenceUnitInfo getPersistenceUnitInfo() {
return this.persistenceUnitInfo;
}

View File

@@ -69,6 +69,7 @@ class SpringPersistenceUnitInfo extends MutablePersistenceUnitInfo {
* if specified.
*/
@Override
@Nullable
public ClassLoader getClassLoader() {
return this.classLoader;
}

View File

@@ -169,7 +169,7 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
}
@Override
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name)
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, @Nullable String name)
throws PersistenceException {
Session session = getSession(entityManager);