Use switch expression where feasible

This commit is contained in:
Yanming Zhou
2023-11-01 10:49:43 +08:00
committed by Sam Brannen
parent 8ed04b5dd1
commit 490b5c77fc
28 changed files with 420 additions and 424 deletions

View File

@@ -1150,36 +1150,34 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on Session interface coming in...
switch (method.getName()) {
case "equals":
return switch (method.getName()) {
case "equals" -> (proxy == args[0]);
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
case "hashCode" -> System.identityHashCode(proxy);
// Use hashCode of Session proxy.
return System.identityHashCode(proxy);
case "close":
case "close" -> null;
// Handle close method: suppress, not valid.
return null;
}
default -> {
// Invoke method on target Session.
try {
Object retVal = method.invoke(this.target, args);
// Invoke method on target Session.
try {
Object retVal = method.invoke(this.target, args);
// If return value is a Query or Criteria, apply transaction timeout.
// Applies to createQuery, getNamedQuery, createCriteria.
if (retVal instanceof Criteria criteria) {
prepareCriteria(criteria);
}
else if (retVal instanceof Query<?> query) {
prepareQuery(query);
}
// If return value is a Query or Criteria, apply transaction timeout.
// Applies to createQuery, getNamedQuery, createCriteria.
if (retVal instanceof Criteria criteria) {
prepareCriteria(criteria);
yield retVal;
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
else if (retVal instanceof Query<?> query) {
prepareQuery(query);
}
return retVal;
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
};
}
}

View File

@@ -412,28 +412,25 @@ public class LocalSessionFactoryBuilder extends Configuration {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "equals":
return switch (method.getName()) {
case "equals" -> (proxy == args[0]);
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
case "hashCode" -> System.identityHashCode(proxy);
// Use hashCode of EntityManagerFactory proxy.
return System.identityHashCode(proxy);
case "getProperties":
return getProperties();
case "getWrappedObject":
case "getProperties" -> getProperties();
case "getWrappedObject" -> getSessionFactory();
// Call coming in through InfrastructureProxy interface...
return getSessionFactory();
}
// Regular delegation to the target SessionFactory,
// enforcing its full initialization...
try {
return method.invoke(getSessionFactory(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
default -> {
// Regular delegation to the target SessionFactory,
// enforcing its full initialization...
try {
yield method.invoke(getSessionFactory(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
};
}
private SessionFactory getSessionFactory() {

View File

@@ -711,13 +711,15 @@ public abstract class AbstractEntityManagerFactoryBean implements
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "equals":
case "equals" -> {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
}
case "hashCode" -> {
// Use hashCode of EntityManagerFactory proxy.
return System.identityHashCode(proxy);
case "unwrap":
}
case "unwrap" -> {
// Handle JPA 2.1 unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null) {
@@ -726,7 +728,7 @@ public abstract class AbstractEntityManagerFactoryBean implements
else if (targetClass.isInstance(proxy)) {
return proxy;
}
break;
}
}
try {

View File

@@ -297,16 +297,19 @@ public abstract class ExtendedEntityManagerCreator {
// Invocation on EntityManager interface coming in...
switch (method.getName()) {
case "equals":
case "equals" -> {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
}
case "hashCode" -> {
// Use hashCode of EntityManager proxy.
return hashCode();
case "getTargetEntityManager":
}
case "getTargetEntityManager" -> {
// Handle EntityManagerProxy interface.
return this.target;
case "unwrap":
}
case "unwrap" -> {
// Handle JPA 2.0 unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null) {
@@ -315,13 +318,13 @@ public abstract class ExtendedEntityManagerCreator {
else if (targetClass.isInstance(proxy)) {
return proxy;
}
break;
case "isOpen":
}
case "isOpen" -> {
if (this.containerManaged) {
return true;
}
break;
case "close":
}
case "close" -> {
if (this.containerManaged) {
throw new IllegalStateException("Invalid usage: Cannot close a container-managed EntityManager");
}
@@ -332,22 +335,23 @@ public abstract class ExtendedEntityManagerCreator {
synch.closeOnCompletion = true;
return null;
}
break;
case "getTransaction":
}
case "getTransaction" -> {
if (this.synchronizedWithTransaction) {
throw new IllegalStateException(
"Cannot obtain local EntityTransaction from a transaction-synchronized EntityManager");
}
break;
case "joinTransaction":
}
case "joinTransaction" -> {
doJoinTransaction(true);
return null;
case "isJoinedToTransaction":
}
case "isJoinedToTransaction" -> {
// Handle JPA 2.1 isJoinedToTransaction method for the non-JTA case.
if (!this.jta) {
return TransactionSynchronizationManager.hasResource(this.target);
}
break;
}
}
// Do automatic joining if required. Excludes toString, equals, hashCode calls.

View File

@@ -221,20 +221,23 @@ public abstract class SharedEntityManagerCreator {
// Invocation on EntityManager interface coming in...
switch (method.getName()) {
case "equals":
case "equals" -> {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
}
case "hashCode" -> {
// Use hashCode of EntityManager proxy.
return hashCode();
case "toString":
}
case "toString" -> {
// Deliver toString without touching a target EntityManager.
return "Shared EntityManager proxy for target factory [" + this.targetFactory + "]";
case "getEntityManagerFactory":
}
case "getEntityManagerFactory" -> {
// JPA 2.0: return EntityManagerFactory without creating an EntityManager.
return this.targetFactory;
case "getCriteriaBuilder":
case "getMetamodel":
}
case "getCriteriaBuilder", "getMetamodel" -> {
// JPA 2.0: return EntityManagerFactory's CriteriaBuilder/Metamodel (avoid creation of EntityManager)
try {
return EntityManagerFactory.class.getMethod(method.getName()).invoke(this.targetFactory);
@@ -242,23 +245,27 @@ public abstract class SharedEntityManagerCreator {
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
case "unwrap":
}
case "unwrap" -> {
// JPA 2.0: handle unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0];
if (targetClass != null && targetClass.isInstance(proxy)) {
return proxy;
}
break;
case "isOpen":
}
case "isOpen" -> {
// Handle isOpen method: always return true.
return true;
case "close":
}
case "close" -> {
// Handle close method: suppress, not valid.
return null;
case "getTransaction":
}
case "getTransaction" -> {
throw new IllegalStateException(
"Not allowed to create transaction on shared EntityManager - " +
"use Spring transactions or EJB CMT instead");
"use Spring transactions or EJB CMT instead");
}
}
// Determine current EntityManager: either the transactional one
@@ -370,13 +377,15 @@ public abstract class SharedEntityManagerCreator {
// Invocation on Query interface coming in...
switch (method.getName()) {
case "equals":
case "equals" -> {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
}
case "hashCode" -> {
// Use hashCode of EntityManager proxy.
return hashCode();
case "unwrap":
}
case "unwrap" -> {
// Handle JPA 2.0 unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null) {
@@ -385,8 +394,8 @@ public abstract class SharedEntityManagerCreator {
else if (targetClass.isInstance(proxy)) {
return proxy;
}
break;
case "getOutputParameterValue":
}
case "getOutputParameterValue" -> {
if (this.entityManager == null) {
Object key = args[0];
if (this.outputParameters == null || !this.outputParameters.containsKey(key)) {
@@ -398,7 +407,7 @@ public abstract class SharedEntityManagerCreator {
}
return value;
}
break;
}
}
// Invoke method on actual Query object.