use of com.facebook.presto.bytecode.SmartClassWriter in project presto by prestodb.
the class BytecodeExpressionAssertions method execute.
public static Object execute(Function<Scope, BytecodeNode> nodeGenerator, ParameterizedType returnType, Optional<ClassLoader> parentClassLoader) throws Exception {
ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("Test"), type(Object.class));
MethodDefinition method = classDefinition.declareMethod(a(PUBLIC, STATIC), "test", returnType);
BytecodeNode node = nodeGenerator.apply(method.getScope());
method.getBody().append(node);
if (DUMP_BYTE_CODE_TREE) {
DumpBytecodeVisitor dumpBytecode = new DumpBytecodeVisitor(System.out);
dumpBytecode.visitClass(classDefinition);
}
DynamicClassLoader classLoader = new DynamicClassLoader(parentClassLoader.orElse(BytecodeExpressionAssertions.class.getClassLoader()));
ClassWriter cw = new SmartClassWriter(ClassInfoLoader.createClassInfoLoader(ImmutableList.of(classDefinition), classLoader));
classDefinition.visit(cw);
Class<?> clazz = classLoader.defineClass(classDefinition.getType().getJavaClassName(), cw.toByteArray());
return clazz.getMethod("test").invoke(null);
}
Aggregations