/** * Returns an object that implements the given interface to allow access to non-standard methods, * or standard methods not exposed by the proxy. The result may be either the object found to * implement the interface or a proxy for that object. If the receiver implements the interface * then that is the object. If the receiver is a wrapper and the wrapped object implements the * interface then that is the object. Otherwise the object is the result of calling <code>unwrap * </code> recursively on the wrapped object. If the receiver is not a wrapper and does not * implement the interface, then an <code>SQLException</code> is thrown. * * @param iface A Class defining an interface that the result must implement. * @return an object that implements the interface. May be a proxy for the actual implementing * object. * @throws java.sql.SQLException If no object found that implements the interface * @since 1.6 */ public synchronized <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException { try { if ("java.sql.Statement".equals(iface.getName()) || "java.sql.PreparedStatement".equals(iface.getName()) || "java.sql.Wrapper.class".equals(iface.getName())) { return iface.cast(this); } if (unwrappedInterfaces == null) { unwrappedInterfaces = new HashMap(); } Object cachedUnwrapped = unwrappedInterfaces.get(iface); if (cachedUnwrapped == null) { if (cachedUnwrapped == null) { cachedUnwrapped = Proxy.newProxyInstance( this.wrappedStmt.getClass().getClassLoader(), new Class[] {iface}, new ConnectionErrorFiringInvocationHandler(this.wrappedStmt)); unwrappedInterfaces.put(iface, cachedUnwrapped); } unwrappedInterfaces.put(iface, cachedUnwrapped); } return iface.cast(cachedUnwrapped); } catch (ClassCastException cce) { throw SQLError.createSQLException( "Unable to unwrap to " + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor); } }
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException { try { // This works for classes that aren't actually wrapping anything return iface.cast(this); } catch (ClassCastException cce) { throw SQLError.createSQLException("Unable to unwrap to " + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor()); } }
private List<Class<?>> buildClassesList( List<Class<?>> result, Set<String> seen, boolean publicOnly) { for (Class<?> c = clazz; c != null; c = c.getSuperclass()) { for (Class<?> f : c.getClassCache().getDeclaredClasses(false, publicOnly)) { String s = f.toString(); if (!seen.contains(s)) { result.add(f); seen.add(s); } } } return result; }
@NoInline private void throwNoSuchMethodException(String name, Class<?>... parameterTypes) throws NoSuchMethodException { StringBuilder typeString; if (parameterTypes == null || parameterTypes.length == 0) { typeString = new StringBuilder("()"); } else { typeString = new StringBuilder("("); for (int i = 0; i < parameterTypes.length - 1; i++) { Class<?> c = parameterTypes[i]; typeString.append(c.toString()); typeString.append(", "); } typeString.append(parameterTypes[parameterTypes.length - 1]); typeString.append(')'); } throw new NoSuchMethodException(name + typeString); }