@Test public void testUnwrapWrongClass() { boolean error = false; try { decorator.unwrap(MyOtherClass.class); } catch (Exception e) { error = true; } assertTrue(error); }
@Override public <T> T unwrap(Class<T> iface) throws IllegalArgumentException { if (delegate instanceof Wrapper) return ((Wrapper) delegate).unwrap(iface); else if (iface.isInstance(delegate)) return (T) delegate; else throw new IllegalArgumentException("Cannot unwrap to the requested interface " + iface); }
AbstractDecorator implements Wrapper, Serializable { @Override public <T> T unwrap(Class<T> iface) throws IllegalArgumentException { if (delegate instanceof Wrapper) return ((Wrapper) delegate).unwrap(iface); else if (iface.isInstance(delegate)) return (T) delegate; else throw new IllegalArgumentException("Cannot unwrap to the requested interface " + iface); } }
AbstractDecorator implements Wrapper, Serializable { @Override public <T> T unwrap(Class<T> iface) throws IllegalArgumentException { if (delegate instanceof Wrapper) return ((Wrapper) delegate).unwrap(iface); else if (iface.isInstance(delegate)) return (T) delegate; else throw new IllegalArgumentException("Cannot unwrap to the requested interface " + iface); } AbstractDecorator(D delegate); }
AbstractDecorator implements Wrapper, Serializable { @Override public <T> T unwrap(Class<T> iface) throws IllegalArgumentException { if (delegate instanceof Wrapper) return ((Wrapper) delegate).unwrap(iface); else if (iface.isInstance(delegate)) return (T) delegate; else throw new IllegalArgumentException("Cannot unwrap to the requested interface " + iface); } AbstractDecorator(D delegate); @Override boolean isWrapperFor(Class<?> iface); @Override T unwrap(Class<T> iface); boolean equals(Object obj); int hashCode(); @Override String toString(); }
AbstractDecorator implements Wrapper, Serializable { @Override public <T> T unwrap(Class<T> iface) throws IllegalArgumentException { if (delegate instanceof Wrapper) return ((Wrapper) delegate).unwrap(iface); else if (iface.isInstance(delegate)) return (T) delegate; else throw new IllegalArgumentException("Cannot unwrap to the requested interface " + iface); } AbstractDecorator(D delegate); @Override boolean isWrapperFor(Class<?> iface); @Override T unwrap(Class<T> iface); boolean equals(Object obj); int hashCode(); @Override String toString();  }