Valhalla项目:LW2内联类型的初步了解
我總結(jié)了最近在Valhalla LW2 項(xiàng)目 “ 內(nèi)聯(lián)類型 ”中取得的一些進(jìn)展,這些進(jìn)展最近在我的博客文章“ Valhalla LW2進(jìn)度-內(nèi)聯(lián)類型 ”中公開了。 在這篇文章中,我通過(guò)針對(duì)最近發(fā)布的Valhalla Early Access Build jdk-14-valhalla + 1-8(2019/7/4)執(zhí)行的代碼示例來(lái)說(shuō)明該文章中概述的一些概念。 這篇文章中介紹的所有代碼示例都可以在GitHub上找到 。
OpenJDK Wiki頁(yè)面“ LW2 ”通過(guò)名為“ InlineType ”的類的源代碼提供了內(nèi)聯(lián)類型的說(shuō)明性示例。 我的示例對(duì)該類進(jìn)行了一些較小的修改和補(bǔ)充,并在GitHub上作為名為InlineTypeExample的類InlineTypeExample 。 在查看此源代碼時(shí),一些立即引起注意的項(xiàng)目是關(guān)鍵字inline的存在和?的存在? 在Comparable的通用參數(shù)中。
我改編的InlineTypeExample類的源代碼試圖將內(nèi)聯(lián)類型類extend另一個(gè)類而被注釋掉,因?yàn)檫@會(huì)導(dǎo)致編譯器錯(cuò)誤: error: Inline type may not extend another inline type or class
同樣,該源代碼也具有嘗試設(shè)置注釋掉內(nèi)聯(lián)類型類的整數(shù)字段的方法,因?yàn)樵摲椒ㄒ矡o(wú)法編譯: error: cannot assign a value to final variable
使用當(dāng)前的Valhalla LW2構(gòu)建,可以使我的內(nèi)聯(lián)類型類可序列化 ,并且仍然可以成功編譯。
另一個(gè)由GitHub托管的說(shuō)明性類是Lw2Demonstration ,該類將內(nèi)聯(lián)類型類(及其實(shí)例)的特性與JDK提供的java.lang.Integer類(及其實(shí)例)以及簡(jiǎn)單的定制構(gòu)建的Integer包裝器進(jìn)行比較和對(duì)比。及其實(shí)例)。 該演示類對(duì)所有三樣?xùn)|西(內(nèi)聯(lián)類型, Integer和自定義Integer包裝器)的“ 類 ”類型調(diào)用反射方法(某些基于JDK 14的Valhalla構(gòu)建是新方法),并調(diào)用一些“通用”方法[ toString () , equals(Object) , hashCode() ]應(yīng)用于所有三種類型的實(shí)例。
在類Lw2Demonstration中,注釋了兩個(gè)方法,因?yàn)樗鼈兠總€(gè)都嘗試對(duì)內(nèi)聯(lián)類型不支持的內(nèi)聯(lián)類型執(zhí)行功能。 這些方法之一嘗試在嵌入式類型的變量上進(jìn)行同步 。 嘗試編譯此內(nèi)聯(lián)類型的同步時(shí),會(huì)看到以下編譯器錯(cuò)誤消息: error: unexpected type ... required: reference ... found: InlineTypeExample
另一個(gè)嘗試將內(nèi)聯(lián)類型分配給null 。 嘗試編譯此錯(cuò)誤時(shí),遇到以下錯(cuò)誤消息: error: incompatible types: <null> cannot be converted to InlineTypeExample
Lw2Demonstration的以下方法寫出了類類型的一些元數(shù)據(jù)特征。
/*** 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實(shí)例上調(diào)用的某些方法是基于JDK 14的Valhalla LW2早期訪問(wèn)構(gòu)建的新增方法。 這些包括isInlineClass() , isIndirectType()和isNullableType() 。
主要的演示類Lw2Demonstration創(chuàng)建內(nèi)聯(lián)類型類InlineTypeExample ,JDK提供的java.lang.Integer以及Integer的自定義包裝器的實(shí)例。 然后,演示通過(guò)相同的方法運(yùn)行這三個(gè)類和類定義的實(shí)例,并為每個(gè)結(jié)果寫出結(jié)果,以便可以對(duì)它們進(jìn)行比較和對(duì)比。 這是針對(duì)本文開頭提到的Valhalla Early Access Build運(yùn)行此示例的輸出。
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上面顯示的輸出演示了內(nèi)聯(lián)類型的一些廣告特性。 最有趣的是下表的重點(diǎn)。
| 排隊(duì)? | 真正 | 假 | 假 |
| 間接? | 假 | 真正 | 真正 |
| 可空嗎? | 假 | 真正 | 真正 |
| 最后? | 真正 | 真正 | 假 |
| ==對(duì)平等有效嗎? | 真正 | 假 | 假 |
| toString() | 隱式定制 | 明確定制 | 使用Object的 |
| hashCode() | 隱式定制 | 明確定制 | 使用Object的 |
為了編譯和執(zhí)行這些示例,我需要為Java編譯器和啟動(dòng)器提供一些特殊的參數(shù)。 具體來(lái)說(shuō),我使用--enable-preview , -Xlint:preview和-source 14編譯。 為了執(zhí)行演示,我將標(biāo)志--enable-preview傳遞給Java啟動(dòng)器。
更新的Valhalla Early Access Build [ Build jdk-14-valhalla + 1-8(2019/7/4) ]為有興趣嘗試Valhalla LW2原型內(nèi)聯(lián)類型的Java開發(fā)人員提供了一個(gè)方便的預(yù)構(gòu)建二進(jìn)制文件。 這篇文章使用此構(gòu)建演示了一些當(dāng)前的LW2內(nèi)聯(lián)類型概念。 RémiForax在GitHub( forax / valuetype-lworld )上提供了更多示例。
翻譯自: https://www.javacodegeeks.com/2019/07/project-valhalla-first-look-lw2-inline-types.html
總結(jié)
以上是生活随笔為你收集整理的Valhalla项目:LW2内联类型的初步了解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ai如何旋转画布_怎样使用AI旋转工具
- 下一篇: nio2和nio2_列出和过滤NIO.2