Skip to content

Instantly share code, notes, and snippets.

@joakime
Last active July 28, 2020 15:10
Show Gist options
  • Select an option

  • Save joakime/ac0d17cbdd4a38b1130f3d5f74cc87ef to your computer and use it in GitHub Desktop.

Select an option

Save joakime/ac0d17cbdd4a38b1130f3d5f74cc87ef to your computer and use it in GitHub Desktop.
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class WithResources
{
public static void main(String[] args) throws IOException
{
try (OutputStream bar = new FileOutputStream(new File("foo")))
{
System.out.println("hi");
}
}
}
$ javap -cp target/classes -c test.WithResources
Compiled from "WithResources.java"
public class test.WithResources {
public test.WithResources();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.io.IOException;
Code:
0: new #2 // class java/io/FileOutputStream
3: dup
4: new #3 // class java/io/File
7: dup
8: ldc #4 // String foo
10: invokespecial #5 // Method java/io/File."<init>":(Ljava/lang/String;)V
13: invokespecial #6 // Method java/io/FileOutputStream."<init>":(Ljava/io/File;)V
16: astore_1
17: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
20: ldc #8 // String hi
22: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
25: aload_1
26: invokevirtual #10 // Method java/io/OutputStream.close:()V
29: goto 48
32: astore_2
33: aload_1
34: invokevirtual #10 // Method java/io/OutputStream.close:()V
37: goto 46
40: astore_3
41: aload_2
42: aload_3
43: invokevirtual #12 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
46: aload_2
47: athrow
48: return
Exception table:
from to target type
17 25 32 Class java/lang/Throwable
33 37 40 Class java/lang/Throwable
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class WithResources2
{
public static void main(String[] args) throws IOException
{
FileOutputStream zed = new FileOutputStream(new File("foo"));
try (OutputStream bar = zed)
{
System.out.println("hi");
}
}
}
$ javap -cp target/classes -c test.WithResources2
Compiled from "WithResources.java"
public class test.WithResources2 {
public test.WithResources2();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.io.IOException;
Code:
0: new #2 // class java/io/FileOutputStream
3: dup
4: new #3 // class java/io/File
7: dup
8: ldc #4 // String foo
10: invokespecial #5 // Method java/io/File."<init>":(Ljava/lang/String;)V
13: invokespecial #6 // Method java/io/FileOutputStream."<init>":(Ljava/io/File;)V
16: astore_1
17: aload_1
18: astore_2
19: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
22: ldc #8 // String hi
24: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
27: aload_2
28: ifnull 60
31: aload_2
32: invokevirtual #10 // Method java/io/OutputStream.close:()V
35: goto 60
38: astore_3
39: aload_2
40: ifnull 58
43: aload_2
44: invokevirtual #10 // Method java/io/OutputStream.close:()V
47: goto 58
50: astore 4
52: aload_3
53: aload 4
55: invokevirtual #12 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
58: aload_3
59: athrow
60: return
Exception table:
from to target type
19 27 38 Class java/lang/Throwable
43 47 50 Class java/lang/Throwable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment