shr*_*000 6 java methods volatile
这个很奇怪.我有以下代码:
class A
{
protected A clone() throws CloneNotSupportedException
{
return (A) super.clone();
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过'showmycode.com'解码它的字节码时,它向我展示了以下代码:
class A
{
A()
{
}
protected A clone()
throws clonenotsupportedexception
{
return (A)super.clone();
}
protected volatile object clone()
throws clonenotsupportedexception
{
return clone();
}
}
Run Code Online (Sandbox Code Playgroud)
在第二个'clone'方法中,方法返回类型是volatile的意思是什么?(此代码是通过Eclipse的默认JDK 1.6编译器编译的).
这个问题的答案已经在为什么在java中使方法易变? 但这里有更多信息.
当重载方法(可能只是超类中的泛型方法)时,该方法被标记为"桥接方法".来自java.lang.reflect.Modifier:
static final int BRIDGE = 0x00000040;
Run Code Online (Sandbox Code Playgroud)
不幸的是,这与用于将字段标记为以下内容的位相同volatile:
public static final int VOLATILE = 0x00000040;
Run Code Online (Sandbox Code Playgroud)
如果在该方法上打印修改器,您将看到如下内容:
public volatile
Run Code Online (Sandbox Code Playgroud)
这是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);
}
Run Code Online (Sandbox Code Playgroud)
字段和方法的修饰符掩码类似,但不完全相同。反编译器很可能使用的是toString这里的方法
但它不处理所有位
// 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
Run Code Online (Sandbox Code Playgroud)
它不处理的是可以表示synthetic和bridge识别编译器生成的代码的位。
如果volatile这里有任何意义,它可能意味着不要删除该方法,即使它不执行任何操作。
| 归档时间: |
|
| 查看次数: |
6908 次 |
| 最近记录: |