Synthetic 规格严格

有synthetic标记的field和method是class内部使用的,正常的源代码里不会出现synthetic field。小颖编译工具用的就是jad.所有反编译工具都不能保证完全正确地反编译class。所以你不能要求太多。
下面我给大家介绍一下synthetic

下面的例子是最常见的synthetic field
class parent
{
public void foo()
{
}
class inner
{
inner()
{
foo();
}
}
}
非static的inner class里面都会有一个this$0的字段保存它的父对象。编译后的inner class 就像下面这样:
class parent$inner
{
synthetic parent this$0;
parent$inner(parent this$0)
{
this.this$0 = this$0;
this$0.foo();
}
}
所有父对象的非私有成员都通过 this$0来访问。

还有许多用到synthetic的地方。比如使用了assert 关键字的class会有一个
synthetic static boolean $assertionsDisabled 字段
使用了assert的地方
assert condition;
在class里被编译成
if(!$assertionsDisabled && !condition)
{
throw new AssertionError();
}

还有,在jvm里,所有class的私有成员都不允许在其他类里访问,包括它的inner class。在java语言里inner class是可以访问父类的私有成员的。在class里是用如下的方法实现的:
class parent
{
private int value = 0;
synthetic static int access$000(parent obj)
{
return value;
}
}
在inner class里通过access$000来访问value字段。

希望通过上面几个例子,大家对synthetic 有所了解。


来自:

http://www.cjsdn.net/post/print?bid=1&id=130784
http://www.cnblogs.com/keis/archive/2011/03/10/1979760.html

原文地址:https://www.cnblogs.com/diyunpeng/p/2379943.html