com.github.javaclub.toolbox.model.BeanFields Maven / Gradle / Ivy
/*
* @(#)BeanFields.java 2024-06-14 15:09:23
*
* Copyright (c) 2024 - 2099. All Rights Reserved.
*
*/
package com.github.javaclub.toolbox.model;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Function;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
/**
* BeanFields
*
* @author Gerald Chen
* @version $Id: BeanFields.java 2024-06-14 15:09:23 Exp $
*/
public class BeanFields {
static final Converter LOWER_UNDERLINE_CONVERTER =
CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
public static void main(String[] args) {
// System.out.println(BeanFields.lowerUnderline(XxxDO::getCategoryTreeIds));
}
public static String lowerUnderline(BeanFunction field) {
SerializedLambda lambda = serializeLambda((Serializable) field);
String methodName = lambda.getImplMethodName();
String fieldName = methodName.substring(3); // 移除 "get" 前缀
return LOWER_UNDERLINE_CONVERTER.convert(fieldName);
}
static SerializedLambda serializeLambda(Serializable lambda) {
try {
Method method = lambda.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(true);
return (SerializedLambda) method.invoke(lambda);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@FunctionalInterface
public interface BeanFunction extends Function, Serializable {
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy