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上面顯示的輸出演示了內聯類型的一些廣告特性。 最有趣的是下表的重點。
| 排隊? | 真正 | 假 | 假 |
| 間接? | 假 | 真正 | 真正 |
| 可空嗎? | 假 | 真正 | 真正 |
| 最后? | 真正 | 真正 | 假 |
| ==對平等有效嗎? | 真正 | 假 | 假 |
| 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
總結
以上是生活随笔為你收集整理的Valhalla项目:LW2内联类型的初步了解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ai如何旋转画布_怎样使用AI旋转工具
- 下一篇: Java中带有NetSuite数据实体的