下面列出了java.lang.invoke.SerializedLambda#getImplClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** 将 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);
}
}
@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);
}
}