Use switch expression where feasible
This commit is contained in:
committed by
Sam Brannen
parent
8ed04b5dd1
commit
490b5c77fc
@@ -1655,42 +1655,36 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
// Invocation on ConnectionProxy 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 PersistenceManager proxy.
|
||||
return System.identityHashCode(proxy);
|
||||
case "close":
|
||||
case "close" -> null;
|
||||
// Handle close method: suppress, not valid.
|
||||
return null;
|
||||
case "isClosed":
|
||||
return false;
|
||||
case "getTargetConnection":
|
||||
case "isClosed" -> false;
|
||||
case "getTargetConnection" -> this.target;
|
||||
// Handle getTargetConnection method: return underlying Connection.
|
||||
return this.target;
|
||||
case "unwrap":
|
||||
return (((Class<?>) args[0]).isInstance(proxy) ? proxy : this.target.unwrap((Class<?>) args[0]));
|
||||
case "isWrapperFor":
|
||||
return (((Class<?>) args[0]).isInstance(proxy) || this.target.isWrapperFor((Class<?>) args[0]));
|
||||
}
|
||||
case "unwrap" -> (((Class<?>) args[0]).isInstance(proxy) ? proxy : this.target.unwrap((Class<?>) args[0]));
|
||||
case "isWrapperFor" -> (((Class<?>) args[0]).isInstance(proxy) || this.target.isWrapperFor((Class<?>) args[0]));
|
||||
default -> {
|
||||
// Invoke method on target Connection.
|
||||
try {
|
||||
Object retVal = method.invoke(this.target, args);
|
||||
|
||||
// Invoke method on target Connection.
|
||||
try {
|
||||
Object retVal = method.invoke(this.target, args);
|
||||
// If return value is a JDBC Statement, apply statement settings
|
||||
// (fetch size, max rows, transaction timeout).
|
||||
if (retVal instanceof Statement statement) {
|
||||
applyStatementSettings(statement);
|
||||
}
|
||||
|
||||
// If return value is a JDBC Statement, apply statement settings
|
||||
// (fetch size, max rows, transaction timeout).
|
||||
if (retVal instanceof Statement statement) {
|
||||
applyStatementSettings(statement);
|
||||
yield retVal;
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
throw ex.getTargetException();
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
throw ex.getTargetException();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -302,28 +302,31 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
|
||||
// Invocation on ConnectionProxy interface coming in...
|
||||
|
||||
switch (method.getName()) {
|
||||
case "equals":
|
||||
case "equals" -> {
|
||||
// We must avoid fetching a target Connection for "equals".
|
||||
// Only consider equal when proxies are identical.
|
||||
return (proxy == args[0]);
|
||||
case "hashCode":
|
||||
}
|
||||
case "hashCode" -> {
|
||||
// We must avoid fetching a target Connection for "hashCode",
|
||||
// and we must return the same hash code even when the target
|
||||
// Connection has been fetched: use hashCode of Connection proxy.
|
||||
return System.identityHashCode(proxy);
|
||||
case "getTargetConnection":
|
||||
}
|
||||
case "getTargetConnection" -> {
|
||||
// Handle getTargetConnection method: return underlying connection.
|
||||
return getTargetConnection(method);
|
||||
case "unwrap":
|
||||
}
|
||||
case "unwrap" -> {
|
||||
if (((Class<?>) args[0]).isInstance(proxy)) {
|
||||
return proxy;
|
||||
}
|
||||
break;
|
||||
case "isWrapperFor":
|
||||
}
|
||||
case "isWrapperFor" -> {
|
||||
if (((Class<?>) args[0]).isInstance(proxy)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasTargetConnection()) {
|
||||
@@ -332,58 +335,68 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
|
||||
// a physical JDBC Connection until absolutely necessary.
|
||||
|
||||
switch (method.getName()) {
|
||||
case "toString":
|
||||
case "toString" -> {
|
||||
return "Lazy Connection proxy for target DataSource [" + getTargetDataSource() + "]";
|
||||
case "getAutoCommit":
|
||||
}
|
||||
case "getAutoCommit" -> {
|
||||
if (this.autoCommit != null) {
|
||||
return this.autoCommit;
|
||||
}
|
||||
// Else fetch actual Connection and check there,
|
||||
// because we didn't have a default specified.
|
||||
break;
|
||||
case "setAutoCommit":
|
||||
}
|
||||
case "setAutoCommit" -> {
|
||||
this.autoCommit = (Boolean) args[0];
|
||||
return null;
|
||||
case "getTransactionIsolation":
|
||||
}
|
||||
case "getTransactionIsolation" -> {
|
||||
if (this.transactionIsolation != null) {
|
||||
return this.transactionIsolation;
|
||||
}
|
||||
// Else fetch actual Connection and check there,
|
||||
// because we didn't have a default specified.
|
||||
break;
|
||||
case "setTransactionIsolation":
|
||||
}
|
||||
case "setTransactionIsolation" -> {
|
||||
this.transactionIsolation = (Integer) args[0];
|
||||
return null;
|
||||
case "isReadOnly":
|
||||
}
|
||||
case "isReadOnly" -> {
|
||||
return this.readOnly;
|
||||
case "setReadOnly":
|
||||
}
|
||||
case "setReadOnly" -> {
|
||||
this.readOnly = (Boolean) args[0];
|
||||
return null;
|
||||
case "getHoldability":
|
||||
}
|
||||
case "getHoldability" -> {
|
||||
return this.holdability;
|
||||
case "setHoldability":
|
||||
}
|
||||
case "setHoldability" -> {
|
||||
this.holdability = (Integer) args[0];
|
||||
return null;
|
||||
case "commit":
|
||||
case "rollback":
|
||||
}
|
||||
case "commit", "rollback" -> {
|
||||
// Ignore: no statements created yet.
|
||||
return null;
|
||||
case "getWarnings":
|
||||
case "clearWarnings":
|
||||
}
|
||||
case "getWarnings", "clearWarnings" -> {
|
||||
// Ignore: no warnings to expose yet.
|
||||
return null;
|
||||
case "close":
|
||||
}
|
||||
case "close" -> {
|
||||
// Ignore: no target connection yet.
|
||||
this.closed = true;
|
||||
return null;
|
||||
case "isClosed":
|
||||
}
|
||||
case "isClosed" -> {
|
||||
return this.closed;
|
||||
default:
|
||||
}
|
||||
default -> {
|
||||
if (this.closed) {
|
||||
// Connection proxy closed, without ever having fetched a
|
||||
// physical JDBC Connection: throw corresponding SQLException.
|
||||
throw new SQLException("Illegal operation: connection is closed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -349,34 +349,28 @@ public class SingleConnectionDataSource extends DriverManagerDataSource
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
// Invocation on ConnectionProxy 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 Connection proxy.
|
||||
return System.identityHashCode(proxy);
|
||||
case "close":
|
||||
case "close" -> null;
|
||||
// Handle close method: don't pass the call on.
|
||||
return null;
|
||||
case "isClosed":
|
||||
return this.target.isClosed();
|
||||
case "getTargetConnection":
|
||||
case "isClosed" -> this.target.isClosed();
|
||||
case "getTargetConnection" -> this.target;
|
||||
// Handle getTargetConnection method: return underlying Connection.
|
||||
return this.target;
|
||||
case "unwrap":
|
||||
return (((Class<?>) args[0]).isInstance(proxy) ? proxy : this.target.unwrap((Class<?>) args[0]));
|
||||
case "isWrapperFor":
|
||||
return (((Class<?>) args[0]).isInstance(proxy) || this.target.isWrapperFor((Class<?>) args[0]));
|
||||
}
|
||||
|
||||
// Invoke method on target Connection.
|
||||
try {
|
||||
return method.invoke(this.target, args);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
throw ex.getTargetException();
|
||||
}
|
||||
case "unwrap" -> (((Class<?>) args[0]).isInstance(proxy) ? proxy : this.target.unwrap((Class<?>) args[0]));
|
||||
case "isWrapperFor" -> (((Class<?>) args[0]).isInstance(proxy) || this.target.isWrapperFor((Class<?>) args[0]));
|
||||
default -> {
|
||||
// Invoke method on target Connection.
|
||||
try {
|
||||
yield method.invoke(this.target, args);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
throw ex.getTargetException();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -176,13 +176,15 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
|
||||
// Invocation on ConnectionProxy interface coming in...
|
||||
|
||||
switch (method.getName()) {
|
||||
case "equals":
|
||||
case "equals" -> {
|
||||
// Only considered as equal when proxies are identical.
|
||||
return (proxy == args[0]);
|
||||
case "hashCode":
|
||||
}
|
||||
case "hashCode" -> {
|
||||
// Use hashCode of Connection proxy.
|
||||
return System.identityHashCode(proxy);
|
||||
case "toString":
|
||||
}
|
||||
case "toString" -> {
|
||||
// Allow for differentiating between the proxy and the raw Connection.
|
||||
StringBuilder sb = new StringBuilder("Transaction-aware proxy for target Connection ");
|
||||
if (this.target != null) {
|
||||
@@ -192,23 +194,26 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
|
||||
sb.append(" from DataSource [").append(this.targetDataSource).append(']');
|
||||
}
|
||||
return sb.toString();
|
||||
case "close":
|
||||
}
|
||||
case "close" -> {
|
||||
// Handle close method: only close if not within a transaction.
|
||||
DataSourceUtils.doReleaseConnection(this.target, this.targetDataSource);
|
||||
this.closed = true;
|
||||
return null;
|
||||
case "isClosed":
|
||||
}
|
||||
case "isClosed" -> {
|
||||
return this.closed;
|
||||
case "unwrap":
|
||||
}
|
||||
case "unwrap" -> {
|
||||
if (((Class<?>) args[0]).isInstance(proxy)) {
|
||||
return proxy;
|
||||
}
|
||||
break;
|
||||
case "isWrapperFor":
|
||||
}
|
||||
case "isWrapperFor" -> {
|
||||
if (((Class<?>) args[0]).isInstance(proxy)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.target == null) {
|
||||
|
||||
Reference in New Issue
Block a user