Java 12 中引入 JVM 常量 API,用来更容易地对关键类文件 (key class-file) 和运行时构件(artefact)的名义描述

(nominal description) 进行建模,特别是对那些从常量池加载的常量,这是一项非常技术性的变化,能够以更简

单、标准的方式处理可加载常量。

具体来说就是java.base模块新增了java.lang.constant包(而非 java.lang.invoke.constant )。包中定义了一系列基于值的符号引用(JVMS 5.1)类型,它们能够描述每种可加载常量。

官方api链接地址:

http://cr.openjdk.java.net/~iris/se/12/latestSpec/api/java.base/java/lang/constant/pack
age-summary.html Java SE > Java SE Specifications > Java Virtual Machine Specification下的第5章:
Chapter 5. Loading, Linking, and Initializing https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html

引入了ConstantDesc接口( ClassDesc、MethodTypeDesc、MethodHandleDesc这几个接口直接继承了ConstantDesc接口)以及Constable接口;ConstantDesc接口定义了resolveConstantDesc方法,Constable接口定义了describeConstable方法;String、Integer、Long、Float、Double均实现了这两个接口,而EnumDesc实现了ConstantDesc接口。

符号引用以纯 nominal 形式描述可加载常量,与类加载或可访问性上下文区分开。有些类可以作为自己的符号引用(例如 String)。而对于可链接常量,另外定义了一系列符号引用类型,具体包括: ClassDesc (Class 的可加载常量标称描述符) ,MethodTypeDesc(方法类型常量标称描述符) ,MethodHandleDesc (方法句柄常量标称描述符) 和DynamicConstantDesc (动态常量标称描述符) ,它们包含描述这些常量的 nominal 信息。此 API 对于操作类和方法的工具很有帮助。

String 实现了 Constable 接口:
public final class String implements java.io.Serializable, Comparable<String>,
CharSequence,Constable, ConstantDesc {

java.lang.constant.Constable接口定义了抽象方法:

public interface Constable {
Optional<? extends ConstantDesc> describeConstable();
}

Java 12 String 的实现源码:

@Override
public Optional<String> describeConstable() {
return Optional.of(this);
}

很简单,其实就是调用 Optional.of 方法返回一个 Optional 类型。

String#describeConstable和resolveConstantDesc

新引入的接口java.lang.constant.Constable - 它用于标记constable类型,这意味着这类型的值是常量,可以在JVMS 4.4常量池中定义。

Java SE > Java SE Specifications > Java Virtual Machine Specification下的第4章:

Chapter 4. The class File Format

String的源码:

/**
* Returns an {@link Optional} containing the nominal descriptor for this
* instance, which is the instance itself.
*
* @return an {@link Optional} describing the {@linkplain String} instance
* @since 12
*/
@Override
public Optional<String> describeConstable() {
return Optional.of(this);
}
/**
* Resolves this instance as a {@link ConstantDesc}, the result of which is
* the instance itself.
*
* @param lookup ignored
* @return the {@linkplain String} instance
* @since 12
*/
@Override
public String resolveConstantDesc(MethodHandles.Lookup lookup) {
return this;
}
private static void testDescribeConstable() {
System.out.println("======test java 12 describeConstable======");
String name = "Java高级工程师";
Optional<String> optional = name.describeConstable();
System.out.println(optional.get());
}

最新文章

  1. 搭建LNAMP环境(六)- PHP7源码安装MongoDB和MongoDB拓展
  2. 说说我的企业级应用上线历程(A little different!)
  3. sublime配置coffeeScript
  4. PPC MPC85xx e500学习笔记
  5. WebForm 基础
  6. Stream 和 byte[]
  7. POJ 1016
  8. iPhone/iPad/Android UI尺寸规范
  9. PHP使用phpexcel读取excel文件
  10. Yii Listview 更新及搜索
  11. Git (2)
  12. 服务器忘记mysql密码怎么修改?
  13. 翻译器DIY它———算在英文文本中的单词数,字符和行数
  14. elasticsearch的percolator操作
  15. 逆FizzBuzz问题求最短序列
  16. [Swift]LeetCode111. 二叉树的最小深度 | Minimum Depth of Binary Tree
  17. 同源策略jsonp和cors
  18. linux下目录简介——/proc
  19. plot 函数常用参数
  20. 解决Fiddler抓不到HTPPS

热门文章

  1. Web数据库架构
  2. Gtest:死亡测试
  3. xshell连接linux使用vim无法正常使用小键盘
  4. thymeleaf教程-springboot项目中实现thymeleaf自定义标签
  5. 实验之RSTP基础配置
  6. 21.centos7基础学习与积累-007-远程连接
  7. 使用Apollo做配置中心
  8. 基于Java+Selenium的WebUI自动化测试框架(一)---页面元素定位器
  9. moviepy这个版本,除了字幕,基本可用
  10. 11.vue-router编程式导航