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 ==: true

Integer.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 ==: false

IntegerWrapper.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值