| // Copyright (c) 2024, the R8 project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| /** | |
| * Dump of class file for twr.twraddsuppressed.TestClass | |
| * | |
| * <p>* | |
| * | |
| * <p>This is used to get the JDK-9 javac code with `$closeResource`, which `-target 9` for later | |
| * javac versions does not produce. | |
| * | |
| * <p>package twr.twraddsuppressed; | |
| * | |
| * <p>import java.io.Closeable; | |
| * | |
| * <p>public class TestClass { | |
| * | |
| * <p>public static class MyClosable implements Closeable { @Override public void close() { throw | |
| * new RuntimeException("CLOSE"); } } | |
| * | |
| * <p>public static void foo() { throw new RuntimeException("FOO"); } | |
| * | |
| * <p>public static void bar() { // NOTE: The $closeResource helper is _only_ generated by the JDK-9 | |
| * compiler. // // Use twr twice to have javac generate a shared $closeResource helper. try | |
| * (MyClosable closable = new MyClosable()) { foo(); } try (MyClosable closable = new MyClosable()) | |
| * { foo(); } } | |
| * | |
| * <p>public static void main(String[] args) { try { bar(); } catch (Exception e) { Throwable[] | |
| * suppressed = e.getSuppressed(); if (suppressed.length == 0) { System.out.println("NONE"); } else | |
| * { for (Throwable throwable : suppressed) { System.out.println(throwable.getMessage()); } } } } } | |
| */ | |
| package twr.twraddsuppressed.asm; | |
| import org.objectweb.asm.AnnotationVisitor; | |
| import org.objectweb.asm.ClassWriter; | |
| import org.objectweb.asm.FieldVisitor; | |
| import org.objectweb.asm.Label; | |
| import org.objectweb.asm.MethodVisitor; | |
| import org.objectweb.asm.Opcodes; | |
| import org.objectweb.asm.RecordComponentVisitor; | |
| public class TestClassDump implements Opcodes { | |
| public static byte[] dump() throws Exception { | |
| ClassWriter classWriter = new ClassWriter(0); | |
| FieldVisitor fieldVisitor; | |
| RecordComponentVisitor recordComponentVisitor; | |
| MethodVisitor methodVisitor; | |
| AnnotationVisitor annotationVisitor0; | |
| classWriter.visit( | |
| V9, | |
| ACC_PUBLIC | ACC_SUPER, | |
| "twr/twraddsuppressed/TestClass", | |
| null, | |
| "java/lang/Object", | |
| null); | |
| classWriter.visitSource("TestClass.java", null); | |
| classWriter.visitInnerClass( | |
| "twr/twraddsuppressed/TestClass$MyClosable", | |
| "twr/twraddsuppressed/TestClass", | |
| "MyClosable", | |
| ACC_PUBLIC | ACC_STATIC); | |
| { | |
| methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); | |
| methodVisitor.visitCode(); | |
| Label label0 = new Label(); | |
| methodVisitor.visitLabel(label0); | |
| methodVisitor.visitLineNumber(8, label0); | |
| methodVisitor.visitVarInsn(ALOAD, 0); | |
| methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); | |
| methodVisitor.visitInsn(RETURN); | |
| Label label1 = new Label(); | |
| methodVisitor.visitLabel(label1); | |
| methodVisitor.visitLocalVariable( | |
| "this", "Ltwraddsuppressed/TestClass;", null, label0, label1, 0); | |
| methodVisitor.visitMaxs(1, 1); | |
| methodVisitor.visitEnd(); | |
| } | |
| { | |
| methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "foo", "()V", null, null); | |
| methodVisitor.visitCode(); | |
| Label label0 = new Label(); | |
| methodVisitor.visitLabel(label0); | |
| methodVisitor.visitLineNumber(19, label0); | |
| methodVisitor.visitTypeInsn(NEW, "java/lang/RuntimeException"); | |
| methodVisitor.visitInsn(DUP); | |
| methodVisitor.visitLdcInsn("FOO"); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); | |
| methodVisitor.visitInsn(ATHROW); | |
| methodVisitor.visitMaxs(3, 0); | |
| methodVisitor.visitEnd(); | |
| } | |
| { | |
| methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "bar", "()V", null, null); | |
| methodVisitor.visitCode(); | |
| Label label0 = new Label(); | |
| Label label1 = new Label(); | |
| Label label2 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label0, label1, label2, "java/lang/Throwable"); | |
| Label label3 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label0, label1, label3, null); | |
| Label label4 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label2, label4, label3, null); | |
| Label label5 = new Label(); | |
| Label label6 = new Label(); | |
| Label label7 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label5, label6, label7, "java/lang/Throwable"); | |
| Label label8 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label5, label6, label8, null); | |
| Label label9 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label7, label9, label8, null); | |
| Label label10 = new Label(); | |
| methodVisitor.visitLabel(label10); | |
| methodVisitor.visitLineNumber(24, label10); | |
| methodVisitor.visitTypeInsn(NEW, "twr/twraddsuppressed/TestClass$MyClosable"); | |
| methodVisitor.visitInsn(DUP); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESPECIAL, "twr/twraddsuppressed/TestClass$MyClosable", "<init>", "()V", false); | |
| methodVisitor.visitVarInsn(ASTORE, 0); | |
| Label label11 = new Label(); | |
| methodVisitor.visitLabel(label11); | |
| methodVisitor.visitInsn(ACONST_NULL); | |
| methodVisitor.visitVarInsn(ASTORE, 1); | |
| methodVisitor.visitLabel(label0); | |
| methodVisitor.visitLineNumber(25, label0); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESTATIC, "twr/twraddsuppressed/TestClass", "foo", "()V", false); | |
| methodVisitor.visitLabel(label1); | |
| methodVisitor.visitLineNumber(26, label1); | |
| methodVisitor.visitVarInsn(ALOAD, 1); | |
| methodVisitor.visitVarInsn(ALOAD, 0); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESTATIC, | |
| "twr/twraddsuppressed/TestClass", | |
| "$closeResource", | |
| "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", | |
| false); | |
| Label label12 = new Label(); | |
| methodVisitor.visitJumpInsn(GOTO, label12); | |
| methodVisitor.visitLabel(label2); | |
| methodVisitor.visitLineNumber(24, label2); | |
| methodVisitor.visitFrame( | |
| Opcodes.F_FULL, | |
| 2, | |
| new Object[] {"twr/twraddsuppressed/TestClass$MyClosable", "java/lang/Throwable"}, | |
| 1, | |
| new Object[] {"java/lang/Throwable"}); | |
| methodVisitor.visitVarInsn(ASTORE, 2); | |
| methodVisitor.visitVarInsn(ALOAD, 2); | |
| methodVisitor.visitVarInsn(ASTORE, 1); | |
| methodVisitor.visitVarInsn(ALOAD, 2); | |
| methodVisitor.visitInsn(ATHROW); | |
| methodVisitor.visitLabel(label3); | |
| methodVisitor.visitLineNumber(26, label3); | |
| methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/Throwable"}); | |
| methodVisitor.visitVarInsn(ASTORE, 3); | |
| methodVisitor.visitLabel(label4); | |
| methodVisitor.visitVarInsn(ALOAD, 1); | |
| methodVisitor.visitVarInsn(ALOAD, 0); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESTATIC, | |
| "twr/twraddsuppressed/TestClass", | |
| "$closeResource", | |
| "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", | |
| false); | |
| methodVisitor.visitVarInsn(ALOAD, 3); | |
| methodVisitor.visitInsn(ATHROW); | |
| methodVisitor.visitLabel(label12); | |
| methodVisitor.visitLineNumber(27, label12); | |
| methodVisitor.visitFrame(Opcodes.F_CHOP, 2, null, 0, null); | |
| methodVisitor.visitTypeInsn(NEW, "twr/twraddsuppressed/TestClass$MyClosable"); | |
| methodVisitor.visitInsn(DUP); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESPECIAL, "twr/twraddsuppressed/TestClass$MyClosable", "<init>", "()V", false); | |
| methodVisitor.visitVarInsn(ASTORE, 0); | |
| Label label13 = new Label(); | |
| methodVisitor.visitLabel(label13); | |
| methodVisitor.visitInsn(ACONST_NULL); | |
| methodVisitor.visitVarInsn(ASTORE, 1); | |
| methodVisitor.visitLabel(label5); | |
| methodVisitor.visitLineNumber(28, label5); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESTATIC, "twr/twraddsuppressed/TestClass", "foo", "()V", false); | |
| methodVisitor.visitLabel(label6); | |
| methodVisitor.visitLineNumber(29, label6); | |
| methodVisitor.visitVarInsn(ALOAD, 1); | |
| methodVisitor.visitVarInsn(ALOAD, 0); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESTATIC, | |
| "twr/twraddsuppressed/TestClass", | |
| "$closeResource", | |
| "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", | |
| false); | |
| Label label14 = new Label(); | |
| methodVisitor.visitJumpInsn(GOTO, label14); | |
| methodVisitor.visitLabel(label7); | |
| methodVisitor.visitLineNumber(27, label7); | |
| methodVisitor.visitFrame( | |
| Opcodes.F_FULL, | |
| 2, | |
| new Object[] {"twr/twraddsuppressed/TestClass$MyClosable", "java/lang/Throwable"}, | |
| 1, | |
| new Object[] {"java/lang/Throwable"}); | |
| methodVisitor.visitVarInsn(ASTORE, 2); | |
| methodVisitor.visitVarInsn(ALOAD, 2); | |
| methodVisitor.visitVarInsn(ASTORE, 1); | |
| methodVisitor.visitVarInsn(ALOAD, 2); | |
| methodVisitor.visitInsn(ATHROW); | |
| methodVisitor.visitLabel(label8); | |
| methodVisitor.visitLineNumber(29, label8); | |
| methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/Throwable"}); | |
| methodVisitor.visitVarInsn(ASTORE, 4); | |
| methodVisitor.visitLabel(label9); | |
| methodVisitor.visitVarInsn(ALOAD, 1); | |
| methodVisitor.visitVarInsn(ALOAD, 0); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESTATIC, | |
| "twr/twraddsuppressed/TestClass", | |
| "$closeResource", | |
| "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", | |
| false); | |
| methodVisitor.visitVarInsn(ALOAD, 4); | |
| methodVisitor.visitInsn(ATHROW); | |
| methodVisitor.visitLabel(label14); | |
| methodVisitor.visitLineNumber(30, label14); | |
| methodVisitor.visitFrame(Opcodes.F_CHOP, 2, null, 0, null); | |
| methodVisitor.visitInsn(RETURN); | |
| methodVisitor.visitLocalVariable( | |
| "closable", "Ltwraddsuppressed/TestClass$MyClosable;", null, label11, label12, 0); | |
| methodVisitor.visitLocalVariable( | |
| "closable", "Ltwraddsuppressed/TestClass$MyClosable;", null, label13, label14, 0); | |
| methodVisitor.visitMaxs(2, 5); | |
| methodVisitor.visitEnd(); | |
| } | |
| { | |
| methodVisitor = | |
| classWriter.visitMethod( | |
| ACC_PUBLIC | ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); | |
| methodVisitor.visitCode(); | |
| Label label0 = new Label(); | |
| Label label1 = new Label(); | |
| Label label2 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label0, label1, label2, "java/lang/Exception"); | |
| methodVisitor.visitLabel(label0); | |
| methodVisitor.visitLineNumber(34, label0); | |
| methodVisitor.visitMethodInsn( | |
| INVOKESTATIC, "twr/twraddsuppressed/TestClass", "bar", "()V", false); | |
| methodVisitor.visitLabel(label1); | |
| methodVisitor.visitLineNumber(44, label1); | |
| Label label3 = new Label(); | |
| methodVisitor.visitJumpInsn(GOTO, label3); | |
| methodVisitor.visitLabel(label2); | |
| methodVisitor.visitLineNumber(35, label2); | |
| methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/Exception"}); | |
| methodVisitor.visitVarInsn(ASTORE, 1); | |
| Label label4 = new Label(); | |
| methodVisitor.visitLabel(label4); | |
| methodVisitor.visitLineNumber(36, label4); | |
| methodVisitor.visitVarInsn(ALOAD, 1); | |
| methodVisitor.visitMethodInsn( | |
| INVOKEVIRTUAL, "java/lang/Exception", "getSuppressed", "()[Ljava/lang/Throwable;", false); | |
| methodVisitor.visitVarInsn(ASTORE, 2); | |
| Label label5 = new Label(); | |
| methodVisitor.visitLabel(label5); | |
| methodVisitor.visitLineNumber(37, label5); | |
| methodVisitor.visitVarInsn(ALOAD, 2); | |
| methodVisitor.visitInsn(ARRAYLENGTH); | |
| Label label6 = new Label(); | |
| methodVisitor.visitJumpInsn(IFNE, label6); | |
| Label label7 = new Label(); | |
| methodVisitor.visitLabel(label7); | |
| methodVisitor.visitLineNumber(38, label7); | |
| methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); | |
| methodVisitor.visitLdcInsn("NONE"); | |
| methodVisitor.visitMethodInsn( | |
| INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); | |
| methodVisitor.visitJumpInsn(GOTO, label3); | |
| methodVisitor.visitLabel(label6); | |
| methodVisitor.visitLineNumber(40, label6); | |
| methodVisitor.visitFrame( | |
| Opcodes.F_APPEND, | |
| 2, | |
| new Object[] {"java/lang/Exception", "[Ljava/lang/Throwable;"}, | |
| 0, | |
| null); | |
| methodVisitor.visitVarInsn(ALOAD, 2); | |
| methodVisitor.visitVarInsn(ASTORE, 3); | |
| methodVisitor.visitVarInsn(ALOAD, 3); | |
| methodVisitor.visitInsn(ARRAYLENGTH); | |
| methodVisitor.visitVarInsn(ISTORE, 4); | |
| methodVisitor.visitInsn(ICONST_0); | |
| methodVisitor.visitVarInsn(ISTORE, 5); | |
| Label label8 = new Label(); | |
| methodVisitor.visitLabel(label8); | |
| methodVisitor.visitFrame( | |
| Opcodes.F_APPEND, | |
| 3, | |
| new Object[] {"[Ljava/lang/Throwable;", Opcodes.INTEGER, Opcodes.INTEGER}, | |
| 0, | |
| null); | |
| methodVisitor.visitVarInsn(ILOAD, 5); | |
| methodVisitor.visitVarInsn(ILOAD, 4); | |
| methodVisitor.visitJumpInsn(IF_ICMPGE, label3); | |
| methodVisitor.visitVarInsn(ALOAD, 3); | |
| methodVisitor.visitVarInsn(ILOAD, 5); | |
| methodVisitor.visitInsn(AALOAD); | |
| methodVisitor.visitVarInsn(ASTORE, 6); | |
| Label label9 = new Label(); | |
| methodVisitor.visitLabel(label9); | |
| methodVisitor.visitLineNumber(41, label9); | |
| methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); | |
| methodVisitor.visitVarInsn(ALOAD, 6); | |
| methodVisitor.visitMethodInsn( | |
| INVOKEVIRTUAL, "java/lang/Throwable", "getMessage", "()Ljava/lang/String;", false); | |
| methodVisitor.visitMethodInsn( | |
| INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); | |
| Label label10 = new Label(); | |
| methodVisitor.visitLabel(label10); | |
| methodVisitor.visitLineNumber(40, label10); | |
| methodVisitor.visitIincInsn(5, 1); | |
| methodVisitor.visitJumpInsn(GOTO, label8); | |
| methodVisitor.visitLabel(label3); | |
| methodVisitor.visitLineNumber(45, label3); | |
| methodVisitor.visitFrame( | |
| Opcodes.F_FULL, 1, new Object[] {"[Ljava/lang/String;"}, 0, new Object[] {}); | |
| methodVisitor.visitInsn(RETURN); | |
| Label label11 = new Label(); | |
| methodVisitor.visitLabel(label11); | |
| methodVisitor.visitLocalVariable( | |
| "throwable", "Ljava/lang/Throwable;", null, label9, label10, 6); | |
| methodVisitor.visitLocalVariable( | |
| "suppressed", "[Ljava/lang/Throwable;", null, label5, label3, 2); | |
| methodVisitor.visitLocalVariable("e", "Ljava/lang/Exception;", null, label4, label3, 1); | |
| methodVisitor.visitLocalVariable("args", "[Ljava/lang/String;", null, label0, label11, 0); | |
| methodVisitor.visitMaxs(2, 7); | |
| methodVisitor.visitEnd(); | |
| } | |
| { | |
| methodVisitor = | |
| classWriter.visitMethod( | |
| ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, | |
| "$closeResource", | |
| "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", | |
| null, | |
| null); | |
| methodVisitor.visitCode(); | |
| Label label0 = new Label(); | |
| Label label1 = new Label(); | |
| Label label2 = new Label(); | |
| methodVisitor.visitTryCatchBlock(label0, label1, label2, "java/lang/Throwable"); | |
| Label label3 = new Label(); | |
| methodVisitor.visitLabel(label3); | |
| methodVisitor.visitLineNumber(26, label3); | |
| methodVisitor.visitVarInsn(ALOAD, 0); | |
| Label label4 = new Label(); | |
| methodVisitor.visitJumpInsn(IFNULL, label4); | |
| methodVisitor.visitLabel(label0); | |
| methodVisitor.visitVarInsn(ALOAD, 1); | |
| methodVisitor.visitMethodInsn( | |
| INVOKEINTERFACE, "java/lang/AutoCloseable", "close", "()V", true); | |
| methodVisitor.visitLabel(label1); | |
| Label label5 = new Label(); | |
| methodVisitor.visitJumpInsn(GOTO, label5); | |
| methodVisitor.visitLabel(label2); | |
| methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/Throwable"}); | |
| methodVisitor.visitVarInsn(ASTORE, 2); | |
| methodVisitor.visitVarInsn(ALOAD, 0); | |
| methodVisitor.visitVarInsn(ALOAD, 2); | |
| methodVisitor.visitMethodInsn( | |
| INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V", false); | |
| methodVisitor.visitJumpInsn(GOTO, label5); | |
| methodVisitor.visitLabel(label4); | |
| methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); | |
| methodVisitor.visitVarInsn(ALOAD, 1); | |
| methodVisitor.visitMethodInsn( | |
| INVOKEINTERFACE, "java/lang/AutoCloseable", "close", "()V", true); | |
| methodVisitor.visitLabel(label5); | |
| methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); | |
| methodVisitor.visitInsn(RETURN); | |
| Label label6 = new Label(); | |
| methodVisitor.visitLabel(label6); | |
| methodVisitor.visitLocalVariable("x0", "Ljava/lang/Throwable;", null, label3, label6, 0); | |
| methodVisitor.visitLocalVariable("x1", "Ljava/lang/AutoCloseable;", null, label3, label6, 1); | |
| methodVisitor.visitMaxs(2, 3); | |
| methodVisitor.visitEnd(); | |
| } | |
| classWriter.visitEnd(); | |
| return classWriter.toByteArray(); | |
| } | |
| } |