Skip to content

Commit

Permalink
invoke by reflection to avoid compile error of new APIs from JDBC4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
longshine committed Jun 17, 2014
1 parent 7a2cbd7 commit 8771954
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
29 changes: 18 additions & 11 deletions src/main/java/lx/easydb/IConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -1089,29 +1089,32 @@ public boolean isWrapperFor(Class arg0) throws SQLException {
return connection.isWrapperFor(arg0);
}

public Object unwrap(Class iface) throws SQLException {
return connection.unwrap(iface);
}

public void setSchema(String schema) throws SQLException {
connection.setSchema(schema);
ReflectHelper.invokeSilent(connection, "setSchema", new Class[] { String.class }, new Object[] { schema });
}

public String getSchema() throws SQLException {
return connection.getSchema();
return (String) ReflectHelper.invokeSilent(connection, "getSchema", null, null);
}

public void abort(java.util.concurrent.Executor executor) throws SQLException {
connection.abort(executor);
ReflectHelper.invokeSilent(connection, "abort",
new Class[] { java.util.concurrent.Executor.class }, new Object[] { executor });
}

public void setNetworkTimeout(java.util.concurrent.Executor executor, int milliseconds)
throws SQLException {
connection.setNetworkTimeout(executor, milliseconds);
ReflectHelper.invokeSilent(connection, "setNetworkTimeout",
new Class[] { java.util.concurrent.Executor.class, int.class },
new Object[] { executor, new Integer(milliseconds) });
}

public int getNetworkTimeout() throws SQLException {
return connection.getNetworkTimeout();
}

public Object unwrap(Class iface) throws SQLException {
return connection.unwrap(iface);
return ((Integer) ReflectHelper.invokeSilent(connection, "getNetworkTimeout", null, null)).intValue();
}
}

Expand Down Expand Up @@ -1945,11 +1948,15 @@ public void updateNClob(String columnLabel, Reader reader)
}

public Object getObject(int columnIndex, Class type) throws SQLException {
return rs.getObject(columnIndex, type);
return ReflectHelper.invokeSilent(rs, "getObject",
new Class[] { int.class, Class.class },
new Object[] { new Integer(columnIndex), type });
}

public Object getObject(String columnLabel, Class type) throws SQLException {
return rs.getObject(columnLabel, type);
return ReflectHelper.invokeSilent(rs, "getObject",
new Class[] { String.class, Class.class },
new Object[] { columnLabel, type });
}

}
31 changes: 31 additions & 0 deletions src/main/java/lx/easydb/ReflectHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lx.easydb;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class ReflectHelper {
public static final Class[] EMPTY_CLASSES = new Class[0];
public static final Object[] EMPTY_PARAMS = EMPTY_CLASSES;

public static Object invokeSilent(Object obj, String methodName,
Class[] parameterTypes, Object[] args) {
try {
return invoke(obj, methodName, parameterTypes, args);
} catch (Exception e) {
// ignore
return null;
}
}

public static Object invoke(Object obj, String methodName,
Class[] parameterTypes, Object[] args)
throws NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (parameterTypes == null)
parameterTypes = EMPTY_CLASSES;
Method method = obj.getClass().getMethod(methodName, parameterTypes);
if (args == null)
args = EMPTY_PARAMS;
return method.invoke(obj, args);
}
}
8 changes: 3 additions & 5 deletions src/main/java/lx/easydb/ReflectiveExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*
*/
public class ReflectiveExtractor implements ValueExtractor {
private static final Class[] EMPTY_CLASSES = new Class[0];
private static final Object[] EMPTY_PARAMS = new Object[0];

public List extract(ResultSet rs, Table table) throws SQLException {
ArrayList list = new ArrayList();
Expand Down Expand Up @@ -127,10 +125,10 @@ public void extract(ResultSet rs, Object item, int index, String field)

protected Object newInstance(Table table) throws QueryException {
try {
Constructor c = table.getEntityClass().getConstructor(EMPTY_CLASSES);
Constructor c = table.getEntityClass().getConstructor(ReflectHelper.EMPTY_CLASSES);
c.setAccessible(true);
return c.newInstance(EMPTY_PARAMS);
} catch (ReflectiveOperationException e) {
return c.newInstance(ReflectHelper.EMPTY_PARAMS);
} catch (Exception e) {
throw new QueryException(e);
}
}
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/lx/easydb/datasource/SingletonDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Map;
import java.util.Properties;

import lx.easydb.ReflectHelper;

/**
* An {@link IDataSource} which holds a {@link Connection}
* as a singleton. It never closes the inner connection really.
Expand Down Expand Up @@ -256,29 +258,32 @@ public boolean isWrapperFor(Class arg0) throws SQLException {
return this.conn.isWrapperFor(arg0);
}

public Object unwrap(Class iface) throws SQLException {
return conn.unwrap(iface);
}

public void setSchema(String schema) throws SQLException {
conn.setSchema(schema);
ReflectHelper.invokeSilent(conn, "setSchema", new Class[] { String.class }, new Object[] { schema });
}

public String getSchema() throws SQLException {
return conn.getSchema();
return (String) ReflectHelper.invokeSilent(conn, "getSchema", null, null);
}

public void abort(java.util.concurrent.Executor executor) throws SQLException {
conn.abort(executor);
ReflectHelper.invokeSilent(conn, "abort",
new Class[] { java.util.concurrent.Executor.class }, new Object[] { executor });
}

public void setNetworkTimeout(java.util.concurrent.Executor executor, int milliseconds)
throws SQLException {
conn.setNetworkTimeout(executor, milliseconds);
ReflectHelper.invokeSilent(conn, "setNetworkTimeout",
new Class[] { java.util.concurrent.Executor.class, int.class },
new Object[] { executor, new Integer(milliseconds) });
}

public int getNetworkTimeout() throws SQLException {
return conn.getNetworkTimeout();
}

public Object unwrap(Class iface) throws SQLException {
return conn.unwrap(iface);
return ((Integer) ReflectHelper.invokeSilent(conn, "getNetworkTimeout", null, null)).intValue();
}
}
}

0 comments on commit 8771954

Please sign in to comment.