/** * Access the registered implementations for a given interface. * * @param intf an interface which has registered implementations in the * factory * @param <T> the type of the interface * @return set of implementation classes (empty if none found) */ public <T extends ICDKObject> Set<Class<?>> implementorsOf(Class<T> intf) { Set<Class<?>> implementations = new HashSet<Class<?>>(5); for (ConstructorKey key : lookup.getConstructors(intf)) { implementations.add(get(key).getDeclaringClass()); } return implementations; }
/** * Access the registered implementations for a given interface. * * @param intf an interface which has registered implementations in the * factory * @param <T> the type of the interface * @return set of implementation classes (empty if none found) */ public <T extends ICDKObject> Set<Class<?>> implementorsOf(Class<T> intf) { Set<Class<?>> implementations = new HashSet<Class<?>>(5); for (ConstructorKey key : lookup.getConstructors(intf)) { implementations.add(get(key).getDeclaringClass()); } return implementations; }
/** * Construct an implementation using a constructor whose parameters match * that of the provided objects. * * @param intf the interface to construct an instance of * @param <T> the type of the class * @return an implementation of provided interface * @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, Object... objects) { try { if (!intf.isInterface()) throw new IllegalArgumentException("expected interface, got " + intf.getClass()); Creator<T> constructor = get(new ObjectBasedKey(intf, objects)); return constructor.create(objects); } 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); } }
/** * Construct an implementation using a constructor whose parameters match * that of the provided objects. * * @param intf the interface to construct an instance of * @param <T> the type of the class * @return an implementation of provided interface * @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, Object... objects) { try { if (!intf.isInterface()) throw new IllegalArgumentException("expected interface, got " + intf.getClass()); Creator<T> constructor = get(new ObjectBasedKey(intf, objects)); return constructor.create(objects); } 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); } }