/** * Construct an implementation using the default constructor. This provides * some speed boost over invoking {@link #ofClass(Class, Object...)}. * * @param intf the interface to construct an instance of * @param <T> the type of the class * @return an implementation of provided interface constructed using the * default constructor. * @throws IllegalArgumentException thrown if the implementation can not be * constructed * @throws IllegalArgumentException thrown if the provided class is not an * interface */ public <T extends ICDKObject> T ofClass(Class<T> intf) { try { if (!intf.isInterface()) throw new IllegalArgumentException("expected interface, got " + intf.getClass()); Creator<T> creator = get(new ClassBasedKey(intf, EMPTY_CLASS_ARRAY)); return creator.create(null); // throws an exception if no impl was found } catch (InstantiationException e) { throw new IllegalArgumentException("unable to instantiate chem object: ", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("constructor is not accessible: ", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException("invocation target exception: ", e); } }
/** * Construct an implementation using the default constructor. This provides * some speed boost over invoking {@link #ofClass(Class, Object...)}. * * @param intf the interface to construct an instance of * @param <T> the type of the class * @return an implementation of provided interface constructed using the * default constructor. * @throws IllegalArgumentException thrown if the implementation can not be * constructed * @throws IllegalArgumentException thrown if the provided class is not an * interface */ public <T extends ICDKObject> T ofClass(Class<T> intf) { try { if (!intf.isInterface()) throw new IllegalArgumentException("expected interface, got " + intf.getClass()); Creator<T> creator = get(new ClassBasedKey(intf, EMPTY_CLASS_ARRAY)); return creator.create(null); // throws an exception if no impl was found } catch (InstantiationException e) { throw new IllegalArgumentException("unable to instantiate chem object: ", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("constructor is not accessible: ", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException("invocation target exception: ", e); } }
/** * Creates a constructor key for use in accessing constructors. The key * combines the interface and types in a single instance which we can then * use in a map. * * @param intf the interface to build the key for * @param types the classes that the the constructor requires * @return a constructor key which can be used to lookup a constructor */ public static ConstructorKey key(Class<?> intf, Class<?>... types) { return new ClassBasedKey(intf, convert(types)); }
/** * Creates a constructor key for use in accessing constructors. The key * combines the interface and types in a single instance which we can then * use in a map. * * @param intf the interface to build the key for * @param types the classes that the the constructor requires * @return a constructor key which can be used to lookup a constructor */ public static ConstructorKey key(Class<?> intf, Class<?>... types) { return new ClassBasedKey(intf, convert(types)); }