表属性获取工具
This commit is contained in:
parent
51ef33cde6
commit
0fc3b3562e
|
|
@ -60,6 +60,8 @@ public abstract class BaseForwardReport {
|
|||
List<ForwardReportVO> bomList = convertBomList.stream().filter(u -> ObjectUtil.isNotNull(u.getBomRowId()) && u.getBomRowId() > 0)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
|
||||
//bomRowId 下无数据
|
||||
bomList.forEach(item->{
|
||||
|
||||
List<ForwardReportVO> tmpList= convertBomList.stream().filter(u -> Objects.equals(u.getParentRowId(),item.getBomRowId()) )
|
||||
|
|
|
|||
|
|
@ -0,0 +1,104 @@
|
|||
package com.nflg.product.bomnew.util;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.invoke.SerializedLambda;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FieldGetterUtil {
|
||||
|
||||
public interface FieldGetter<T, R> extends Function<T, R>, Serializable {
|
||||
|
||||
@SneakyThrows
|
||||
default String getFieldName() {
|
||||
String methodName = getMethodName();
|
||||
if (methodName.startsWith("get")) {
|
||||
methodName = methodName.substring(3);
|
||||
}
|
||||
return CharSequenceUtil.lowerFirst(methodName);
|
||||
}
|
||||
|
||||
|
||||
default String getTableField() {
|
||||
String fieldName = getFieldName();
|
||||
fieldName = fieldName.replaceFirst(fieldName.charAt(0) + "", (fieldName.charAt(0) + "").toLowerCase());
|
||||
Field field;
|
||||
try {
|
||||
SerializedLambda serializedLambda = getSerializedLambda();
|
||||
field = Class.forName(serializedLambda.getImplClass().replace("/", ".")).getDeclaredField(fieldName);
|
||||
// 从field取出字段名,可以根据实际情况调整
|
||||
TableField tableField = field.getAnnotation(TableField.class);
|
||||
if (tableField != null && tableField.value().length() > 0) {
|
||||
return tableField.value();
|
||||
} else {
|
||||
return fieldName.replaceAll("[A-Z]", "_$0").toLowerCase();
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException | NoSuchFieldException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
default String getMethodName() {
|
||||
return getSerializedLambda().getImplMethodName();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
default Class<?> getFieldClass() {
|
||||
return getReturnType();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
default SerializedLambda getSerializedLambda() {
|
||||
// 从function取出序列化方法
|
||||
Method method = getClass().getDeclaredMethod("writeReplace");
|
||||
method.setAccessible(true);
|
||||
return (SerializedLambda) method.invoke(this);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
default Class<?> getReturnType() {
|
||||
SerializedLambda lambda = getSerializedLambda();
|
||||
Class<?> className = Class.forName(lambda.getImplClass().replace("/", "."));
|
||||
Method method = className.getMethod(getMethodName());
|
||||
return method.getReturnType();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static <T,R> String getFieldInfo(FieldGetter<T, R> fieldGetter) {
|
||||
|
||||
return fieldGetter.getTableField();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static <T,R> String getFieldName( FieldGetter<T, R> fieldGetter) {
|
||||
return fieldGetter.getFieldName();
|
||||
|
||||
}
|
||||
|
||||
public static <T,R> Class<?> getFieldClass( FieldGetter<T, R> fieldGetter) {
|
||||
return fieldGetter.getFieldClass();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static <T,R> String getMethodName( FieldGetter<T, R> fieldGetter) {
|
||||
return fieldGetter.getMethodName();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue