我总结了最近在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