| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package com.kym.jdbc.lambda;
- import com.kym.DbUtil;
- import java.lang.invoke.SerializedLambda;
- import java.lang.reflect.Method;
- /**
- * 解析字段列名
- */
- public class DefaultLambdaParser {
- public static <T> String getPropertyName(ColumnFunc<T, ?> property) {
- if (property == null) {
- return null;
- }
- try {
- Method declaredMethod = property.getClass().getDeclaredMethod("writeReplace");
- declaredMethod.setAccessible(Boolean.TRUE);
- SerializedLambda serializedLambda = (SerializedLambda) declaredMethod.invoke(property);
- // //Class.method.method....
- // if (serializedLambda.getImplMethodKind() == MethodHandleInfo.REF_invokeStatic) {
- // return getPropertyNameByInvokeStatic(property.getClass().getClassLoader(), serializedLambda);
- // }
- //Class::method
- return getPropertyNameByInvokeVirtual(serializedLambda);
- } catch (ReflectiveOperationException e) {
- throw new RuntimeException(e);
- }
- }
- private static String getPropertyNameByInvokeVirtual(SerializedLambda serializedLambda) {
- String method = serializedLambda.getImplMethodName();
- String attr = null;
- if (method.startsWith("get")) {
- attr = method.substring(3);
- } else {
- attr = method.substring(2);
- }
- return DbUtil.firstToLowerCase(attr);
- }
- }
|