Refactor method name dispatching to switch statements

Closes gh-25163
This commit is contained in:
Juergen Hoeller
2020-05-29 23:07:25 +02:00
parent 7207f7645c
commit e955e52f2f
15 changed files with 499 additions and 547 deletions

View File

@@ -1581,34 +1581,25 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on ConnectionProxy interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of PersistenceManager proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("unwrap")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return proxy;
}
}
else if (method.getName().equals("isWrapperFor")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return true;
}
}
else if (method.getName().equals("close")) {
// Handle close method: suppress, not valid.
return null;
}
else if (method.getName().equals("isClosed")) {
return false;
}
else if (method.getName().equals("getTargetConnection")) {
// Handle getTargetConnection method: return underlying Connection.
return this.target;
switch (method.getName()) {
case "equals":
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
// Use hashCode of PersistenceManager proxy.
return System.identityHashCode(proxy);
case "close":
// Handle close method: suppress, not valid.
return null;
case "isClosed":
return false;
case "getTargetConnection":
// 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -285,30 +285,29 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on ConnectionProxy interface coming in...
if (method.getName().equals("equals")) {
// We must avoid fetching a target Connection for "equals".
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("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);
}
else if (method.getName().equals("unwrap")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return proxy;
}
}
else if (method.getName().equals("isWrapperFor")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return true;
}
}
else if (method.getName().equals("getTargetConnection")) {
// Handle getTargetConnection method: return underlying connection.
return getTargetConnection(method);
switch (method.getName()) {
case "equals":
// We must avoid fetching a target Connection for "equals".
// Only consider equal when proxies are identical.
return (proxy == args[0]);
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":
// Handle getTargetConnection method: return underlying connection.
return getTargetConnection(method);
case "unwrap":
if (((Class<?>) args[0]).isInstance(proxy)) {
return proxy;
}
break;
case "isWrapperFor":
if (((Class<?>) args[0]).isInstance(proxy)) {
return true;
}
break;
}
if (!hasTargetConnection()) {
@@ -316,65 +315,59 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
// resolve transaction demarcation methods without fetching
// a physical JDBC Connection until absolutely necessary.
if (method.getName().equals("toString")) {
return "Lazy Connection proxy for target DataSource [" + getTargetDataSource() + "]";
}
else if (method.getName().equals("getAutoCommit")) {
if (this.autoCommit != null) {
return this.autoCommit;
}
// Else fetch actual Connection and check there,
// because we didn't have a default specified.
}
else if (method.getName().equals("setAutoCommit")) {
this.autoCommit = (Boolean) args[0];
return null;
}
else if (method.getName().equals("getTransactionIsolation")) {
if (this.transactionIsolation != null) {
return this.transactionIsolation;
}
// Else fetch actual Connection and check there,
// because we didn't have a default specified.
}
else if (method.getName().equals("setTransactionIsolation")) {
this.transactionIsolation = (Integer) args[0];
return null;
}
else if (method.getName().equals("isReadOnly")) {
return this.readOnly;
}
else if (method.getName().equals("setReadOnly")) {
this.readOnly = (Boolean) args[0];
return null;
}
else if (method.getName().equals("getHoldability")) {
return this.holdability;
}
else if (method.getName().equals("setHoldability")) {
this.holdability = (Integer) args[0];
return null;
}
else if (method.getName().equals("commit") || method.getName().equals("rollback")) {
// Ignore: no statements created yet.
return null;
}
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
// Ignore: no warnings to expose yet.
return null;
}
else if (method.getName().equals("close")) {
// Ignore: no target connection yet.
this.closed = true;
return null;
}
else if (method.getName().equals("isClosed")) {
return this.closed;
}
else 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");
switch (method.getName()) {
case "toString":
return "Lazy Connection proxy for target DataSource [" + getTargetDataSource() + "]";
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":
this.autoCommit = (Boolean) args[0];
return null;
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":
this.transactionIsolation = (Integer) args[0];
return null;
case "isReadOnly":
return this.readOnly;
case "setReadOnly":
this.readOnly = (Boolean) args[0];
return null;
case "getHoldability":
return this.holdability;
case "setHoldability":
this.holdability = (Integer) args[0];
return null;
case "commit":
case "rollback":
// Ignore: no statements created yet.
return null;
case "getWarnings":
case "clearWarnings":
// Ignore: no warnings to expose yet.
return null;
case "close":
// Ignore: no target connection yet.
this.closed = true;
return null;
case "isClosed":
return this.closed;
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");
}
}
}

View File

@@ -299,34 +299,25 @@ public class SingleConnectionDataSource extends DriverManagerDataSource implemen
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on ConnectionProxy interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of Connection proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("unwrap")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return proxy;
}
}
else if (method.getName().equals("isWrapperFor")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return true;
}
}
else if (method.getName().equals("close")) {
// Handle close method: don't pass the call on.
return null;
}
else if (method.getName().equals("isClosed")) {
return this.target.isClosed();
}
else if (method.getName().equals("getTargetConnection")) {
// Handle getTargetConnection method: return underlying Connection.
return this.target;
switch (method.getName()) {
case "equals":
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
// Use hashCode of Connection proxy.
return System.identityHashCode(proxy);
case "close":
// Handle close method: don't pass the call on.
return null;
case "isClosed":
return this.target.isClosed();
case "getTargetConnection":
// 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -175,43 +175,40 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on ConnectionProxy interface coming in...
if (method.getName().equals("equals")) {
// Only considered as equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of Connection proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("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) {
sb.append("[").append(this.target.toString()).append("]");
}
else {
sb.append(" from DataSource [").append(this.targetDataSource).append("]");
}
return sb.toString();
}
else if (method.getName().equals("unwrap")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return proxy;
}
}
else if (method.getName().equals("isWrapperFor")) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return true;
}
}
else if (method.getName().equals("close")) {
// Handle close method: only close if not within a transaction.
DataSourceUtils.doReleaseConnection(this.target, this.targetDataSource);
this.closed = true;
return null;
}
else if (method.getName().equals("isClosed")) {
return this.closed;
switch (method.getName()) {
case "equals":
// Only considered as equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
// Use hashCode of Connection proxy.
return System.identityHashCode(proxy);
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) {
sb.append("[").append(this.target.toString()).append("]");
}
else {
sb.append(" from DataSource [").append(this.targetDataSource).append("]");
}
return sb.toString();
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":
return this.closed;
case "unwrap":
if (((Class<?>) args[0]).isInstance(proxy)) {
return proxy;
}
break;
case "isWrapperFor":
if (((Class<?>) args[0]).isInstance(proxy)) {
return true;
}
break;
}
if (this.target == null) {