All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.map.asm.MapperAsmFactory Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

The newest version!
package org.simpleflatmapper.map.asm;

import org.simpleflatmapper.map.FieldKey;
import org.simpleflatmapper.map.FieldMapper;
import org.simpleflatmapper.map.SourceMapper;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.mapper.AbstractMapper;
import org.simpleflatmapper.reflect.BiInstantiator;
import org.simpleflatmapper.reflect.asm.AsmFactory;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class MapperAsmFactory {

    private final AsmFactory asmFactory;

	public MapperAsmFactory(AsmFactory asmFactory) {
        this.asmFactory = asmFactory;
	}


    private final Map>> fieldMapperCache = new HashMap>>();

    private  String generateClassNameForFieldMapper(final FieldMapper[] mappers, final FieldMapper[] constructorMappers, final Class source, final Class 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();
    }

    @SuppressWarnings("unchecked")
    public  AbstractMapper createMapper(final FieldKey[] keys,
                                                    final FieldMapper[] mappers,
                                                    final FieldMapper[] constructorMappers,
                                                    final BiInstantiator, T> instantiator,
                                                    final Class source,
                                                    final Class target) throws Exception {
        ClassLoader classLoader = target.getClassLoader();

        MapperKey key = MapperKey.of(keys, mappers, constructorMappers, instantiator, target, source);

        synchronized (fieldMapperCache) {
            Constructor> constructor = (Constructor>) 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> type = (Class>) asmFactory.createClass(className, bytes, classLoader);
                constructor = (Constructor>) type.getDeclaredConstructors()[0];
                fieldMapperCache.put(key, constructor);
            }
            return (AbstractMapper) constructor.newInstance(mappers, constructorMappers, instantiator);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy