cn.sylinx.hbatis.kit.LambdaUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbatis-core Show documentation
Show all versions of hbatis-core Show documentation
hbatis is a simple orm framework
package cn.sylinx.hbatis.kit;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import cn.sylinx.hbatis.exception.HbatisException;
public class LambdaUtil {
public static String methodToFieldName(String methodName) {
return capitalize(methodName.replace("get", ""));
}
public static String capitalize(String input) {
return input.substring(0, 1).toLowerCase() + input.substring(1, input.length());
}
public static String getLambdaFieldName(Serializable lambda) {
SerializedLambda serializedLambda = computeSerializedLambda(lambda);
return methodToFieldName(serializedLambda.getImplMethodName());
}
private static SerializedLambda computeSerializedLambda(Serializable lambda) {
Class> cl = lambda.getClass();
try {
Method m = cl.getDeclaredMethod("writeReplace");
m.setAccessible(true);
Object replacement = m.invoke(lambda);
if (replacement instanceof SerializedLambda) {
return (SerializedLambda) replacement;
}
} catch (Exception e) {
throw new HbatisException("get lambda column name fail", e);
}
return null;
}
}