浅谈Java编程中的synthetic关键字
作者:mengwei 发布时间:2022-01-27 18:48:41
java synthetic关键字。有synthetic标记的field和method是class内部使用的,正常的源代码里不会出现synthetic field。小颖编译工具用的就是jad.所有反编译工具都不能保证完全正确地反编译class。所以你不能要求太多。
下面我给大家介绍一下synthetic
下面的例子是最常见的synthetic field
Java代码
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里被编译成
Java代码
if(!$assertionsDisabled && !condition){
throw new AssertionError();
}
还有,在jvm里,所有class的私有成员都不允许在其他类里访问,包括它的inner class。在java语言里inner class是可以访问父类的私有成员的。在class里是用如下的方法实现的:
Java代码
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思想中的类的复合,也就是只要含有其它类的引用即为复合。
来源:https://www.2cto.com/kf/201703/618626.html


猜你喜欢
- android中提供了4中动画: AlphaAnimation 透明度动画效果 ScaleAnimation 缩放动画效果 Translat
- 一、什么是默认方法,为什么要有默认方法简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法。只需在方法名前面加个default关键字
- 1、心跳机制简介在分布式系统中,分布在不同主机上的节点需要检测其他节点的状态,如服务器节点需要检测从节点是否失效。为了检测对方节点的有效性,
- 这篇文章主要介绍了springmvc如何使用POJO作为参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 什么是WebSocket?WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信—
- 思路:1.安装插件:安装log4net2.使用配置:添加log4net.config配置文件3.输出日志文件格式:添加日志配置4.Assem
- 分页是Java Web项目常用的功能,昨天在Spring MVC中实现了简单的分页操作和搜索分页,在此记录一下。使用的框架为(MyBatis
- jasperreport导出的pdf每页显示的记录太少主要是确保Details的高度与Details中Field Text的高度一致。jas
- 本文实例讲述了C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题。分享给大家供大家参考,具体如下:一、理论定义中介者模式&nb
- 本文实例讲述了java实现MD5加密的方法。分享给大家供大家参考,具体如下:private String getMD5Str(String
- SpringBoot使用Commons Logging进行所有内部日志记录,但保留底层日志实现。默认提供了Java Util Logging
- 前文传送门:Netty分布式FastThreadLocal的set方法实现逻辑剖析recycler的使用这一小节开始学习recycler相关
- 本文实例讲述了Android编程实现应用强制安装到手机内存的方法。分享给大家供大家参考,具体如下:在Froyo(android 2.2,AP
- 在Java编程过程中,我们常常会遇到比较基本类型或者对象之间的大小关系,下面我们来看看怎么去比较。源码如下:package object;c
- 一个围绕统计分析功能的系统,在最后制作统计分析时需要一个批量点击的功能,用以批量制作echarts图形后生成图片并保存图形和图片。方便后续导
- static void Main(string[] args) &nb
- 一、简介在现阶段的Android开发中,注解越来越流行起来,比如ButterKnife,Retrofit,Dragger,EventBus等
- 目录通过Resource接口手动加载通过@Value自动转换通过ResourceLoader加载使用ResourceUtils加载资源读取资
- FileStream缓冲读取和写入可以提高性能。FileStream读取文件的时候,是先将流放入内存,经Flush()方法后将内存中(缓冲中
- Android音乐播放器的运行效果这篇博客还是接着上一篇Android音乐播放器制作写的,没看过的可以去看看。其中这个效果(圆形ImageV