fix generic typing of Consumers to make them contravariant

This commit is contained in:
Gavin King 2024-11-18 16:11:40 +01:00
parent b15368b93e
commit 0878fbbddf
2 changed files with 11 additions and 12 deletions

View File

@ -220,7 +220,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
/**
* Open a {@link Session} and use it to perform an action.
*/
default void inSession(Consumer<Session> action) {
default void inSession(Consumer<? super Session> action) {
try ( Session session = openSession() ) {
action.accept( session );
}
@ -231,7 +231,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
*
* @since 6.3
*/
default void inStatelessSession(Consumer<StatelessSession> action) {
default void inStatelessSession(Consumer<? super StatelessSession> action) {
try ( StatelessSession session = openStatelessSession() ) {
action.accept( session );
}
@ -244,7 +244,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
* @apiNote This method competes with the JPA-defined method
* {@link #runInTransaction}
*/
default void inTransaction(Consumer<Session> action) {
default void inTransaction(Consumer<? super Session> action) {
inSession( session -> manageTransaction( session, session.beginTransaction(), action ) );
}
@ -254,14 +254,14 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
*
* @since 6.3
*/
default void inStatelessTransaction(Consumer<StatelessSession> action) {
default void inStatelessTransaction(Consumer<? super StatelessSession> action) {
inStatelessSession( session -> manageTransaction( session, session.beginTransaction(), action ) );
}
/**
* Open a {@link Session} and use it to obtain a value.
*/
default <R> R fromSession(Function<Session,R> action) {
default <R> R fromSession(Function<? super Session,R> action) {
try ( Session session = openSession() ) {
return action.apply( session );
}
@ -272,7 +272,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
*
* @since 6.3
*/
default <R> R fromStatelessSession(Function<StatelessSession,R> action) {
default <R> R fromStatelessSession(Function<? super StatelessSession,R> action) {
try ( StatelessSession session = openStatelessSession() ) {
return action.apply( session );
}
@ -285,7 +285,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
* @apiNote This method competes with the JPA-defined method
* {@link #callInTransaction}
*/
default <R> R fromTransaction(Function<Session,R> action) {
default <R> R fromTransaction(Function<? super Session,R> action) {
return fromSession( session -> manageTransaction( session, session.beginTransaction(), action ) );
}
@ -295,7 +295,7 @@ public interface SessionFactory extends EntityManagerFactory, Referenceable, Ser
*
* @since 6.3
*/
default <R> R fromStatelessTransaction(Function<StatelessSession,R> action) {
default <R> R fromStatelessTransaction(Function<? super StatelessSession,R> action) {
return fromStatelessSession( session -> manageTransaction( session, session.beginTransaction(), action ) );
}

View File

@ -904,15 +904,14 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
return getJpaMetamodel().getNamedEntityGraphs( entityType );
}
@Override @SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void runInTransaction(Consumer<EntityManager> work) {
inTransaction( (Consumer) work );
inTransaction( work );
}
@Override
public <R> R callInTransaction(Function<EntityManager, R> work) {
//noinspection unchecked,rawtypes
return (R) fromTransaction( (Function) work );
return fromTransaction( work );
}
@Override