Skip to content

Commit

Permalink
Issue 9: Consider optimising AbstractInvocationHandler::invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalamohan committed Apr 27, 2018
1 parent 5b3349a commit 3eea6b1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.vibur.dbcp.ViburConfig.SQLSTATE_OBJECT_CLOSED_ERROR;
Expand All @@ -44,7 +42,7 @@ abstract class AbstractInvocationHandler<T> extends ExceptionCollector implement

private static final Logger logger = LoggerFactory.getLogger(AbstractInvocationHandler.class);

private static final Object NO_RESULT = new Object();
protected static final Object NO_RESULT = new Object();

/** The real (raw) object that we are dynamically proxying.
* For example, the underlying JDBC Connection, the underlying JDBC Statement, etc. */
Expand All @@ -57,9 +55,6 @@ abstract class AbstractInvocationHandler<T> extends ExceptionCollector implement

private final AtomicBoolean closed = new AtomicBoolean(false);

private static final Set<String> unrestrictedMethods =
new HashSet<>(Arrays.asList("equals", "hashCode", "toString", "unwrap", "isWrapperFor"));

AbstractInvocationHandler(T target, ViburConfig config, ExceptionCollector exceptionCollector) {
assert target != null;
assert config != null;
Expand Down Expand Up @@ -102,7 +97,8 @@ public final Object invoke(Object objProxy, Method method, Object[] args) throws
Object unrestrictedInvoke(T proxy, Method method, Object[] args) throws SQLException {
String methodName = method.getName();

if (!unrestrictedMethods.contains(methodName)) {
// short circuit for getXXX method calls
if (methodName.startsWith("get")) {
return NO_RESULT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.vibur.dbcp.proxy.Proxy.*;

Expand All @@ -48,9 +44,6 @@ class ConnectionInvocationHandler extends AbstractInvocationHandler<Connection>

private final StatementCache statementCache;

private static final Set<String> unrestrictedMethods =
new HashSet<>(Arrays.asList("close", "isClosed", "isValid", "abort"));

ConnectionInvocationHandler(ConnHolder connHolder, PoolOperations poolOperations, ViburConfig config) {
super(connHolder.rawConnection(), config, null /* becomes a new ExceptionCollector */);
this.connHolder = connHolder;
Expand All @@ -63,8 +56,9 @@ class ConnectionInvocationHandler extends AbstractInvocationHandler<Connection>
@Override
Object unrestrictedInvoke(Connection proxy, Method method, Object[] args) throws SQLException {
String methodName = method.getName();
if (!unrestrictedMethods.contains(methodName)) {
return super.unrestrictedInvoke(proxy, method, args);
// short circuit for getXXX method calls
if (methodName.startsWith("get")) {
return NO_RESULT;
}

if (methodName == "close")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static java.lang.Boolean.FALSE;

Expand All @@ -45,9 +42,6 @@ class ResultSetInvocationHandler extends ChildObjectInvocationHandler<Statement,
private long firstResultSetNanoTime;
private long lastResultSetNanoTime;

private static final Set<String> unrestrictedMethods =
new HashSet<>(Arrays.asList("close", "isClosed", "isValid", "abort"));

ResultSetInvocationHandler(ResultSet rawResultSet, Statement statementProxy,
String sqlQuery, List<Object[]> sqlQueryParams,
ViburConfig config, ExceptionCollector exceptionCollector) {
Expand All @@ -61,8 +55,9 @@ class ResultSetInvocationHandler extends ChildObjectInvocationHandler<Statement,
Object unrestrictedInvoke(ResultSet proxy, Method method, Object[] args) throws SQLException {
String methodName = method.getName();

if (!unrestrictedMethods.contains(methodName)) {
return super.unrestrictedInvoke(proxy, method, args);
// short circuit for getXXX method calls
if (methodName.startsWith("get")) {
return NO_RESULT;
}

if (methodName == "close")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class StatementInvocationHandler extends ChildObjectInvocationHandler<Connection
private final boolean logSqlQueryParams;
private final List<Object[]> sqlQueryParams;

private static final Set<String> unrestrictedMethods =
new HashSet<>(Arrays.asList("close", "isClosed"));

StatementInvocationHandler(StatementHolder statement, StatementCache statementCache, Connection connProxy,
ViburConfig config, ExceptionCollector exceptionCollector) {
super(statement.rawStatement(), connProxy, "getConnection", config, exceptionCollector);
Expand All @@ -79,8 +76,9 @@ class StatementInvocationHandler extends ChildObjectInvocationHandler<Connection
Object unrestrictedInvoke(Statement proxy, Method method, Object[] args) throws SQLException {
String methodName = method.getName();

if (!unrestrictedMethods.contains(methodName)) {
return super.unrestrictedInvoke(proxy, method, args);
// short circuit for getXXX method calls
if (methodName.startsWith("get")) {
return NO_RESULT;
}

if (methodName == "close")
Expand Down

0 comments on commit 3eea6b1

Please sign in to comment.