@SuppressWarnings("unchecked") public <T, P> Setter<T,P> createSetter(Field field) throws Exception { Setter<T,P> setter = (Setter<T, P>) setterCache.get(field); if (setter == null) { final String className = generateClassNameForSetter(field); final byte[] bytes = generateSetterByteCodes(field, className); final Class<?> type = createClass(className, bytes, field.getDeclaringClass().getClassLoader()); setter = (Setter<T, P>) type.newInstance(); setterCache.putIfAbsent(field, setter); } return setter; }
public <T, P> Getter<T, P> getMethodGetter(final Method method) { boolean accessible = Modifier.isPublic(method.getModifiers()) && Modifier.isPublic(method.getDeclaringClass().getModifiers()); if (asmFactory != null && accessible) { try { return asmFactory.createGetter(method); } catch(Throwable e) { // ignore } } if (!accessible) { try { method.setAccessible(true); } catch (Exception e) { // cannot make field accessible return null; } } return new MethodGetter<T, P>(method); }
@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 <T, P> Getter<T,P> createGetter(final Field m) throws Exception { Getter<T,P> getter = (Getter<T, P>) getterCache.get(m); if (getter == null) { final String className = generateClassNameForGetter(m); final byte[] bytes = generateGetterByteCodes(m, className); final Class<?> type = createClass(className, bytes, m.getDeclaringClass().getClassLoader()); getter = (Getter<T, P>) type.newInstance(); getterCache.putIfAbsent(m, getter); } return getter; }
private <S, T> String generateClassNameForFieldMapper(final FieldMapper<S, T>[] mappers, final FieldMapper<S, T>[] constructorMappers, final Class<? super S> source, final Class<T> target) { StringBuilder sb = new StringBuilder(); sb.append("org.simpleflatmapper.map.generated."); sb.append(asmFactory.getPackageName(target)); sb.append(".AsmMapperFrom").append(asmFactory.replaceArray(source.getSimpleName())); sb.append("To").append(asmFactory.replaceArray(target.getSimpleName())); if (constructorMappers.length > 0) { sb.append("ConstInj").append(constructorMappers.length); } if (mappers.length > 0) { sb.append("Inj").append(mappers.length); } sb.append("_I").append(Long.toHexString(asmFactory.getNextClassNumber())); return sb.toString(); }
public <T, P> Setter<T, P> getFieldSetter(Field field) { boolean accessible = Modifier.isPublic(field.getModifiers()) && Modifier.isPublic(field.getDeclaringClass().getModifiers()); if (asmFactory != null && accessible) { try { return asmFactory.createSetter(field); } catch(Throwable e) { } } if (!accessible) { try { field.setAccessible(true); } catch (Exception e) { // cannot make field accessible return null; } } return new FieldSetter<T, P>(field); }
private String generateClassNameForBiInstantiator(final BiInstantiatorKey key) { StringBuilder sb = new StringBuilder(); sb.append( "org.simpleflatmapper.reflect.generated.") .append(getPackageName(key.getDeclaringClass())) .append(".AsmBiInstantiator").append(key.getDeclaringClass().getSimpleName()); sb.append("From"); sb.append(replaceArray(key.getS1().getSimpleName())); sb.append("And"); sb.append(replaceArray(key.getS2().getSimpleName())); String[] injectedParams = key.getInjectedParams(); if (injectedParams != null && injectedParams.length > 0) { sb.append("Into"); int e = Math.min(16, injectedParams.length); for(int i = 0; i < e; i++) { if (i!=0) { sb.append("And"); } sb.append(injectedParams[i]); } int l = injectedParams.length - e; if (l >0) { sb.append("And").append(Integer.toString(l)).append("More"); } } sb.append("_I").append(Long.toHexString(classNumber.getAndIncrement())); return sb.toString(); }
Instantiator builderInstantiator = null; if (instantiator == null) { final String className = generateClassNameForBiInstantiator(instantiatorKey); final byte[] bytes; if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) { } else { InstantiatorDefinition biii = ((BuilderInstantiatorDefinition) instantiatorDefinition).getBuilderInstantiator(); builderInstantiator = createInstantiator(Void.class, biii, new HashMap<Parameter, Getter<? super Void, ?>>(), builderIgnoresNullValues); bytes = BiInstantiatorBuilder.createInstantiator( className, (BuilderInstantiatorDefinition)instantiatorDefinition, injections, builderIgnoresNullValues); instantiator = (Class<? extends BiInstantiator<?, ?, ?>>) createClass(className, bytes, instantiatorKey.getDeclaringClass().getClassLoader()); biInstantiatorCache.put(instantiatorKey, instantiator); if (builderInstantiator == null) { InstantiatorDefinition biii = ((BuilderInstantiatorDefinition) instantiatorDefinition).getBuilderInstantiator(); builderInstantiator = createInstantiator(Void.class, biii, new HashMap<Parameter, Getter<? super Void, ?>>(), builderIgnoresNullValues);
@Test public void testSetterInteger() throws Exception { Getter<Pojo, Integer> getI = asmFactory.createGetter(Pojo.class.getDeclaredMethod("getI")); Setter<Pojo, Integer> setI = asmFactory.createSetter(Pojo.class.getDeclaredMethod("setI", Integer.class)); Pojo p = new Pojo(); assertNull(getI.get(p)); setI.set(p, 123); assertEquals(new Integer(123), getI.get(p)); }
@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(); }
@Test public void testRegisterOrCreate() { AsmFactory asmFactory = new AsmFactory(getClass().getClassLoader()); final MyService[] services = new MyService[] {new MyService(), new MyService() }; UnaryFactory<AsmFactory, MyService> factory = new UnaryFactory<AsmFactory, MyService>() { int i = 0; @Override public MyService newInstance(AsmFactory asmFactory) { return services[i++]; } }; assertSame(services[0], asmFactory.registerOrCreate(MyService.class, factory)); assertSame(services[0], asmFactory.registerOrCreate(MyService.class, factory)); }
private String generateClassNameForSetter(final Method m) { return "org.simpleflatmapper.reflect.generated." + (m.getDeclaringClass().getCanonicalName()) + "AsmMethodSetter" +"_" + m.getName()+ "_" + replaceArray(m.getParameterTypes()[0].getSimpleName()) ; }
if (Modifier.isPublic(executable.getModifiers())) { try { return asmFactory.createInstantiator(source, executableInstantiatorDefinition, injections, builderIgnoresNullValues); } catch (Exception e) { return asmFactory.createInstantiator(source, (BuilderInstantiatorDefinition)instantiatorDefinition, injections, builderIgnoresNullValues); } catch (Exception e) {
MapperAsmFactory mapperAsmFactory = reflectionService .getAsmFactory() .registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() { @Override
@Test public void testFailFallBackToMethod() throws Exception { Setter<Foo, String> setter = new ObjectSetterFactory(new AsmFactory(Thread.currentThread().getContextClassLoader()){ @Override public <T, P> Setter<T, P> createSetter(Method m) throws Exception { throw new UnsupportedOperationException(); } }).getSetter(Foo.class, "foo"); assertTrue(setter instanceof MethodSetter); SetterHelperTest.validateFooSetter(setter); }
@SuppressWarnings("unchecked") public <S, T> AbstractMapper<S, T> createMapper(final FieldKey<?>[] keys, final FieldMapper<S, T>[] mappers, final FieldMapper<S, T>[] constructorMappers, final BiInstantiator<S, MappingContext<? super S>, T> instantiator, final Class<? super S> source, final Class<T> target) throws Exception { MapperKey key = MapperKey.of(keys, mappers, constructorMappers, instantiator, target, source); Constructor<SourceMapper<S, T>> constructor = (Constructor<SourceMapper<S, T>>) fieldMapperCache.get(key); if (constructor == null) { final String className = generateClassNameForFieldMapper(mappers, constructorMappers, source, target); final byte[] bytes = MapperAsmBuilder.dump(className, mappers, constructorMappers, source, target); Class<SourceMapper<S, T>> type = (Class<SourceMapper<S, T>>) asmFactory.createClass(className, bytes, target.getClass().getClassLoader()); constructor = (Constructor<SourceMapper<S, T>>) type.getDeclaredConstructors()[0]; fieldMapperCache.put(key, constructor); } return (AbstractMapper<S, T>) constructor.newInstance(mappers, constructorMappers, instantiator); } }
if (Modifier.isPublic(executable.getModifiers())) { try { return asmFactory.createBiInstantiator(s1, s2, executableInstantiatorDefinition, injections, builderIgnoresNullValues); } catch (Exception e) { return asmFactory.createBiInstantiator(s1, s2, (BuilderInstantiatorDefinition)instantiatorDefinition, injections, builderIgnoresNullValues); } catch (Exception e) {
@Test public void testCreateInstantiatorEmptyConstructor() throws Exception { Instantiator<Object, DbObject> instantiator = asmFactory.createEmptyArgsInstantiator(Object.class, DbObject.class); assertNotNull(instantiator.newInstance(null)); assertSame(instantiator.getClass(), asmFactory.createEmptyArgsInstantiator(Object.class, DbObject.class).getClass()); } @Test
public <T, P> Setter<T, P> getFieldSetter(Field field) { boolean accessible = Modifier.isPublic(field.getModifiers()) && Modifier.isPublic(field.getDeclaringClass().getModifiers()); if (asmFactory != null && accessible) { try { return asmFactory.createSetter(field); } catch(Throwable e) { } } if (!accessible) { try { field.setAccessible(true); } catch (Exception e) { // cannot make field accessible return null; } } return new FieldSetter<T, P>(field); }
@SuppressWarnings("unchecked") public <T, P> Getter<T,P> createGetter(final Method m) throws Exception { Getter<T,P> getter = (Getter<T, P>) getterCache.get(m); if (getter == null) { final String className = generateClassNameForGetter(m); final byte[] bytes = generateGetterByteCodes(m, className); final Class<?> type = createClass(className, bytes, m.getDeclaringClass().getClassLoader()); getter = (Getter<T, P>) type.newInstance(); getterCache.putIfAbsent(m, getter); } return getter; }