@SuppressWarnings("unchecked") public <S, T> Instantiator<S, T> createEmptyArgsInstantiator(final Class<S> source, final Class<? extends T> target) throws Exception { InstantiatorKey instantiatorKey = new InstantiatorKey(target, source); Class<? extends Instantiator<?, ?>> instantiatorType = instantiatorCache.get(instantiatorKey); if (instantiatorType == null) { final String className = generateClassNameForInstantiator(instantiatorKey); final byte[] bytes = ConstructorBuilder.createEmptyConstructor(className, source, target); instantiatorType = (Class<? extends Instantiator<?, ?>>) createClass(className, bytes, target.getClassLoader()); instantiatorCache.putIfAbsent(instantiatorKey, instantiatorType); } return (Instantiator<S, T>) instantiatorType.newInstance(); }
@SuppressWarnings("unchecked") public <S, T> Instantiator<S, T> createEmptyArgsInstantiator(final Class<S> source, final Class<? extends T> target) throws Exception { InstantiatorKey instantiatorKey = new InstantiatorKey(target, source); Class<? extends Instantiator<?, ?>> instantiatorType = instantiatorCache.get(instantiatorKey); if (instantiatorType == null) { final String className = generateClassNameForInstantiator(instantiatorKey); final byte[] bytes = ConstructorBuilder.createEmptyConstructor(className, source, target); instantiatorType = (Class<? extends Instantiator<?, ?>>) createClass(className, bytes, target.getClassLoader()); instantiatorCache.putIfAbsent(instantiatorKey, instantiatorType); } return (Instantiator<S, T>) instantiatorType.newInstance(); }
@SuppressWarnings("unchecked") public <S, T> Instantiator<S, T> createInstantiator(final Class<S> source, final InstantiatorDefinition instantiatorDefinition, final Map<Parameter, Getter<? super S, ?>> injections, boolean builderIgnoresNullValues) throws Exception { InstantiatorKey<S> instantiatorKey = new InstantiatorKey<S>(instantiatorDefinition, injections, source); Class<? extends Instantiator<?, ?>> instantiator = instantiatorCache.get(instantiatorKey); Instantiator<Void, ?> builderInstantiator = null; if (instantiator == null) { final String className = generateClassNameForInstantiator(instantiatorKey); final byte[] bytes; if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) { bytes = InstantiatorBuilder.createInstantiator(className, source, (ExecutableInstantiatorDefinition)instantiatorDefinition, injections); } else { builderInstantiator = createInstantiator(Void.class, ((BuilderInstantiatorDefinition)instantiatorDefinition).getBuilderInstantiator(), new HashMap<Parameter, Getter<? super Void, ?>>(), builderIgnoresNullValues); bytes = InstantiatorBuilder.createInstantiator( className, source, (BuilderInstantiatorDefinition)instantiatorDefinition, injections, builderIgnoresNullValues); } instantiator = (Class<? extends Instantiator<?, ?>>) createClass(className, bytes, instantiatorKey.getDeclaringClass().getClassLoader()); instantiatorCache.put(instantiatorKey, instantiator); } Map<String, Getter<? super S, ?>> getterPerName = new HashMap<String, Getter<? super S, ?>>(); for(Entry<Parameter, Getter<? super S, ?>> e : injections.entrySet()) { getterPerName.put(e.getKey().getName(), e.getValue()); } if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) { return (Instantiator<S, T>) instantiator.getConstructor(Map.class).newInstance(getterPerName); } else { return (Instantiator<S, T>) instantiator.getConstructor(Map.class, Instantiator.class).newInstance(getterPerName, builderInstantiator); } }
@SuppressWarnings("unchecked") public <S, T> Instantiator<S, T> createInstantiator(final Class<S> source, final InstantiatorDefinition instantiatorDefinition, final Map<Parameter, Getter<? super S, ?>> injections, boolean builderIgnoresNullValues) throws Exception { InstantiatorKey<S> instantiatorKey = new InstantiatorKey<S>(instantiatorDefinition, injections, source); Class<? extends Instantiator<?, ?>> instantiator = instantiatorCache.get(instantiatorKey); Instantiator<Void, ?> builderInstantiator = null; if (instantiator == null) { final String className = generateClassNameForInstantiator(instantiatorKey); final byte[] bytes; if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) { bytes = InstantiatorBuilder.createInstantiator(className, source, (ExecutableInstantiatorDefinition)instantiatorDefinition, injections); } else { builderInstantiator = createInstantiator(Void.class, ((BuilderInstantiatorDefinition)instantiatorDefinition).getBuilderInstantiator(), new HashMap<Parameter, Getter<? super Void, ?>>(), builderIgnoresNullValues); bytes = InstantiatorBuilder.createInstantiator( className, source, (BuilderInstantiatorDefinition)instantiatorDefinition, injections, builderIgnoresNullValues); } instantiator = (Class<? extends Instantiator<?, ?>>) createClass(className, bytes, instantiatorKey.getDeclaringClass().getClassLoader()); instantiatorCache.put(instantiatorKey, instantiator); } Map<String, Getter<? super S, ?>> getterPerName = new HashMap<String, Getter<? super S, ?>>(); for(Entry<Parameter, Getter<? super S, ?>> e : injections.entrySet()) { getterPerName.put(e.getKey().getName(), e.getValue()); } if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) { return (Instantiator<S, T>) instantiator.getConstructor(Map.class).newInstance(getterPerName); } else { return (Instantiator<S, T>) instantiator.getConstructor(Map.class, Instantiator.class).newInstance(getterPerName, builderInstantiator); } }
@Test public void testEqualsOnSameSourceAndTargetValues() throws NoSuchMethodException { InstantiatorKey<Date> k1 = new InstantiatorKey<Date>(DbObject.class, Date.class); InstantiatorKey<Date> k2 = new InstantiatorKey<Date>(DbObject.class, Date.class); assertTrue(k1.equals(k2)); }
@Test public void testEqualsOnSameSourceAndTargetAndInjectParamValues() throws NoSuchMethodException { final InjectedParam[] injectedParameters = createInjectedParameters("param", Getter.class); InstantiatorKey<Date> k1 = new InstantiatorKey<Date>(DbObject.class.getConstructor(), injectedParameters, Date.class); InstantiatorKey<Date> k2 = new InstantiatorKey<Date>(DbObject.class.getConstructor(), injectedParameters, Date.class); assertTrue(k1.equals(k2)); } private InjectedParam[] createInjectedParameters(String param) {
@Test public void testNotEqualsOnDiffClass() throws NoSuchMethodException { InstantiatorKey<Date> k1 = new InstantiatorKey<Date>(DbObject.class, Date.class); assertFalse(k1.equals(new Object())); }
@Test public void testNotEqualsOnSameSourceAndTargetAndDiffInjectParamValues() throws NoSuchMethodException { InstantiatorKey<Date> k1 = new InstantiatorKey<Date>(DbObject.class.getConstructor(), createInjectedParameters("param"), Date.class); InstantiatorKey<Date> k2 = new InstantiatorKey<Date>(DbObject.class.getConstructor(), createInjectedParameters("param2"), Date.class); InstantiatorKey<Date> k3 = new InstantiatorKey<Date>(DbObject.class.getConstructor(), null, Date.class); InstantiatorKey<Date> k4 = new InstantiatorKey<Date>(DbObject.class.getConstructor(), createInjectedParameters("param", IntGetter.class), Date.class); assertFalse(k1.equals(k2)); assertFalse(k1.equals(k3)); assertFalse(k3.equals(k1)); assertFalse(k4.equals(k1)); }
@Test public void testNotEqualsOnDiffSourceAndSameTargetValues() throws NoSuchMethodException { InstantiatorKey<Date> k1 = new InstantiatorKey<Date>(DbObject.class, Date.class); InstantiatorKey<InputStream> k2 = new InstantiatorKey<InputStream>(DbObject.class, InputStream.class); InstantiatorKey<Object> k3 = new InstantiatorKey<Object>(DbObject.class, null); assertFalse(k1.equals(k2)); assertFalse(k1.equals(k3)); assertFalse(k3.equals(k1)); }
@Test public void testNotEqualsDiffConstructor() throws NoSuchMethodException { InstantiatorKey<Date> k1 = new InstantiatorKey<Date>(DbObject.class.getConstructor(), createInjectedParameters("param"), Date.class); InstantiatorKey<Date> k2 = new InstantiatorKey<Date>(DbFinalObject.class.getDeclaredConstructors()[0], createInjectedParameters("param"), Date.class); InstantiatorKey<Date> k3 = new InstantiatorKey<Date>(null, createInjectedParameters( "param"), Date.class); assertFalse(k1.equals(k2)); assertFalse(k1.equals(k3)); assertFalse(k3.equals(k1)); }
@Test public void testNotEqualsOnNull() throws NoSuchMethodException { InstantiatorKey<Date> k1 = new InstantiatorKey<Date>(DbObject.class, Date.class); assertFalse(k1.equals(null)); }
@Test public void testEqualsOnSameKey() throws NoSuchMethodException { InstantiatorKey<Date> k = new InstantiatorKey<Date>(DbObject.class, Date.class); assertTrue(k.equals(k)); }