1 遇到这样的需求,在baseservice类中处理数据权限,子类可能使用QueryWrapper或者LambdaQueryWrapper调用base类的方法进行查询。
2 可以拿到的:PO的类,数据权限属性的属性名(是固定的)
直接上代码:
public static SFunction getSFunction(Class<?> entityClass, String fieldName) {
if (functionMap.containsKey(entityClass.getName() + fieldName)) {
return functionMap.get(entityClass.getName() + fieldName);
}
Field field = getDeclaredField(entityClass, fieldName);
if(field == null){
throw ExceptionUtils.mpe("This class %s is not have field %s ", entityClass.getName(), fieldName);
}
SFunction func = null;
final MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType methodType = MethodType.methodType(field.getType(), entityClass);
final CallSite site;
String getFunName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
site = LambdaMetafactory.altMetafactory(lookup,
"invoke",
MethodType.methodType(SFunction.class),
methodType,
lookup.findVirtual(entityClass, getFunName, MethodType.methodType(field.getType())),
methodType, FLAG_SERIALIZABLE);
func = (SFunction) site.getTarget().invokeExact();
functionMap.put(entityClass.getName() + field, func);
return func;
} catch (Throwable e) {
throw ExceptionUtils.mpe("This class %s is not have method %s ", entityClass.getName(), getFunName);
}
}