Valhalla项目:LW2内联类型的初步了解

我总结了最近在Valhalla LW2 项目 “ 内联类型 ”中取得的一些进展,这些进展最近在我的博客文章“ Valhalla LW2进度-内联类型 ”中公开了。 在这篇文章中,我通过针对最近发布的Valhalla Early Access Build jdk-14-valhalla + 1-8(2019/7/4)执行的代码示例来说明该文章中概述的一些概念。 这篇文章中介绍的所有代码示例都可以在GitHub上找到 。

OpenJDK Wiki页面“ LW2 ”通过名为“ InlineType ”的类的源代码提供了内联类型的说明性示例。 我的示例对该类进行了一些较小的修改和补充,并在GitHub上作为名为InlineTypeExample的类InlineTypeExample 。 在查看此源代码时,一些立即引起注意的项目是关键字inline的存在和?的存在?Comparable的通用参数中。

我改编的InlineTypeExample类的源代码试图将内联类型类extend另一个类而被注释掉,因为这会导致编译器错误: error: Inline type may not extend another inline type or class

同样,该源代码也具有尝试设置注释掉内联类型类的整数字段的方法,因为该方法也无法编译: error: cannot assign a value to final variable

使用当前的Valhalla LW2构建,可以使我的内联类型类可序列化 ,并且仍然可以成功编译。

另一个由GitHub托管的说明性类是Lw2Demonstration ,该类将内联类型类(及其实例)的特性与JDK提供的java.lang.Integer类(及其实例)以及简单的定制构建的Integer包装器进行比较和对比。及其实例)。 该演示类对所有三样东西(内联类型, Integer和自定义Integer包装器)的“ 类 ”类型调用反射方法(某些基于JDK 14的Valhalla构建是新方法),并调用一些“通用”方法[ toString () , equals(Object) , hashCode() ]应用于所有三种类型的实例。

在类Lw2Demonstration中,注释了两个方法,因为它们每个都尝试对内联类型不支持的内联类型执行功能。 这些方法之一尝试在嵌入式类型的变量上进行同步 。 尝试编译此内联类型的同步时,会看到以下编译器错误消息: error: unexpected type ... required: reference ... found: InlineTypeExample

另一个尝试将内联类型分配给null 。 尝试编译此错误时,遇到以下错误消息: error: incompatible types: <null> cannot be converted to InlineTypeExample

Lw2Demonstration的以下方法写出了类类型的一些元数据特征。

/*** Provides metadata extracted from the provided instance of* {@link Class} as a single {@link String}.** @param classToInvokeInlineMethodsOn Class for which metadata*    is to be extracted and returned in {@link String} format;*    should NOT be {@code null}.* @return Single string representation of metadata extracted*    from the provided {@link Class} instance.* @throws NullPointerException Thrown if {@code null} is*    provided for my sole parameter.*/
public static String extractClassMetadata(final Class classToInvokeInlineMethodsOn)
{Objects.requireNonNull("Provided Class must be non-null to extract its metadata.");final String className = classToInvokeInlineMethodsOn.getSimpleName();final String outputPrefix = "\n" + className + ".class.";return outputPrefix + "getName(): " + classToInvokeInlineMethodsOn.getName()+ outputPrefix + "getSimpleName(): " + classToInvokeInlineMethodsOn.getSimpleName()+ outputPrefix + "getCanonicalName(): " + classToInvokeInlineMethodsOn.getCanonicalName()+ outputPrefix + "toGenericString(): " + classToInvokeInlineMethodsOn.toGenericString()+ outputPrefix + "getTypeName(): " + classToInvokeInlineMethodsOn.getTypeName()+ outputPrefix + "getComponentType(): " + classToInvokeInlineMethodsOn.getComponentType()+ outputPrefix + "isInlineClass(): " + classToInvokeInlineMethodsOn.isInlineClass()+ outputPrefix + "isIndirectType(): " + classToInvokeInlineMethodsOn.isIndirectType()+ outputPrefix + "isNullableType(): " + classToInvokeInlineMethodsOn.isNullableType()+ outputPrefix + "isPrimitive(): " + classToInvokeInlineMethodsOn.isPrimitive()+ outputPrefix + " final?: " + isFinal(classToInvokeInlineMethodsOn);
}

在以前的方法中,在Class实例上调用的某些方法是基于JDK 14的Valhalla LW2早期访问构建的新增方法。 这些包括isInlineClass()isIndirectType()isNullableType()

主要的演示类Lw2Demonstration创建内联类型类InlineTypeExample ,JDK提供的java.lang.Integer以及Integer的自定义包装器的实例。 然后,演示通过相同的方法运行这三个类和类定义的实例,并为每个结果写出结果,以便可以对它们进行比较和对比。 这是针对本文开头提到的Valhalla Early Access Build运行此示例的输出。

InlineTypeExample.class.getName(): dustin.examples.valhalla.lw2.InlineTypeExample
InlineTypeExample.class.getSimpleName(): InlineTypeExample
InlineTypeExample.class.getCanonicalName(): dustin.examples.valhalla.lw2.InlineTypeExample
InlineTypeExample.class.toGenericString(): public final inline class dustin.examples.valhalla.lw2.InlineTypeExample
InlineTypeExample.class.getTypeName(): dustin.examples.valhalla.lw2.InlineTypeExample
InlineTypeExample.class.getComponentType(): null
InlineTypeExample.class.isInlineClass(): true
InlineTypeExample.class.isIndirectType(): false
InlineTypeExample.class.isNullableType(): false
InlineTypeExample.class.isPrimitive(): false
InlineTypeExample.class. final?: true
InlineTypeExample: toString(): [dustin.examples.valhalla.lw2.InlineTypeExample someIntegerValue=1]
InlineTypeExample: hashCode(): 1303372796
Inline Type Example ==: trueInteger.class.getName(): java.lang.Integer
Integer.class.getSimpleName(): Integer
Integer.class.getCanonicalName(): java.lang.Integer
Integer.class.toGenericString(): public final class java.lang.Integer
Integer.class.getTypeName(): java.lang.Integer
Integer.class.getComponentType(): null
Integer.class.isInlineClass(): false
Integer.class.isIndirectType(): true
Integer.class.isNullableType(): true
Integer.class.isPrimitive(): false
Integer.class. final?: true
Integer: toString(): 1
Integer: hashCode(): 1
Integer Type Example ==: falseIntegerWrapper.class.getName(): dustin.examples.valhalla.lw2.IntegerWrapper
IntegerWrapper.class.getSimpleName(): IntegerWrapper
IntegerWrapper.class.getCanonicalName(): dustin.examples.valhalla.lw2.IntegerWrapper
IntegerWrapper.class.toGenericString(): public class dustin.examples.valhalla.lw2.IntegerWrapper
IntegerWrapper.class.getTypeName(): dustin.examples.valhalla.lw2.IntegerWrapper
IntegerWrapper.class.getComponentType(): null
IntegerWrapper.class.isInlineClass(): false
IntegerWrapper.class.isIndirectType(): true
IntegerWrapper.class.isNullableType(): true
IntegerWrapper.class.isPrimitive(): false
IntegerWrapper.class. final?: false
IntegerWrapper: toString(): dustin.examples.valhalla.lw2.IntegerWrapper@5442a311
IntegerWrapper: hashCode(): 1413653265
Integer Wrapper Example ==: false

上面显示的输出演示了内联类型的一些广告特性。 最有趣的是下表的重点。

特性 内联类型包装整数 java.lang.Integer 自定义整数包装器
排队? 真正
间接? 真正 真正
可空吗? 真正 真正
最后? 真正 真正
==对平等有效吗? 真正
toString() 隐式定制 明确定制 使用Object的
hashCode() 隐式定制 明确定制 使用Object的

为了编译和执行这些示例,我需要为Java编译器和启动器提供一些特殊的参数。 具体来说,我使用--enable-preview-Xlint:preview-source 14编译。 为了执行演示,我将标志--enable-preview传递给Java启动器。

更新的Valhalla Early Access Build [ Build jdk-14-valhalla + 1-8(2019/7/4) ]为有兴趣尝试Valhalla LW2原型内联类型的Java开发人员提供了一个方便的预构建二进制文件。 这篇文章使用此构建演示了一些当前的LW2内联类型概念。 RémiForax在GitHub( forax / valuetype-lworld )上提供了更多示例。

翻译自: https://www.javacodegeeks.com/2019/07/project-valhalla-first-look-lw2-inline-types.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/341291.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

QCOW2镜像格式

QEMU copy-on-write format with a range of special features, including the ability to take multiple snapshots, smaller images on filesystems that don’t support sparse files, optional AES encryption, and optional zlib compression 现在比较主流的一种虚拟化镜…

for循环简介及实例(输出九九乘法表)

for循环简介 简介 for循环是3大循环其中一种&#xff0c;相比while循环和do while循环&#xff0c;它更加灵活&#xff0c;而且完全包含了while循环的功能&#xff0c;用for语句可以解决编程中所有循环问题。 语法格式 for(语句1;表达式1;表达式2) 语句 //语句1一般用于设置…

Windows下使用net user命令管理账户

准备工作-以管理员身份运行命令提示符&#xff08;cmd&#xff09; 鼠标左键单击Windos标志或者按下键盘Windows键&#xff0c;下滑菜单&#xff0c;在Windows系统 中找到命令提示符。 2.右键&#xff0c;更多->以管理员身份运行 net user 命令用法 1.查看所有用户 net…

Java中带有NetSuite数据实体的对象关系映射(ORM)

对象关系映射&#xff08;ORM&#xff09;技术使使用关系数据源更容易&#xff0c;并且可以将逻辑业务模型与物理存储模型联系在一起。 遵循本教程&#xff0c;将与NetSuite数据的连接集成到基于Java的ORM框架Hibernate中。 您可以使用Hibernate将面向对象的域模型映射到传统的…

vmware中ubuntu虚拟机扩容

两种扩容方式&#xff1a; 重新创建一块虚拟硬盘 扩大原来的硬盘&#xff1a;如果装系统时没有进行手动分区&#xff0c;系统只有两个分区&#xff08;根分区和交换分区&#xff09;&#xff0c;这种情况我们直接扩展根分区的大小。 我们是为了解决当前用户空间不够的问题&…

pat 乙级 1021 个位数统计(C++)

题目 给定一个 k 位整数 Nd​k−1​​10​k−1​​⋯d​1​​10​1d​0(0≤d​i≤9, i0,⋯,k−1, d​k−1>0)&#xff0c;请编写程序统计每种不同的个位数字出现的次数。例如&#xff1a;给定 N100311&#xff0c;则有 2 个 0&#xff0c;3 个 1&#xff0c;和 1 个 3。 输…

该虚拟机似乎正在使用

该虚拟机似乎正在使用 点击获取所有权&#xff0c;此时虚拟机还是不能打开。 打开Vmware虚拟机虚拟磁盘文件和配置文件存放的位置 删除后缀为.lck的文件夹 然后开启此虚拟机就可以啦

PAT 乙级(Basic Level) 题解汇总(持续更新)(C++)

前言 为了准备3月份的CCF CSP认证&#xff0c;以及提升自己的编程能力和数据结构与算法基础&#xff0c;目前我坚持每天刷pat乙级题库&#xff0c;然后记录在CSDN。一则&#xff0c;希望夯实我做过的题和学到的东西&#xff1b;二则&#xff0c;希望对和我一样的小伙伴有些许帮…

反射是最重要的Java API

前几天我在想-这是最重要的Java API。 哪种SE和EE API可以使大多数Java生态系统成为可能&#xff0c;而哪些API不能刚刚被重新创建为第三方库。 正如您可能已经猜到标题一样&#xff0c;我认为它是Reflection API 。 是的&#xff0c;它不可避免地是每个项目的直接或间接的一部…

Wireshark常用过滤使用方法

过滤源ip、目的ip。 在wireshark的过滤规则框Filter中输入过滤条件。如查找目的地址为192.168.101.8的包&#xff0c;ip.dst192.168.101.8&#xff1b;查找源地址为ip.src1.1.1.1 端口过滤。 如过滤80端口&#xff0c;在Filter中输入&#xff0c;tcp.port80&#xff0c;这条规…

pat 乙级 1003 我要通过!(C++)

题目 “答案正确"是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件&#xff0c;系统就输出“答案正确”&#xff0c;否则输出"答案错误”。 得到"答案正确"的条件是&#xff1a; 字符串中必…

【C】printf按8进制、10进制、16进制输出以及高位补0

#include <iostream> #include <iomanip> #include "Circle.h" // Circle class declaration file int main() { int PrintVal 9;/*按整型输出&#xff0c;默认右对齐*/printf("%d\n",PrintVal);/*按整型输出&#xff0c;补齐4位的宽度&#…

pat 乙级 1005 继续(3n+1)猜想(C++)

题目 卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里&#xff0c;情况稍微有些复杂。 当我们验证卡拉兹猜想的时候&#xff0c;为了避免重复计算&#xff0c;可以记录下递推过程中遇到的每一个数。例如对 n3 进行验证的时候&#xff0c;我们需要计算 3、5、8、4、…

带有Spring Boot和Spring Cloud的Java微服务

朋友不允许朋友写用户身份验证。 厌倦了管理自己的用户&#xff1f; 立即尝试Okta的API和Java SDK。 在几分钟之内即可对任何应用程序中的用户进行身份验证&#xff0c;管理和保护。 Java是开发微服务架构时使用的一种很棒的语言。 实际上&#xff0c;我们行业中的一些知名人士…

pat 乙级 1018 锤子剪刀布(C++)

题目 两人玩锤子剪刀布&#xff0c;现给出两人的交锋记录&#xff0c;请统计双方的胜、平、负次数&#xff0c;并且给出双方分别出什么手势的胜算最大。 输入格式&#xff1a; 输入第 1 行给出正整数 N&#xff08;≤105 &#xff09;&#xff0c;即双方交锋的次数。随后 N …

Terminal中输入一行命令快速移动光标至行首行尾

Linux&#xff1a;### ①快速移动光标至行首 **Home或CtrlA ** ②快速移动光标至行尾 **End或CtrlE ** ③从光标处开始删除&#xff0c;直到行尾 **CtrlK ** ④到下一行 **CtrlN 或 方向键&#xff1a;↓ ** ⑤到上一行 **CtrlP 或 方向键&#xff1a;↑ **

nio2和nio2_列出和过滤NIO.2中的目录内容

nio2和nio2在Java 7发行之前&#xff0c;列出目录内容的领域并没有发生太多事情。但是&#xff0c;由于NIO.2引入了一种新的方法来做到这一点&#xff0c;因此覆盖这一领域可能是值得的。 NIO.2的一大优点是能够在一个方法调用中立即使用列表和过滤。 这为与文件系统相关的大多…

pat 乙级 1033 旧键盘打字(C++)

题目 旧键盘上坏了几个键&#xff0c;于是在敲一段文字的时候&#xff0c;对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键&#xff0c;打出的结果文字会是怎样&#xff1f; 输入格式&#xff1a; 输入在 2 行中分别给出坏掉的那些键、以及应该输入的文…

修改typora主题的字体

简 述&#xff1a; 在 mac 中修改 typora 主题的英文和中文的字体&#xff0c;使得码字更加舒服&#xff08;win 也有效&#xff09;。 [TOC] 本文初发于 “偕臧的小站“&#xff0c;同步转载于此。 书写环境&#xff1a; &#x1f4bb;&#xff1a; MacOS 10.14.6 &#x1…