use of org.objectweb.asm.ClassTooLargeException 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;
}
Aggregations