/** * 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 constructor the constructor value which this key will be linked * to * @return a constructor key which can be used to lookup a constructor */ private static ConstructorKey key(Class<?> intf, Constructor<?> constructor) { return key(intf, constructor.getParameterTypes()); }
/** * 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 constructor the constructor value which this key will be linked * to * @return a constructor key which can be used to lookup a constructor */ private static ConstructorKey key(Class<?> intf, Constructor<?> constructor) { return key(intf, constructor.getParameterTypes()); }
return register(key(intf, constructor), constructor, modifier) != null;
return register(key(intf, constructor), constructor, modifier) != null;
/** * Unit test ensures the factory wraps up varargs into the correct * representation. This is needed as passing a uniform array will be * converted to actual varargs - this test checks both cases. */ @Test public void testOfClass_Wrapping() { DynamicFactory factory = new DynamicFactory(5); // register ICDKObject with an mock instantiator factory.register(key(ICDKObject.class, IAtom[].class), new DynamicFactory.BasicCreator<IAtom>(null) { @Override public IAtom create(Object[] objects) { return mock(IAtom.class); } }); // uniform parameter array assertNotNull(factory.ofClass(ICDKObject.class, new IAtom[]{mock(IAtom.class), mock(IAtom.class), mock(IAtom.class)})); // is equivalent to just using varargs... assertNotNull(factory.ofClass(ICDKObject.class, mock(IAtom.class), mock(IAtom.class), mock(IAtom.class))); // unless we double wrap it (which resolves to the same instantiator) assertNotNull(factory.ofClass(ICDKObject.class, new Object[]{new IAtom[]{mock(IAtom.class), mock(IAtom.class), mock(IAtom.class)}})); }
@Test public void testKey_Default() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IAtom.class); assertEquals(IAtom.class, key.intf()); assertEquals(0, key.n()); }
@Test public void testOfClass_Instantiator() throws Exception { DynamicFactory factory = new DynamicFactory(5); factory.register(key(IAtom.class), new DynamicFactory.BasicCreator<IAtom>(null) { @Override public IAtom create(Object[] objects) { return mock(IAtom.class); } }); assertNotNull(factory.ofClass(IAtom.class)); }
@Test public void testKey_ArrayParameters() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IBond.class, IAtom[].class); assertEquals(IBond.class, key.intf()); assertEquals(1, key.n()); assertTrue(key.type(0).isArray()); assertEquals(IAtom[].class, key.type(0)); }
@Test public void testKey_Parameters() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IBond.class, IAtom.class, IAtom.class); assertEquals(IBond.class, key.intf()); assertEquals(2, key.n()); assertEquals(IAtom.class, key.type(0)); assertEquals(IAtom.class, key.type(1)); }
/** * Ensures primitive types are converted. */ @Test public void testKey_Primitives() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IAtom.class, boolean.class, byte.class, char.class, short.class, int.class, float.class, long.class, double.class); assertEquals(IAtom.class, key.intf()); assertEquals(8, key.n()); assertEquals(Boolean.class, key.type(0)); assertEquals(Byte.class, key.type(1)); assertEquals(Character.class, key.type(2)); assertEquals(Short.class, key.type(3)); assertEquals(Integer.class, key.type(4)); assertEquals(Float.class, key.type(5)); assertEquals(Long.class, key.type(6)); assertEquals(Double.class, key.type(7)); }