Search in sources :

Example 1 with MethodTooLargeException

use of org.objectweb.asm.MethodTooLargeException in project presto by prestodb.

the class ClassGenerator method defineClasses.

public Map<String, Class<?>> defineClasses(List<ClassDefinition> classDefinitions) {
    ClassInfoLoader classInfoLoader = createClassInfoLoader(classDefinitions, classLoader);
    Map<String, byte[]> bytecodes = new LinkedHashMap<>();
    for (ClassDefinition classDefinition : classDefinitions) {
        // We call the simpler class writer first to get any errors out using simpler setting.
        // This helps when we have large queries that can potentially cause COMPUTE_FRAMES
        // (used by SmartClassWriter for doing more thorough analysis)
        ClassWriter simpleClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classDefinition.visit(simpleClassWriter);
        try {
            simpleClassWriter.toByteArray();
        } catch (ClassTooLargeException | MethodTooLargeException largeCodeException) {
            throw new ByteCodeTooLargeException(largeCodeException);
        } catch (RuntimeException e) {
            throw new CompilationException("Error compiling class: " + classDefinition.getName(), e);
        }
        ClassWriter writer = new SmartClassWriter(classInfoLoader);
        try {
            classDefinition.visit(fakeLineNumbers ? new AddFakeLineNumberClassVisitor(writer) : writer);
        } catch (IndexOutOfBoundsException | NegativeArraySizeException e) {
            StringWriter out = new StringWriter();
            classDefinition.visit(new TraceClassVisitor(null, new Textifier(), new PrintWriter(out)));
            throw new IllegalArgumentException("Error processing class definition:\n" + out, e);
        }
        byte[] bytecode;
        try {
            bytecode = writer.toByteArray();
        } catch (ClassTooLargeException | MethodTooLargeException largeCodeException) {
            throw new ByteCodeTooLargeException(largeCodeException);
        } catch (RuntimeException e) {
            throw new CompilationException("Error compiling class: " + classDefinition.getName(), e);
        }
        bytecodes.put(classDefinition.getType().getJavaClassName(), bytecode);
        if (runAsmVerifier) {
            ClassReader reader = new ClassReader(bytecode);
            CheckClassAdapter.verify(reader, classLoader, true, new PrintWriter(output));
        }
    }
    dumpClassPath.ifPresent(path -> bytecodes.forEach((className, bytecode) -> {
        String name = typeFromJavaClassName(className).getClassName() + ".class";
        Path file = path.resolve(name).toAbsolutePath();
        try {
            createDirectories(file.getParent());
            Files.write(file, bytecode);
        } catch (IOException e) {
            throw new UncheckedIOException("Failed to write generated class file: " + file, e);
        }
    }));
    if (dumpRawBytecode) {
        for (byte[] bytecode : bytecodes.values()) {
            ClassReader classReader = new ClassReader(bytecode);
            classReader.accept(new TraceClassVisitor(new PrintWriter(output)), ClassReader.EXPAND_FRAMES);
        }
    }
    Map<String, Class<?>> classes = classLoader.defineClasses(bytecodes);
    try {
        for (Class<?> clazz : classes.values()) {
            Reflection.initialize(clazz);
        }
    } catch (VerifyError e) {
        throw new RuntimeException(e);
    }
    return classes;
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) MethodHandle(java.lang.invoke.MethodHandle) CharStreams.nullWriter(com.google.common.io.CharStreams.nullWriter) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ParameterizedType.typeFromJavaClassName(com.facebook.presto.bytecode.ParameterizedType.typeFromJavaClassName) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) Path(java.nio.file.Path) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) PrintWriter(java.io.PrintWriter) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) Reflection(com.google.common.reflect.Reflection) IOException(java.io.IOException) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) Files.createDirectories(java.nio.file.Files.createDirectories) ClassInfoLoader.createClassInfoLoader(com.facebook.presto.bytecode.ClassInfoLoader.createClassInfoLoader) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) ClassReader(org.objectweb.asm.ClassReader) Writer(java.io.Writer) Optional(java.util.Optional) ClassTooLargeException(org.objectweb.asm.ClassTooLargeException) Textifier(org.objectweb.asm.util.Textifier) UncheckedIOException(java.io.UncheckedIOException) Textifier(org.objectweb.asm.util.Textifier) LinkedHashMap(java.util.LinkedHashMap) ClassInfoLoader.createClassInfoLoader(com.facebook.presto.bytecode.ClassInfoLoader.createClassInfoLoader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) PrintWriter(java.io.PrintWriter) Path(java.nio.file.Path) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ClassWriter(org.objectweb.asm.ClassWriter) ClassTooLargeException(org.objectweb.asm.ClassTooLargeException) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

ClassInfoLoader.createClassInfoLoader (com.facebook.presto.bytecode.ClassInfoLoader.createClassInfoLoader)1 ParameterizedType.typeFromJavaClassName (com.facebook.presto.bytecode.ParameterizedType.typeFromJavaClassName)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 CharStreams.nullWriter (com.google.common.io.CharStreams.nullWriter)1 Reflection (com.google.common.reflect.Reflection)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UncheckedIOException (java.io.UncheckedIOException)1 Writer (java.io.Writer)1 MethodHandle (java.lang.invoke.MethodHandle)1 Files (java.nio.file.Files)1 Files.createDirectories (java.nio.file.Files.createDirectories)1 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1