SerializedLambda优化
This commit is contained in:
@@ -3,10 +3,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.util;
|
package org.redkale.util;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.Serializable;
|
||||||
import java.lang.invoke.MethodHandleInfo;
|
import java.lang.invoke.MethodHandleInfo;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 完全复制java.lang.invoke.SerializedLambda类源码,必须保持字段信息一样
|
* 完全复制java.lang.invoke.SerializedLambda类源码,必须保持字段信息一样
|
||||||
@@ -249,29 +248,4 @@ public class SerializedLambda implements Serializable {
|
|||||||
"numCaptured", capturedArgs.length);
|
"numCaptured", capturedArgs.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final ConcurrentHashMap<Class, String> lambdaFieldNameCache = new ConcurrentHashMap();
|
|
||||||
|
|
||||||
public static String readLambdaFieldName(Serializable func) {
|
|
||||||
if (!func.getClass().isSynthetic()) { //必须是Lambda表达式的合成类
|
|
||||||
throw new RedkaleException("Not a synthetic lambda class");
|
|
||||||
}
|
|
||||||
return lambdaFieldNameCache.computeIfAbsent(func.getClass(), clazz -> {
|
|
||||||
try {
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(out);
|
|
||||||
oos.writeObject(func);
|
|
||||||
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())) {
|
|
||||||
@Override
|
|
||||||
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
|
|
||||||
Class<?> clazz = super.resolveClass(desc);
|
|
||||||
return clazz == java.lang.invoke.SerializedLambda.class ? SerializedLambda.class : clazz;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return Utility.readFieldName(((SerializedLambda) in.readObject()).getImplMethodName());
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RedkaleException(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -282,7 +282,7 @@ public final class Utility {
|
|||||||
return lambdaFieldNameCache.computeIfAbsent(func.getClass(), clazz -> {
|
return lambdaFieldNameCache.computeIfAbsent(func.getClass(), clazz -> {
|
||||||
try {
|
try {
|
||||||
if (nativeImageEnv) {
|
if (nativeImageEnv) {
|
||||||
return org.redkale.util.SerializedLambda.readLambdaFieldName(func);
|
return readLambdaFieldNameFromBytes(func);
|
||||||
} else {
|
} else {
|
||||||
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(func.getClass(), MethodHandles.lookup());
|
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(func.getClass(), MethodHandles.lookup());
|
||||||
MethodHandle mh = lookup.findVirtual(func.getClass(), "writeReplace", MethodType.methodType(Object.class));
|
MethodHandle mh = lookup.findVirtual(func.getClass(), "writeReplace", MethodType.methodType(Object.class));
|
||||||
@@ -290,11 +290,32 @@ public final class Utility {
|
|||||||
return readFieldName(methodName);
|
return readFieldName(methodName);
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
throw new RedkaleException(e);
|
if (!nativeImageEnv) {
|
||||||
|
return readLambdaFieldNameFromBytes(func);
|
||||||
|
}
|
||||||
|
throw e instanceof RedkaleException ? (RedkaleException) e : new RedkaleException(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String readLambdaFieldNameFromBytes(Serializable func) {
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
ObjectOutputStream oos = new ObjectOutputStream(out);
|
||||||
|
oos.writeObject(func);
|
||||||
|
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())) {
|
||||||
|
@Override
|
||||||
|
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
|
||||||
|
Class<?> clazz = super.resolveClass(desc);
|
||||||
|
return clazz == java.lang.invoke.SerializedLambda.class ? org.redkale.util.SerializedLambda.class : clazz;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return Utility.readFieldName(((org.redkale.util.SerializedLambda) in.readObject()).getImplMethodName());
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new RedkaleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static String readFieldName(String methodName) {
|
static String readFieldName(String methodName) {
|
||||||
String name;
|
String name;
|
||||||
if (methodName.startsWith("is")) {
|
if (methodName.startsWith("is")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user