必威体育Betway必威体育官网
当前位置:首页 > IT技术

Java中的synthetic

时间:2019-08-17 16:44:37来源:IT技术作者:seo实验室小编阅读:82次「手机版」
 

synthetic

转载自  java中的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 就像下面这样: 

java代码

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的概念 

According to the JVM Spec: "A class member that does not APPear in the source code must be marked using a Synthetic attribute." Also, "The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces."  

I know that nested classes are sometimes implemented using synthetic fields and synthetic contructors, e.g. an inner class may use a synthetic field to save a reference to its outer class instance, and it may generate a synthetic contructor to set that field correctly. I'm not sure if it Java still uses synthetic constructors or methods for this, but I'm pretty sure I did see them used in the past. I don't know why they might need synthetic classes here. On the other hand, something like RMI or java.lang.reflect.Proxy should probably create synthetic classes, since those classes don't actually appear in source code. I just ran a test where Proxy did not create a synthetic instance, but I believe that's probably a bug. 

Hmm, we discussed this some time ago back here. It seems like Sun is just ignoring this synthetic attribute, for classes at least, and we should too. 

注意上文的第一处黑体部分,一个类的复合属性表示他支持嵌套的类或者接口 

注意上文的第二处黑体部分,说明符合这个概念就是OO思想中的类的复合,也就是只要含有其它类的引用即为复合。

相关阅读

java基础入门(一)

前言: 1. 笔者的java没有经过真正系统的学习过,只是跟着书上自学的。所以有些地方难免会理解错误之类的,如果看到错误的地方,请指出

Java语言介绍

目录 一、引入 二、Java简介 1.Java语言的发展历程 2.Java语言的特点 3.Java语言的编译解释性 三、环境搭建 1.Dos命令 2.环境变

java生成身份证号码,并将结果保存生成txt文件

代码可直接运行,当前是生成2000个身份证号码到 "E:\\IdCardGenerator.txt",可以主函数里根据自己的需求更改。 用notepad++打开

Javaweb学习笔记3—Serverlet

今天来讲javaweb的第三个阶段学习。老规矩,首先先用一张思维导图来展现今天的博客内容。ps:我的思维是用的xMind画的,如果你对我的

ltp的JAVA调用方法

LTP4J的编译 1.从github中下载ltp4j源文件。在这里我使用DownGit作为下载工具,将ltp4j的github链接输入DownGit的输入框,如图: 2.下

分享到:

栏目导航

推荐阅读

热门阅读