下面列出了java.lang.invoke.SerializedLambda#getImplMethodName ( ) 实例代码,或者点击链接到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);
}
}
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);
}
}
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 "";
}
}
/**
* 获取列名称
* @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);
}
}
@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);
}
}
public ControllerInspector(Controller controller) {
Class<?> controllerClass = controller.getClass();
try {
if (controller instanceof LambdaController) {
Method writeReplace = controllerClass.getDeclaredMethod("writeReplace");
if (!writeReplace.trySetAccessible()) {
throw new Error("failed to inspect controller, cannot access writeReplace");
}
SerializedLambda lambda = (SerializedLambda) writeReplace.invoke(controller);
Class<?> targetClass = Class.forName(lambda.getImplClass().replace('/', '.'));
String targetMethodName = lambda.getImplMethodName();
controllerInfo = targetClass.getCanonicalName() + "." + targetMethodName;
if (targetMethodName.contains("$")) { // for lambda
this.targetClass = controllerClass;
targetMethod = controllerClass.getMethod("execute", Request.class);
} else { // for method reference
this.targetClass = targetClass;
targetMethod = targetClass.getDeclaredMethod(targetMethodName, Request.class);
}
} else {
targetClass = controllerClass;
targetMethod = controllerClass.getMethod("execute", Request.class);
controllerInfo = controllerClass.getCanonicalName() + ".execute";
}
} catch (ReflectiveOperationException e) {
throw new Error("failed to inspect controller", e);
}
}
default String getResultDescription() {
SerializedLambda lambda = asSerializedLambda();
if (! lambda.getImplMethodName().startsWith("lambda$")) {
return lambda.getImplMethodName();
}
return "boolean";
}
default String getResultDescription() {
SerializedLambda lambda = asSerializedLambda();
MethodType lambdaMethodType = MethodType.fromMethodDescriptorString(lambda.getImplMethodSignature(), getClass().getClassLoader());
String resultType = lambdaMethodType.returnType().getSimpleName();
if (! lambda.getImplMethodName().startsWith("lambda$")) {
return lambda.getImplMethodName() + " (" + withPrefixedArticle(resultType) + ")";
}
return resultType;
}