All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.sylinx.hbatis.kit.LambdaUtil Maven / Gradle / Ivy

There is a newer version: 2.0.0.RELEASE
Show newest version
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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy