使用 try-catch-finally 等效的 Java 7 try-with-resources 字节码是什么?
我试图了解新的 try-with-resources 语句 的工作原理是使用常规的 try-catch-finally 语句重新创建它。给定使用 Java 7 try-with-resources 的以下测试类:
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class TryWithResources {
public static void main(String[] args) {
try (GZIPOutputStream gzip = new GZIPOutputStream(System.out)) {
gzip.write("TEST".getBytes("UTF-8"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
您如何重写此类以使用 try-catch-finally 语句,该语句生成与 try-with-resources 语句生成的字节码完全相同的字节码?此外,使用两个资源时也会出现同样的问题,如下例所示:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class TryWithResources2 {
public static void main(String[] args) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(baos)) {
gzip.write("TEST".getBytes("UTF-8"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
I'm trying to understand how the new try-with-resources statement works by recreating it using regular try-catch-finally statements. Given the following test class using Java 7 try-with-resources:
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class TryWithResources {
public static void main(String[] args) {
try (GZIPOutputStream gzip = new GZIPOutputStream(System.out)) {
gzip.write("TEST".getBytes("UTF-8"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
How would you rewrite this class to use try-catch-finally statements which produces exactly the same bytecode as the try-with-resources statement produces? Also, same question when two resources are used, as in the following example:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class TryWithResources2 {
public static void main(String[] args) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(baos)) {
gzip.write("TEST".getBytes("UTF-8"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于
TryWithResources
类,以下类生成与 try-with-resources 等效的字节码:使用 Sun JDK 1.7.0,
TryWithResources
中的 main 方法的字节码和异常表code> 和TryCatchFinally
类是:对于
TryWithResources2
类,以下类生成与try-with-resources:TryWithResources2
和TryCatchFinally2
类中 main 方法的字节码和异常表为:For the
TryWithResources
class, the following class produces equivalent bytecode as the try-with-resources:Using Sun JDK 1.7.0, the bytecode and exception tables for the main method in both
TryWithResources
andTryCatchFinally
classes is:For the
TryWithResources2
class, the following class produces equivalent bytecode as the try-with-resources:The bytecode and exception tables for the main method in both
TryWithResources2
andTryCatchFinally2
classes is:根据 Java 7 的 Java 语言规范 (JLS) 的第 14.20.3.1 节:
相当于
JLS 更详细……太多,无法在此处重现。
有关 try-with-resources 的 Java 7 JLS 章节以 HTML 形式提供 此处,您可以获得 JLS 此处。
According to section 14.20.3.1 of the Java Language Specification (JLS) for Java 7:
is equivalent to
The JLS goes into a lot more detail ... too much to reproduce here.
The Java 7 JLS chapter on try-with-resources is available in HTML form here, and you can get the PDF version of the JLS here.