@Test public void testUnWrap() throws SQLException { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); DataSource wrap = SniffyDataSource.wrap(h2DataSource); assertNotNull(wrap); assertTrue(wrap.isWrapperFor(JdbcDataSource.class)); assertNotNull(wrap.unwrap(JdbcDataSource.class)); { Exception exception = null; Boolean isWrapper = null; try { isWrapper = wrap.isWrapperFor(Test.class); } catch (Exception e) { exception = e; } assertTrue(null != exception || !isWrapper); } { Exception exception = null; Object unwrapped = wrap; try { unwrapped = wrap.unwrap(Test.class); } catch (Exception e) { exception = e; } assertTrue(null != exception || null == unwrapped); } }
@Override public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isAssignableFrom(target.getClass())) { return iface.cast(target); } else { return dataSource.unwrap(iface); } }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isAssignableFrom(target.getClass())) { return iface.cast(target); } else { return dataSource.unwrap(iface); } } }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isAssignableFrom(target.getClass())) { return iface.cast(target); } else { return dataSource.unwrap(iface); } } <T extends CommonDataSource> SniffyDataSource(T target); }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isAssignableFrom(target.getClass())) { return iface.cast(target); } else { return dataSource.unwrap(iface); } } <T extends CommonDataSource> SniffyDataSource(T target); @SuppressWarnings("unchecked") static R wrap(T target); @Override Connection getConnection(); @Override Connection getConnection(String userName, String password); @Override XAConnection getXAConnection(); @Override XAConnection getXAConnection(String user, String password); @Override PooledConnection getPooledConnection(); @Override PooledConnection getPooledConnection(String user, String password); @Override PrintWriter getLogWriter(); @Override void setLogWriter(PrintWriter out); @Override void setLoginTimeout(int seconds); @Override int getLoginTimeout(); @Override @IgnoreJRERequirement @SuppressWarnings("Since15") Logger getParentLogger(); @Override T unwrap(Class<T> iface); @Override boolean isWrapperFor(Class<?> iface); }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isAssignableFrom(target.getClass())) { return iface.cast(target); } else { return dataSource.unwrap(iface); } } <T extends CommonDataSource> SniffyDataSource(T target); @SuppressWarnings("unchecked") static R wrap(T target); @Override Connection getConnection(); @Override Connection getConnection(String userName, String password); @Override XAConnection getXAConnection(); @Override XAConnection getXAConnection(String user, String password); @Override PooledConnection getPooledConnection(); @Override PooledConnection getPooledConnection(String user, String password); @Override PrintWriter getLogWriter(); @Override void setLogWriter(PrintWriter out); @Override void setLoginTimeout(int seconds); @Override int getLoginTimeout(); @Override @IgnoreJRERequirement @SuppressWarnings("Since15") Logger getParentLogger(); @Override T unwrap(Class<T> iface); @Override boolean isWrapperFor(Class<?> iface);  }