DefaultLambdaParser.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.kym.jdbc.lambda;
  2. import com.kym.DbUtil;
  3. import java.lang.invoke.SerializedLambda;
  4. import java.lang.reflect.Method;
  5. /**
  6. * 解析字段列名
  7. */
  8. public class DefaultLambdaParser {
  9. public static <T> String getPropertyName(ColumnFunc<T, ?> property) {
  10. if (property == null) {
  11. return null;
  12. }
  13. try {
  14. Method declaredMethod = property.getClass().getDeclaredMethod("writeReplace");
  15. declaredMethod.setAccessible(Boolean.TRUE);
  16. SerializedLambda serializedLambda = (SerializedLambda) declaredMethod.invoke(property);
  17. // //Class.method.method....
  18. // if (serializedLambda.getImplMethodKind() == MethodHandleInfo.REF_invokeStatic) {
  19. // return getPropertyNameByInvokeStatic(property.getClass().getClassLoader(), serializedLambda);
  20. // }
  21. //Class::method
  22. return getPropertyNameByInvokeVirtual(serializedLambda);
  23. } catch (ReflectiveOperationException e) {
  24. throw new RuntimeException(e);
  25. }
  26. }
  27. private static String getPropertyNameByInvokeVirtual(SerializedLambda serializedLambda) {
  28. String method = serializedLambda.getImplMethodName();
  29. String attr = null;
  30. if (method.startsWith("get")) {
  31. attr = method.substring(3);
  32. } else {
  33. attr = method.substring(2);
  34. }
  35. return DbUtil.firstToLowerCase(attr);
  36. }
  37. }