java 保留关键字,Java类文件可以使用保留关键字作为名称吗?

I'm aware that Java-the-compilable-programming-language is not one and the same as Java-the-bytecode-format-for-JVM-execution. There are examples of things that are valid in the .class format but not in the .java source code, such as constructor-less classes and synthetic methods.

If we hand-craft a .class file with a reserved Java language keyword (e.g. int, while) as the class, method, or field name, will the Java virtual machine accept it for loading?

If the class is loaded, does it imply that the only way to access this class or member is through Java reflection, because the name is syntactically illegal in the Java programming language?

解决方案

The only restrictions on class names at the bytecode level are that they can't contain the characters [, . or ; and that they're at most 65535 bytes long. Among other things, this means that you can freely use reserved words, whitespace, special characters, Unicode, or even weird stuff like newlines.

You can theoretically even use null characters in a class name, but since it's impossible to have a null character in the filename, you can't include such a classfile in a jar. You might be able to create and load one dynamically though.

Here's an example of some of the things that you can do (written in Krakatau assembly):

; Entry point for the jar

.class Main

.super java/lang/Object

.method public static main : ([Ljava/lang/String;)V

.limit stack 10

.limit locals 10

invokestatic int hello ()V

invokestatic "-42" hello ()V

invokestatic "" hello ()V

invokestatic " some whitespace and \t tabs" hello ()V

invokestatic "new\nline" hello ()V

invokestatic 'name with "Quotes" in it' hello ()V

return

.end method

.end class

.class int

.super java/lang/Object

.method public static hello : ()V

.limit stack 2

.limit locals 0

getstatic java/lang/System out Ljava/io/PrintStream;

ldc "Hello from int"

invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V

return

.end method

.end class

.class "-42"

.super java/lang/Object

.method public static hello : ()V

.limit stack 2

.limit locals 0

getstatic java/lang/System out Ljava/io/PrintStream;

ldc "Hello from -42"

invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V

return

.end method

.end class

; Even the empty string can be a class name!

.class ""

.super java/lang/Object

.method public static hello : ()V

.limit stack 2

.limit locals 0

getstatic java/lang/System out Ljava/io/PrintStream;

ldc "Hello from "

invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V

return

.end method

.end class

.class " some whitespace and \t tabs"

.super java/lang/Object

.method public static hello : ()V

.limit stack 2

.limit locals 0

getstatic java/lang/System out Ljava/io/PrintStream;

ldc "Hello from some whitespace and \t tabs"

invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V

return

.end method

.end class

.class "new\nline"

.super java/lang/Object

.method public static hello : ()V

.limit stack 2

.limit locals 0

getstatic java/lang/System out Ljava/io/PrintStream;

ldc "Hello from new\nline"

invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V

return

.end method

.end class

.class 'name with "Quotes" in it'

.super java/lang/Object

.method public static hello : ()V

.limit stack 2

.limit locals 0

getstatic java/lang/System out Ljava/io/PrintStream;

ldc "Hello from name with \"Quotes\" in it"

invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V

return

.end method

.end class

Execution output:

Hello from int

Hello from -42

Hello from

Hello from some whitespace and tabs

Hello from new

line

Hello from name with "Quotes" in it

See Holger's answer for the exact quote of the rules from the JVM specification.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值