@Override public Object invoke(Object target, Method method, Object[] args) throws Throwable { Object result = InvocationUtils.invokeQuietly(connection, method, args); if ("prepareStatement".equals(method.getName())) { PreparedStatement statement = (PreparedStatement) result; String sql = (String) args[0]; return buildStatementProxy(PreparedStatement.class, new ProfilingPreparedStatementHandler(statement, sql)); } if ("createStatement".equals(method.getName())) { Statement statement = (Statement) result; return buildStatementProxy(Statement.class, new ProfilingStatementHandler(statement)); } return result; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { profiler.addContext("sql", SqlLogFormatter.reformatSql(sql)); if (sqlParams.length > 0) { profiler.addContext("params", SqlLogFormatter.reformatParams(sqlParams)); } profiler.stopTrace(""); } return result; } else if (method.getName().startsWith("set") && args.length > 1) { sqlParams[(int) args[0] - 1] = args[1]; return InvocationUtils.invokeQuietly(statement, method, args); } else { return InvocationUtils.invokeQuietly(statement, method, args); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { String sql = (String) args[0]; profiler.addContext("sql", SqlLogFormatter.reformatSql(sql)); profiler.stopTrace(""); } return result; } else { return InvocationUtils.invokeQuietly(statement, method, args); } } }
@Test public void should_return_result() throws Throwable { String toString = "toString"; Object target = mock(Object.class); when(target.toString()).thenReturn(toString); assertThat(InvocationUtils.invokeQuietly(target, Object.class.getMethod("toString"), new Object[0])).isEqualTo(toString); }
@Test public void should_throw_declared_exception() throws Throwable { Connection target = mock(Connection.class); String failSql = "any sql"; when(target.prepareStatement(failSql)).thenThrow(new SQLException("Expected")); try { InvocationUtils.invokeQuietly(target, Connection.class.getMethod("prepareStatement", String.class), new Object[] {failSql}); fail(); } catch (Throwable t) { assertThat(t).isInstanceOf(SQLException.class); } }
@Test public void should_wrap_undeclared_exception() throws Throwable { Connection target = mock(Connection.class); String failSql = "any sql"; when(target.prepareStatement(failSql)).thenThrow(new SQLException("Expected")); try { InvocationUtils.invokeQuietly(target, Object.class.getMethod("wait"), new Object[0]); fail(); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalStateException.class); } }
@Override public Object invoke(Object target, Method method, Object[] args) throws Throwable { Object result = InvocationUtils.invokeQuietly(connection, method, args); if ("prepareStatement".equals(method.getName())) { PreparedStatement statement = (PreparedStatement) result; String sql = (String) args[0]; return buildStatementProxy(PreparedStatement.class, new ProfilingPreparedStatementHandler(statement, sql)); } if ("createStatement".equals(method.getName())) { Statement statement = (Statement) result; return buildStatementProxy(Statement.class, new ProfilingStatementHandler(statement)); } return result; }
@Override public Object invoke(Object target, Method method, Object[] args) throws Throwable { Object result = InvocationUtils.invokeQuietly(connection, method, args); if ("prepareStatement".equals(method.getName())) { PreparedStatement statement = (PreparedStatement) result; String sql = (String) args[0]; return buildStatementProxy(PreparedStatement.class, new ProfilingPreparedStatementHandler(statement, sql)); } if ("createStatement".equals(method.getName())) { Statement statement = (Statement) result; return buildStatementProxy(Statement.class, new ProfilingStatementHandler(statement)); } return result; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { profiler.addContext("sql", SqlLogFormatter.formatSql(sql)); if (sqlParams.length > 0) { profiler.addContext("params", SqlLogFormatter.formatParams(sqlParams)); } profiler.stopTrace(""); } return result; } else if (method.getName().startsWith("set") && args.length > 1) { sqlParams[(int) args[0] - 1] = args[1]; return InvocationUtils.invokeQuietly(statement, method, args); } else { return InvocationUtils.invokeQuietly(statement, method, args); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { String sql = (String) args[0]; profiler.addContext("sql", SqlLogFormatter.formatSql(sql)); profiler.stopTrace(""); } return result; } else { return InvocationUtils.invokeQuietly(statement, method, args); } } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { String sql = (String) args[0]; profiler.addContext("sql", SqlLogFormatter.reformatSql(sql)); profiler.stopTrace(""); } return result; } else { return InvocationUtils.invokeQuietly(statement, method, args); } } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { profiler.addContext("sql", SqlLogFormatter.reformatSql(sql)); if (sqlParams.length > 0) { profiler.addContext("params", SqlLogFormatter.reformatParams(sqlParams)); } profiler.stopTrace(""); } return result; } else if (method.getName().startsWith("set") && args.length > 1) { sqlParams[(int) args[0] - 1] = args[1]; return InvocationUtils.invokeQuietly(statement, method, args); } else { return InvocationUtils.invokeQuietly(statement, method, args); } }