use of com.facebook.presto.bytecode.ClassGenerator in project ignite-3 by apache.
the class AsmMarshallerGenerator method create.
/**
* {@inheritDoc}
*/
@Override
public <K, V> KvMarshaller<K, V> create(SchemaDescriptor schema, Mapper<K> keyMapper, Mapper<V> valueMapper) {
final String className = MARSHALLER_CLASS_NAME_PREFIX + schema.version();
Class<K> keyClass = keyMapper.targetType();
Class<V> valClass = valueMapper.targetType();
final StringWriter writer = new StringWriter();
try {
// Generate Marshaller code.
long generation = System.nanoTime();
final ClassDefinition classDef = generateMarshallerClass(className, schema, keyClass, valClass);
long compilationTime = System.nanoTime();
generation = compilationTime - generation;
final ClassGenerator generator = ClassGenerator.classGenerator(getClassLoader());
if (dumpCode) {
generator.outputTo(writer).fakeLineNumbers(true).runAsmVerifier(true).dumpRawBytecode(true);
}
final Class<? extends KvMarshaller> aClass = generator.defineClass(classDef, KvMarshaller.class);
compilationTime = System.nanoTime() - compilationTime;
if (LOG.isTraceEnabled()) {
LOG.trace("ASM marshaller created: codeGenStage={}us, compileStage={}us. Code: {}", TimeUnit.NANOSECONDS.toMicros(generation), TimeUnit.NANOSECONDS.toMicros(compilationTime), writer);
} else if (LOG.isDebugEnabled()) {
LOG.debug("ASM marshaller created: codeGenStage={}us, compileStage={}us.", TimeUnit.NANOSECONDS.toMicros(generation), TimeUnit.NANOSECONDS.toMicros(compilationTime));
}
// noinspection unchecked
return aClass.getDeclaredConstructor(SchemaDescriptor.class, ObjectFactory.class, ObjectFactory.class).newInstance(schema, MarshallerUtil.factoryForClass(keyClass), MarshallerUtil.factoryForClass(valClass));
} catch (Exception | LinkageError e) {
throw new IllegalArgumentException("Failed to create marshaller for key-value pair: schemaVer=" + schema.version() + ", keyClass=" + keyClass.getSimpleName() + ", valueClass=" + valClass.getSimpleName(), e);
}
}
Aggregations