字节码功能在Java语言中不可用 [英] Bytecode features not available in the Java language

查看:69
本文介绍了字节码功能在Java语言中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在Java字节码中你可以用Java语言做些什么(Java 6)吗?

Are there currently (Java 6) things you can do in Java bytecode that you can't do from within the Java language?

我知道两者都是图灵完成的,所以读可以做为可以做得更快/更好,或者只是以不同的方式。

I know both are Turing complete, so read "can do" as "can do significantly faster/better, or just in a different way".

我正在考虑额外的字节码,如 invokedynamic ,不能使用Java生成,除非特定的是未来版本。

I'm thinking of extra bytecodes like invokedynamic, which can't be generated using Java, except that specific one is for a future version.

推荐答案

据我所知,Java 6支持的字节码中没有主要功能,这些功能也无法从Java源代码访问。主要原因显然是Java字节码是在考虑Java语言的情况下设计的。

As far as I know there are no major features in the bytecodes supported by Java 6 that are not also accessible from Java source code. The main reason for this is obviously that the Java bytecode was designed with the Java language in mind.

现代Java编译器不会生成一些功能,但是:

There are some features that are not produced by modern Java compilers, however:

  • The ACC_SUPER flag:

这是一个可以在类上设置的标志,并指定如何为此类处理 invokespecial 字节码的特定边案例。它是由所有现代Java编译器设置的(其中现代是> = Java 1.1,如果我没记错的话)并且只有古老的Java编译器生成了这个未设置的类文件。此标志仅出于向后兼容性原因而存在。请注意,从Java 7u51开始,由于安全原因,ACC_SUPER将被完全忽略。

This is a flag that can be set on a class and specifies how a specific corner case of the invokespecial bytecode is handled for this class. It is set by all modern Java compilers (where "modern" is >= Java 1.1, if I remember correctly) and only ancient Java compilers produced class files where this was un-set. This flag exists only for backwards-compatibility reasons. Note that starting with Java 7u51, ACC_SUPER is ignored completely due to security reasons.

jsr / ret bytecodes。

The jsr/ret bytecodes.

这些字节码用于实现子例程(主要用于实现最后块)。它们是自Java 6以来不再生成 。他们弃用的原因是它们使静态验证复杂化并没有太大的好处(即使用的代码几乎总是可以通过正常的跳转重新实现而且开销非常小)。

These bytecodes were used to implement sub-routines (mostly for implementing finally blocks). They are no longer produced since Java 6. The reason for their deprecation is that they complicate static verification a lot for no great gain (i.e. code that uses can almost always be re-implemented with normal jumps with very little overhead).

在类中只有两个方法只有不同的返回类型。

Having two methods in a class that only differ in return type.

Java语言规范不允许同一个类中的两个方法不同返回类型中只有 (即同名,相同的参数列表,......)。但是,JVM规范没有这样的限制,因此类文件可以包含两个这样的方法,使用普通的Java编译器无法生成这样的类文件。 此答案中有一个很好的示例/解释。

The Java language specification does not allow two methods in the same class when they differ only in their return type (i.e. same name, same argument list, ...). The JVM specification however, has no such restriction, so a class file can contain two such methods, there's just no way to produce such a class file using the normal Java compiler. There's a nice example/explanation in this answer.

这篇关于字节码功能在Java语言中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆