public SpecificationRunnerMonitorProxy(Object proxied) { if ( ! DuckType.instanceOf(SpecificationRunnerMonitor.class, proxied)) { throw new IllegalArgumentException("Not a SpecificationRunnerMonitor: " + proxied.getClass().getName()); } this.proxied = DuckType.implement(SpecificationRunnerMonitor.class, proxied); }
/** * Causes object to implement the interfaceToImplement and returns an * instance of the object implementing interfaceToImplement even if * interfaceToImplement was not declared in object.getClass()'s implements * declaration. * <p> * This works as long as all methods declared in interfaceToImplement are * present on object. * * @param type The Java class of the interface to implement * @param object The object to force to implement interfaceToImplement * @return object, but now implementing interfaceToImplement */ public static <T> T implement(Class<T> type, Object object) { if (type.isInstance(object)) { return type.cast(object); } return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, new DuckType(object))); }
private String regularExpressionOf(Annotation paramAnnotation) { if (DuckType.instanceOf(RegEx.class, paramAnnotation)) { try { RegEx regEx = DuckType.implement(RegEx.class, paramAnnotation); return regEx.value(); } catch (Exception e) { LOG.error(LOG_ERROR, e); // Not a valid RegEx annotation } } return null; }
private static BreachSpecificationEncapsulation breachEncapsulation(Example example) { if ( ! DuckType.instanceOf(BreachSpecificationEncapsulation.class, example)) { throw new IllegalArgumentException("Cant' breach encapsulation of " + example.getClass().getName()); } return DuckType.implement(BreachSpecificationEncapsulation.class, example); } }
private static BreachSpecificationEncapsulation breachEncapsulation(Example example) { if ( ! DuckType.instanceOf(BreachSpecificationEncapsulation.class, example)) { throw new IllegalArgumentException("Cant' breach encapsulation of " + example.getClass().getName()); } return DuckType.implement(BreachSpecificationEncapsulation.class, example); } }