下面列出了怎么用java.lang.invoke.SerializedLambda的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
Class<?> serialClass = filter.serialClass();
System.out.printf(" checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
serialClass, filter.arrayLength(), filter.references(),
filter.depth(), filter.streamBytes());
count++;
if (serialClass != null) {
if (serialClass.getName().contains("$$Lambda$")) {
// TBD: proper identification of serialized Lambdas?
// Fold the serialized Lambda into the SerializedLambda type
classes.add(SerializedLambda.class);
} else if (Proxy.isProxyClass(serialClass)) {
classes.add(Proxy.class);
} else {
classes.add(serialClass);
}
}
this.maxArray = Math.max(this.maxArray, filter.arrayLength());
this.maxRefs = Math.max(this.maxRefs, filter.references());
this.maxDepth = Math.max(this.maxDepth, filter.depth());
this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
return ObjectInputFilter.Status.UNDECIDED;
}
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
Class<?> serialClass = filter.serialClass();
System.out.printf(" checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
serialClass, filter.arrayLength(), filter.references(),
filter.depth(), filter.streamBytes());
count++;
if (serialClass != null) {
if (serialClass.getName().contains("$$Lambda$")) {
// TBD: proper identification of serialized Lambdas?
// Fold the serialized Lambda into the SerializedLambda type
classes.add(SerializedLambda.class);
} else if (Proxy.isProxyClass(serialClass)) {
classes.add(Proxy.class);
} else {
classes.add(serialClass);
}
}
this.maxArray = Math.max(this.maxArray, filter.arrayLength());
this.maxRefs = Math.max(this.maxRefs, filter.references());
this.maxDepth = Math.max(this.maxDepth, filter.depth());
this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
return ObjectInputFilter.Status.UNDECIDED;
}
public static <T> String getFieldName(EntityGetterMethod<T, Object> expression) {
if (expression == null)
throw new IllegalArgumentException("Expression should not be null");
try {
Method method = expression.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda)method.invoke(expression);
String fieldName = StringUtils.resolveFieldName(serializedLambda.getImplMethodName());
String className = serializedLambda.getImplClass().replace("/", ".");
Field field = ReflectionUtils.findField(Class.forName(className), fieldName);
String columnName = field.getName();
TableField[] tableField = field.getAnnotationsByType(TableField.class);
if (null != tableField && tableField.length == 1) {
if (!StringUtils.isEmpty(tableField[0].value())) {
columnName = tableField[0].value();
}
}
String ret = StringUtils.camelToUnderline(columnName);
return ret;
} catch (ReflectiveOperationException e) {
throw new RuntimeException("This will never happen!", e);
}
}
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
Class<?> serialClass = filter.serialClass();
System.out.printf(" checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
serialClass, filter.arrayLength(), filter.references(),
filter.depth(), filter.streamBytes());
count++;
if (serialClass != null) {
if (serialClass.getName().contains("$$Lambda$")) {
// TBD: proper identification of serialized Lambdas?
// Fold the serialized Lambda into the SerializedLambda type
classes.add(SerializedLambda.class);
} else if (Proxy.isProxyClass(serialClass)) {
classes.add(Proxy.class);
} else {
classes.add(serialClass);
}
}
this.maxArray = Math.max(this.maxArray, filter.arrayLength());
this.maxRefs = Math.max(this.maxRefs, filter.references());
this.maxDepth = Math.max(this.maxDepth, filter.depth());
this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
return ObjectInputFilter.Status.UNDECIDED;
}
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
Class<?> serialClass = filter.serialClass();
System.out.printf(" checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
serialClass, filter.arrayLength(), filter.references(),
filter.depth(), filter.streamBytes());
count++;
if (serialClass != null) {
if (serialClass.getName().contains("$$Lambda$")) {
// TBD: proper identification of serialized Lambdas?
// Fold the serialized Lambda into the SerializedLambda type
classes.add(SerializedLambda.class);
} else if (Proxy.isProxyClass(serialClass)) {
classes.add(Proxy.class);
} else {
classes.add(serialClass);
}
}
this.maxArray = Math.max(this.maxArray, filter.arrayLength());
this.maxRefs = Math.max(this.maxRefs, filter.references());
this.maxDepth = Math.max(this.maxDepth, filter.depth());
this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
return ObjectInputFilter.Status.UNDECIDED;
}
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
Class<?> serialClass = filter.serialClass();
System.out.printf(" checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
serialClass, filter.arrayLength(), filter.references(),
filter.depth(), filter.streamBytes());
count++;
if (serialClass != null) {
if (serialClass.getName().contains("$$Lambda$")) {
// TBD: proper identification of serialized Lambdas?
// Fold the serialized Lambda into the SerializedLambda type
classes.add(SerializedLambda.class);
} else if (Proxy.isProxyClass(serialClass)) {
classes.add(Proxy.class);
} else {
classes.add(serialClass);
}
}
this.maxArray = Math.max(this.maxArray, filter.arrayLength());
this.maxRefs = Math.max(this.maxRefs, filter.references());
this.maxDepth = Math.max(this.maxDepth, filter.depth());
this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
return ObjectInputFilter.Status.UNDECIDED;
}
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
count++;
if (filter.serialClass() != null) {
if (filter.serialClass().getName().contains("$$Lambda$")) {
// TBD: proper identification of serialized Lambdas?
// Fold the serialized Lambda into the SerializedLambda type
classes.add(SerializedLambda.class);
} else if (Proxy.isProxyClass(filter.serialClass())) {
classes.add(Proxy.class);
} else {
classes.add(filter.serialClass());
}
}
this.maxArray = Math.max(this.maxArray, filter.arrayLength());
this.maxRefs = Math.max(this.maxRefs, filter.references());
this.maxDepth = Math.max(this.maxDepth, filter.depth());
this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
return ObjectInputFilter.Status.UNDECIDED;
}
@Override
public ObjectInputFilter.Status checkInput(FilterInfo filter) {
Class<?> serialClass = filter.serialClass();
System.out.printf(" checkInput: class: %s, arrayLen: %d, refs: %d, depth: %d, bytes; %d%n",
serialClass, filter.arrayLength(), filter.references(),
filter.depth(), filter.streamBytes());
count++;
if (serialClass != null) {
if (serialClass.getName().contains("$$Lambda$")) {
// TBD: proper identification of serialized Lambdas?
// Fold the serialized Lambda into the SerializedLambda type
classes.add(SerializedLambda.class);
} else if (Proxy.isProxyClass(serialClass)) {
classes.add(Proxy.class);
} else {
classes.add(serialClass);
}
}
this.maxArray = Math.max(this.maxArray, filter.arrayLength());
this.maxRefs = Math.max(this.maxRefs, filter.references());
this.maxDepth = Math.max(this.maxDepth, filter.depth());
this.maxBytes = Math.max(this.maxBytes, filter.streamBytes());
return ObjectInputFilter.Status.UNDECIDED;
}
private static String[] getSerializedLambdaParameterizedTypeNames(Object source) {
Method method = ReflectionUtils.findMethod(source.getClass(), "writeReplace");
if (method == null) {
return null;
}
ReflectionUtils.makeAccessible(method);
SerializedLambda serializedLambda = (SerializedLambda) ReflectionUtils
.invokeMethod(method, source);
String signature = serializedLambda.getImplMethodSignature().replaceAll("[()]",
"");
List<String> typeNames = Stream.of(signature.split(";"))
.map(t -> t.substring(1).replace('/', '.')).collect(Collectors.toList());
return typeNames.toArray(new String[typeNames.size()]);
}
/** 将 Property 转为 PropertyWrap */
private static <C> PropertyWrap wrap(Property<C, ?> p) {
try {
Method fun = p.getClass().getDeclaredMethod("writeReplace");
fun.setAccessible(Boolean.TRUE);
SerializedLambda slambda = (SerializedLambda) fun.invoke(p);
String method = slambda.getImplMethodName();
String attr = null;
if (method.startsWith("get")) {
attr = method.substring(3);
} else {
attr = method.substring(2);//is
}
return new PropertyWrap(p, slambda.getImplClass(), attr);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
private static <C> String getName(Property<C, ?> property) {
try {
Method declaredMethod = property.getClass().getDeclaredMethod("writeReplace");
declaredMethod.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda) declaredMethod.invoke(property);
String method = serializedLambda.getImplMethodName();
String attr = null;
if (method.startsWith("get")) {
attr = method.substring(3);
} else {
attr = method.substring(2);
}
return attr;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public static String getLambdaFieldName(SerializedLambda serializedLambda) {
String name = CACHE_LAMBDA_NAME.get(serializedLambda);
if (null != name) {
return name;
}
String className = serializedLambda.getImplClass().replace("/", ".");
String methodName = serializedLambda.getImplMethodName();
String fieldName = methodToFieldName(methodName);
try {
Field field = Class.forName(className).getDeclaredField(fieldName);
name = field.getName();
CACHE_LAMBDA_NAME.put(serializedLambda, name);
return name;
} catch (NoSuchFieldException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static String fnToFieldName(Fn fn) {
try {
Method method = fn.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(fn);
String getter = serializedLambda.getImplMethodName();
if (GET_PATTERN.matcher(getter).matches()) {
getter = getter.substring(3);
} else if (IS_PATTERN.matcher(getter).matches()) {
getter = getter.substring(2);
}
return Introspector.decapitalize(getter);
} catch (ReflectiveOperationException e) {
throw new ReflectionOperationException(e);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
public static String expressionToMethodName(Expression expression) {
if (expression == null)
return "";
try {
Method method = expression.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda)method.invoke(expression);
return serializedLambda.getImplMethodName();
} catch (ReflectiveOperationException e) {
return "";
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
/**
* 获取列名称
* @param lambda lamda表达式
* @return String 列名称
*/
static String getLambdaColumnName(Serializable lambda) {
try {
Method method = lambda.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(lambda);
String getter = serializedLambda.getImplMethodName();
String fieldName = Introspector.decapitalize(getter.replace("get", ""));
return EntityTools.transferColumnName(fieldName);
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException(e);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
default SerializedLambda serialized() {
try {
Method replaceMethod = getClass().getDeclaredMethod("writeReplace");
replaceMethod.setAccessible(true);
return (SerializedLambda) replaceMethod.invoke(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
default Method method() {
SerializedLambda lambda = serialized();
Class<?> containingClass = getContainingClass();
return Arrays.asList(containingClass.getDeclaredMethods())
.stream()
.filter(method -> Objects.equals(method.getName(), lambda.getImplMethodName()))
.findFirst()
.orElseThrow(UnableToGuessMethodException::new);
}
default SerializedLambda serialized() {
try {
Method replaceMethod = getClass().getDeclaredMethod("writeReplace");
replaceMethod.setAccessible(true);
return (SerializedLambda) replaceMethod.invoke(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
default Method method() {
SerializedLambda lambda = serialized();
Class containingClass = getContainingClass();
return Arrays.stream(containingClass.getDeclaredMethods())
.filter(method -> Objects.equals(method.getName(), lambda.getImplMethodName()))
.findFirst()
.orElseThrow(MethodReferenceReflection.UnableToGuessMethodException::new);
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
@SuppressWarnings("squid:S00112")
public static <T, R extends Comparable> PropertyInfo getPropertyInfo(GetPropertyFunction<T, R> fn) {
try {
Method method = fn.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(true);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(fn);
String methodName = serializedLambda.getImplMethodName();
String className = serializedLambda.getImplClass();
String propertyName;
String getString = "get";
String isString = "is";
if (methodName.startsWith(getString)) {
propertyName = java.beans.Introspector.decapitalize(methodName.substring(3));
} else if (methodName.startsWith(isString)) {
propertyName = java.beans.Introspector.decapitalize(methodName.substring(2));
} else {
propertyName = methodName;
}
Class ownerClass;
if (classMap.containsKey(className)) {
ownerClass = classMap.get(className);
} else {
ownerClass = Class.forName(className.replace('/', '.'));
classMap.put(className, ownerClass);
}
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setPropertyName(propertyName);
propertyInfo.setOwnerClass(ownerClass);
return propertyInfo;
} catch (ReflectiveOperationException e) {
throw new InternalRuntimeException(e);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}
private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
try {
Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
if (!expectedPresent)
fail("Unexpected $deserializeLambda$ in " + clazz);
}
catch (NoSuchMethodException e) {
if (expectedPresent)
fail("Expected to find $deserializeLambda$ in " + clazz);
}
}