Java 获取属性名称
package com.huake.erp.common.tools.util;
import cn.hutool.core.util.StrUtil;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
/**
* @author xxxx
* @description 属性工具类
* @since 2022-12-29
*/
public final class FieldUtil {
/**
* 工具类隐藏构造器
*/
private FieldUtil() {
}
/**
* 下划线样式,小写
*
* @param fn fn
* @param <T> T
* @return result
*/
public static <T> String underline(IGetter<T> fn) {
return toSymbolCase(fn, '_');
}
/**
* 下划线样式,大写
*
* @param fn fn
* @param <T> T
* @return result
*/
public static <T> String underlineUpper(IGetter<T> fn) {
return underline(fn).toUpperCase();
}
/**
* 依据符号转换样式
*
* @param fn fn
* @param symbol symbol
* @param <T> T
* @return result
*/
public static <T> String toSymbolCase(IGetter<T> fn, char symbol) {
return StrUtil.toSymbolCase(noPrefix(fn), symbol);
}
/**
* 转换getter方法引用为属性名,首字母小写
*
* @param fn fn
* @param <T> T
* @return result
*/
public static <T> String noPrefix(IGetter<T> fn) {
return getGeneralField(fn);
}
/*
* ===========> setter 方法引用 <===========
*/
/**
* 下划线样式,小写
*
* @param fn fn
* @param <T> T
* @param <R> R
* @return result
*/
public static <T, R> String underline(ISetter<T, R> fn) {
return toSymbolCase(fn, '_');
}
/**
* 下划线样式,大写
*
* @param fn fn
* @param <T> T
* @param <R> R
* @return result
*/
public static <T, R> String underlineUpper(ISetter<T, R> fn) {
return underline(fn).toUpperCase();
}
/**
* 依据符号转换样式
*
* @param fn fn
* @param symbol symbol
* @param <T> T
* @param <R> R
* @return result
*/
public static <T, R> String toSymbolCase(ISetter<T, R> fn, char symbol) {
return StrUtil.toSymbolCase(noPrefix(fn), symbol);
}
/**
* 转换setter方法引用为属性名,首字母小写
*
* @param fn fn
* @param <T> T
* @param <R> R
* @return result
*/
public static <T, R> String noPrefix(ISetter<T, R> fn) {
return getGeneralField(fn);
}
/**
* 获得set或get或is方法对应的标准属性名,其它前缀的方法名使用原名
*
* @param fn
* @return
*/
private static String getGeneralField(Serializable fn) {
SerializedLambda lambda = getSerializedLambda(fn);
String getOrSetMethodName = lambda.getImplMethodName();
final String generalField = StrUtil.getGeneralField(getOrSetMethodName);
return StrUtil.isEmpty(generalField) ? getOrSetMethodName : generalField;
}
/**
* 获取类对应的Lambda
*
* @param fn
* @return
*/
private static SerializedLambda getSerializedLambda(Serializable fn) {
//先检查缓存中是否已存在
SerializedLambda lambda;
try {
//提取SerializedLambda并缓存
Method method = fn.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
lambda = (SerializedLambda) method.invoke(fn);
} catch (Exception e) {
throw new IllegalArgumentException("获取SerializedLambda异常, class=" + fn.getClass().getSimpleName(), e);
}
return lambda;
}
/**
* @author heyangyang
* @param <T> T
* @description 属性工具类
* @since 2022-12-29
*/
@FunctionalInterface
public interface IGetter<T> extends Serializable {
/**
* 获取
* @param source source
* @return 返回
*/
Object apply(T source);
}
/**
* @author heyangyang
* @param <T> T
* @param <U> T
* @description 属性工具类
* @since 2022-12-29
*/
@FunctionalInterface
public interface ISetter<T, U> extends Serializable {
/**
* 申请
*
* @param t
* @param u
*/
void accept(T t, U u);
}
}
使用方法:
ReflectionUtil.noPrefix(CrmCustomerBasePageSelVo::getCustomerType)