这个很奇怪。我有以下代码:
class A
{
protected A clone() throws CloneNotSupportedException
{
return (A) super.clone();
}
}当我通过“showmycode.com”反编译它的字节码时,它向我展示了以下代码:
class A
{
A()
{
}
protected A clone()
throws clonenotsupportedexception
{
return (A)super.clone();
}
protected volatile object clone()
throws clonenotsupportedexception
{
return clone();
}
}在第二个“clone”方法中,方法返回类型是易失性的意味着什么?(这段代码是通过Eclipse的默认JDK 1.6编译器编译的)。
发布于 2012-04-29 16:21:10
字段和方法的修饰符掩码相似,但并不完全相同。在这里,反编译器很可能使用toString方法
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/reflect/Modifier.java
但它不能处理所有的比特
// Bits not (yet) exposed in the public API either because they
// have different meanings for fields and methods and there is no
// way to distinguish between the two in this class, or because
// they are not Java programming language keywords它不处理的是位,这些位可以表示标识编译器生成的代码的synthetic和bridge。
如果volatile在这里有任何意义,它可能意味着即使该方法没有做任何事情,也不要删除它。
发布于 2014-01-14 06:15:22
问题Why make a method volatile in java?中已经介绍了这个答案,但这里有更多信息。
当您重载方法(可能只是超类中的泛型方法)时,该方法被标记为"bridge method"。来自java.lang.reflect.Modifier
static final int BRIDGE = 0x00000040;不幸的是,这与用于将字段标记为volatile的位相同
public static final int VOLATILE = 0x00000040;如果您在该方法上打印修饰符,您将看到类似以下内容:
public volatile这是Modifiers.toString(int)方法的一个限制,它不知道它是字段还是方法。
public static String toString(int mod) {
StringBuffer sb = new StringBuffer();
...
if ((mod & VOLATILE) != 0) sb.append("volatile ");
// no mention of BRIDGE here
...
return sb.toString().substring(0, len-1);
}发布于 2012-04-29 14:38:48
这并不意味着什么。这是反编译器中的一个错误。故事到此结束。
( bug可能与类文件格式中使用的某些标志位是“重载”的事实有关,在类、字段或方法的上下文中表示不同的东西。我还隐约记得在最近的JVM规范修订中有一些“新用途”。)
https://stackoverflow.com/questions/10370033
复制相似问题