@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); } }