快速上手 Record 類
我們先舉一個簡單例子,宣告一個使用者 Record。
public record User(long id, String name, int age) {}
這樣編寫程式碼之後,Record 類預設包含的元素和方法實現包括:
- record 頭指定的組成元素(
int id, String name, int age
),並且,這些元素都是 final 的。 - record 預設只有一個構造器,是包含所有元素的構造器。
- record 的每個元素都有一個對應的 getter(但這種 getter 並不是 getxxx(),而是直接用變數名命名,所以使用序列化框架,DAO 框架都要注意這一點)
- 實現好的 hashCode(),equals(),toString() 方法(通過自動在編譯階段生成關於 hashCode(),equals(),toString() 方法實現的位元組碼實現)。
我們來使用下這個 Record :
User zhx = new User(1, "zhx", 29);
User ttj = new User(2, "ttj", 25);
System.out.println(zhx.id());//1
System.out.println(zhx.name());//zhx
System.out.println(zhx.age());//29
System.out.println(zhx.equals(ttj));//false
System.out.println(zhx.toString());//User[id=1, name=zhx, age=29]
System.out.println(zhx.hashCode());//3739156
Record 的結構是如何實現的
編譯後插入相關域與方法的位元組碼
檢視上面舉得例子的位元組碼,有兩種方式,一是通過 javap -v User.class
命令檢視文字版的位元組碼,擷取重要的位元組碼如下所示:
//省略檔案頭,檔案常量池部分
{
//public 構造器,全部屬性作為引數,並給每個 Field 賦值
public com.github.hashzhang.basetest.User(long, java.lang.String, int);
descriptor: (JLjava/lang/String;I)V
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=5, args_size=4
0: aload_0
1: invokespecial #1 // Method java/lang/Record."<init>":()V
4: aload_0
5: lload_1
6: putfield #7 // Field id:J
9: aload_0
10: aload_3
11: putfield #13 // Field name:Ljava/lang/String;
14: aload_0
15: iload 4
17: putfield #17 // Field age:I
20: return
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 21 0 this Lcom/github/hashzhang/basetest/User;
0 21 1 id J
0 21 3 name Ljava/lang/String;
0 21 4 age I
MethodParameters:
Name Flags
id
name
age
//public final 修飾的 toString 方法
public final java.lang.String toString();
descriptor: ()Ljava/lang/String;
flags: (0x0011) ACC_PUBLIC, ACC_FINAL
Code:
stack=1, locals=1, args_size=1
0: aload_0
//核心實現是這個 invokedynamic,我們後面會分析
1: invokedynamic #21, 0 // InvokeDynamic #0:toString:(Lcom/github/hashzhang/basetest/User;)Ljava/lang/String;
6: areturn
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 7 0 this Lcom/github/hashzhang/basetest/User;
//public final 修飾的 hashCode 方法
public final int hashCode();
descriptor: ()I
flags: (0x0011) ACC_PUBLIC, ACC_FINAL
Code:
stack=1, locals=1, args_size=1
0: aload_0
//核心實現是這個 invokedynamic,我們後面會分析
1: invokedynamic #25, 0 // InvokeDynamic #0:hashCode:(Lcom/github/hashzhang/basetest/User;)I
6: ireturn
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 7 0 this Lcom/github/hashzhang/basetest/User;
//public final 修飾的 equals 方法
public final boolean equals(java.lang.Object);
descriptor: (Ljava/lang/Object;)Z
flags: (0x0011) ACC_PUBLIC, ACC_FINAL
Code:
stack=2, locals=2, args_size=2
0: aload_0
1: aload_1
//核心實現是這個 invokedynamic,我們後面會分析
2: invokedynamic #29, 0 // InvokeDynamic #0:equals:(Lcom/github/hashzhang/basetest/User;Ljava/lang/Object;)Z
7: ireturn
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 8 0 this Lcom/github/hashzhang/basetest/User;
0 8 1 o Ljava/lang/Object;
//public 修飾的 id 的 getter
public long id();
descriptor: ()J
flags: (0x0001) ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: getfield #7 // Field id:J
4: lreturn
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/github/hashzhang/basetest/User;
//public 修飾的 name 的 getter
public java.lang.String name();
descriptor: ()Ljava/lang/String;
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: getfield #13 // Field name:Ljava/lang/String;
4: areturn
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/github/hashzhang/basetest/User;
//public 修飾的 age 的 getter
public int age();
descriptor: ()I
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: getfield #17 // Field age:I
4: ireturn
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/github/hashzhang/basetest/User;
}
SourceFile: "User.java"
Record:
long id;
descriptor: J
java.lang.String name;
descriptor: Ljava/lang/String;
int age;
descriptor: I
//以下是 invokedynamic 會呼叫的方法以及引數資訊,我們後面會分析
BootstrapMethods:
0: #50 REF_invokeStatic java/lang/runtime/ObjectMethods.bootstrap:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava
/lang/Object;
Method arguments:
#8 com/github/hashzhang/basetest/User
#57 id;name;age
#59 REF_getField com/github/hashzhang/basetest/User.id:J
#60 REF_getField com/github/hashzhang/basetest/User.name:Ljava/lang/String;
#61 REF_getField com/github/hashzhang/basetest/User.age:I
InnerClasses:
public static final #67= #63 of #65; // Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
另一種是通過 IDE 的 jclasslib 外掛檢視,我推薦使用這種方法,檢視效果如下:
自動生成的 private final field
自動生成的全屬性構造器
自動生成的 public getter 方法
自動生成的 hashCode(),equals(),toString() 方法
這些方法的核心就是 invokedynamic:
看上去貌似是呼叫另外一個方法,這種間接呼叫難道沒有效能損耗問題麼?這一點 JVM 開發者已經想到了。我們先來來了解下 invokedynamic。
invokedynamic 產生的背景
Java 最早是一種靜態型別語言,也就是說它的型別檢查的主體過程主要是在編譯期而不是執行期。為了相容動態型別語法,也是為了 JVM 能夠相容動態語言(JVM 設計初衷並不是只能執行 Java),在 Java 7 引入了位元組碼指令 invokedynamic。這也是後來 Java 8 的拉姆達表示式以及 var 語法的實現基礎。
invokedynamic 與 MethodHandle
invokedynamic 離不開對 java.lang.invoke 包的使用。這個包的主要目的是在之前單純依靠符號引用來確定呼叫的目標方法這種方式以外,提供一種新的動態確定目標方法的機制,稱為MethodHandle
。
通過 MethodHandle
可以動態獲取想呼叫的方法進行呼叫,和 Java Reflection
反射類似,但是為了追求效能效率,需要用 MethodHandle
,主要原因是: Reflection
僅僅是 Java 語言上補充針對反射的實現,並沒有考慮效率的問題,尤其是 JIT 基本無法針對這種反射呼叫進行有效的優化。MethodHandle
更是像是對於位元組碼的方法指令呼叫的模擬,適當使用的話 JIT 也能對於它進行優化,例如將 MethodHandle
相關方法引用宣告為 static final 的:
private static final MutableCallSite callSite = new MutableCallSite(
MethodType.methodType(int.class, int.class, int.class));
private static final MethodHandle invoker = callSite.dynamicInvoker();
自動生成的 toString(), hashcode(), equals() 的實現
通過位元組碼可以看出 incokedynamic 實際呼叫的是 BoostrapMethods
中的 #0 方法:
0 aload_0
1 invokedynamic #24 <hashCode, BootstrapMethods #0>
6 ireturn
Bootstap 方法表包括:
BootstrapMethods:
//呼叫的實際是 java.lang.runtime.ObjectMethods 的 boostrap 方法
0: #50 REF_invokeStatic java/lang/runtime/ObjectMethods.bootstrap:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava
/lang/Object;
Method arguments:
#8 com/github/hashzhang/basetest/User
#57 id;name;age
#59 REF_getField com/github/hashzhang/basetest/User.id:J
#60 REF_getField com/github/hashzhang/basetest/User.name:Ljava/lang/String;
#61 REF_getField com/github/hashzhang/basetest/User.age:I
InnerClasses:
//宣告 MethodHandles.Lookup 為 final,加快呼叫效能,這樣呼叫 BootstrapMethods 裡面的方法可以實現近似於直接呼叫的效能
public static final #67= #63 of #65; // Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
從這裡,我們就能看出,實際上 toString() 呼叫的是 java.lang.runtime.ObjectMethods
的 bootstap()
方法。其核心程式碼是:
ObjectMethods.java
public static Object bootstrap(MethodHandles.Lookup lookup, String methodName, TypeDescriptor type,
Class<?> recordClass,
String names,
MethodHandle... getters) throws Throwable {
MethodType methodType;
if (type instanceof MethodType)
methodType = (MethodType) type;
else {
methodType = null;
if (!MethodHandle.class.equals(type))
throw new IllegalArgumentException(type.toString());
}
List<MethodHandle> getterList = List.of(getters);
MethodHandle handle;
//根據 method 名稱,處理對應的邏輯,分別對應了 equals(),hashCode(),toString() 的實現
switch (methodName) {
case "equals":
if (methodType != null && !methodType.equals(MethodType.methodType(boolean.class, recordClass, Object.class)))
throw new IllegalArgumentException("Bad method type: " + methodType);
handle = makeEquals(recordClass, getterList);
return methodType != null ? new ConstantCallSite(handle) : handle;
case "hashCode":
if (methodType != null && !methodType.equals(MethodType.methodType(int.class, recordClass)))
throw new IllegalArgumentException("Bad method type: " + methodType);
handle = makeHashCode(recordClass, getterList);
return methodType != null ? new ConstantCallSite(handle) : handle;
case "toString":
if (methodType != null && !methodType.equals(MethodType.methodType(String.class, recordClass)))
throw new IllegalArgumentException("Bad method type: " + methodType);
List<String> nameList = "".equals(names) ? List.of() : List.of(names.split(";"));
if (nameList.size() != getterList.size())
throw new IllegalArgumentException("Name list and accessor list do not match");
handle = makeToString(recordClass, getterList, nameList);
return methodType != null ? new ConstantCallSite(handle) : handle;
default:
throw new IllegalArgumentException(methodName);
}
}
其中,toString() 方法 的核心實現邏輯,就要看case "toString"
這一分支了,核心邏輯是makeToString(recordClass, getterList, nameList)
:
private static MethodHandle makeToString(Class<?> receiverClass,
//所有的 getter 方法
List<MethodHandle> getters,
//所有的 field 名稱
List<String> names) {
assert getters.size() == names.size();
int[] invArgs = new int[getters.size()];
Arrays.fill(invArgs, 0);
MethodHandle[] filters = new MethodHandle[getters.size()];
StringBuilder sb = new StringBuilder();
//先拼接類名稱[
sb.append(receiverClass.getSimpleName()).append("[");
for (int i=0; i<getters.size(); i++) {
MethodHandle getter = getters.get(i); // (R)T
MethodHandle stringify = stringifier(getter.type().returnType()); // (T)String
MethodHandle stringifyThisField = MethodHandles.filterArguments(stringify, 0, getter); // (R)String
filters[i] = stringifyThisField;
//之後拼接 field 名稱=值
sb.append(names.get(i)).append("=%s");
if (i != getters.size() - 1)
sb.append(", ");
}
sb.append(']');
String formatString = sb.toString();
MethodHandle formatter = MethodHandles.insertArguments(STRING_FORMAT, 0, formatString)
.asCollector(String[].class, getters.size()); // (R*)String
if (getters.size() == 0) {
// Add back extra R
formatter = MethodHandles.dropArguments(formatter, 0, receiverClass);
}
else {
MethodHandle filtered = MethodHandles.filterArguments(formatter, 0, filters);
formatter = MethodHandles.permuteArguments(filtered, MethodType.methodType(String.class, receiverClass), invArgs);
}
return formatter;
}
同理,hashcode()
實現是:
private static MethodHandle makeHashCode(Class<?> receiverClass,
List<MethodHandle> getters) {
MethodHandle accumulator = MethodHandles.dropArguments(ZERO, 0, receiverClass); // (R)I
// 對於每一個 field,找到對應的 hashcode 方法,取 雜湊值,最後組合在一起
for (MethodHandle getter : getters) {
MethodHandle hasher = hasher(getter.type().returnType()); // (T)I
MethodHandle hashThisField = MethodHandles.filterArguments(hasher, 0, getter); // (R)I
MethodHandle combineHashes = MethodHandles.filterArguments(HASH_COMBINER, 0, accumulator, hashThisField); // (RR)I
accumulator = MethodHandles.permuteArguments(combineHashes, accumulator.type(), 0, 0); // adapt (R)I to (RR)I
}
return accumulator;
}
同理,equals()
實現是:
private static MethodHandle makeEquals(Class<?> receiverClass,
List<MethodHandle> getters) {
MethodType rr = MethodType.methodType(boolean.class, receiverClass, receiverClass);
MethodType ro = MethodType.methodType(boolean.class, receiverClass, Object.class);
MethodHandle instanceFalse = MethodHandles.dropArguments(FALSE, 0, receiverClass, Object.class); // (RO)Z
MethodHandle instanceTrue = MethodHandles.dropArguments(TRUE, 0, receiverClass, Object.class); // (RO)Z
MethodHandle isSameObject = OBJECT_EQ.asType(ro); // (RO)Z
MethodHandle isInstance = MethodHandles.dropArguments(CLASS_IS_INSTANCE.bindTo(receiverClass), 0, receiverClass); // (RO)Z
MethodHandle accumulator = MethodHandles.dropArguments(TRUE, 0, receiverClass, receiverClass); // (RR)Z
//對比兩個物件的每個 field 的 getter 獲取的值是否一樣,對於引用型別通過 Objects.equals 方法,對於原始型別直接通過 ==
for (MethodHandle getter : getters) {
MethodHandle equalator = equalator(getter.type().returnType()); // (TT)Z
MethodHandle thisFieldEqual = MethodHandles.filterArguments(equalator, 0, getter, getter); // (RR)Z
accumulator = MethodHandles.guardWithTest(thisFieldEqual, accumulator, instanceFalse.asType(rr));
}
return MethodHandles.guardWithTest(isSameObject,
instanceTrue,
MethodHandles.guardWithTest(isInstance, accumulator.asType(ro), instanceFalse));
}
我在參與 掘金2021年度人氣榜單,麻煩大家幫我投出寶貴一票,謝謝