前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >isSynthetic

isSynthetic

作者头像
阿超
发布2022-08-16 18:56:33
7770
发布2022-08-16 18:56:33
举报
文章被收录于专栏:快乐阿超

巧诈不如拙诚。——韩非子

isSynthetic这个函数,在Class类中存在,在Field类中存在,一搜,发现还挺多地方都有这个函数

image-20210126212938785
image-20210126212938785

这个函数我们点进去看源码和注释

代码语言:javascript
复制
/**
 * Returns {@code true} if this class is a synthetic class;
 * returns {@code false} otherwise.
 * @return {@code true} if and only if this class is a synthetic class as
 *         defined by the Java Language Specification.
 * @jls 13.1 The Form of a Binary
 * @since 1.5
 */
public boolean isSynthetic() {
    return (getModifiers() & SYNTHETIC) != 0;
}
代码语言:javascript
复制
/**
 * Returns {@code true} if this field is a synthetic
 * field; returns {@code false} otherwise.
 *
 * @return true if and only if this field is a synthetic
 * field as defined by the Java Language Specification.
 * @since 1.5
 */
public boolean isSynthetic() {
    return Modifier.isSynthetic(getModifiers());
}

发现Class的和Field中的还不一样,一个是说

如果此类是综合类,则返回true,否则返回false 当且仅当此类是Java语言规范定义的综合类时,才返回true

另一个

如果此字段是合成字段,则返回true,否则返回false 当且仅当此字段是Java语言规范定义的合成字段时,才返回true

什么意思呢?

很简单,如果这个是通过编译生成的类(字段),就返回true

举几个很常见的例子,首先定义一个枚举

代码语言:javascript
复制
private enum Ruben {
}

然后获取它的$VALUES字段,调用isSynthetic函数,打印结果

代码语言:javascript
复制
System.out.println(Ruben.class.getDeclaredField("$VALUES").isSynthetic());

输出结果为true,我们知道枚举是在编译后会生成$VALUES字段,所以它是一个合成字段

还有类的,例如我们常见的lambda

代码语言:javascript
复制
System.out.println(((Consumer) (c) -> System.gc()).getClass().isSynthetic());
System.out.println(((Supplier) System::nanoTime).getClass().isSynthetic());
System.out.println(((Predicate) (p) -> Boolean.FALSE).getClass().isSynthetic());
System.out.println(((Function) (f) -> "ruben").getClass().isSynthetic());
System.out.println(((Runnable) new Runnable() {
    @Override
    public void run() {
    }
}).getClass().isSynthetic());
System.out.println(((Runnable) System::nanoTime).getClass().isSynthetic());

lambda在编译时生成匿名内部类,所以使用lambda属于我们的合成类

执行结果为

image-20210126214200674
image-20210126214200674

可以明显看出,倒数第二个我们自己写了一个匿名内部类,编译没有生成,所以它不是合成类

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-01-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档