/**
* Created by bruce on 2020/4/10 14:16
*/
@FunctionalInterface
public interface SerializableFunction<T, R> extends Function<T, R>, Serializable {
}
//方法引用
SerializableFunction<People, String> getName1 = People::getName;
Field field = ReflectionUtil.getField(getName1);
public class ReflectionUtil {
private static Map<SerializableFunction<?, ?>, Field> cache = new ConcurrentHashMap<>();
public static <T, R> String getFieldName(SerializableFunction<T, R> function) {
Field field = ReflectionUtil.getField(function);
return field.getName();
}
public static Field getField(SerializableFunction<?, ?> function) {
return cache.computeIfAbsent(function, ReflectionUtil::findField);
}
public static Field findField(SerializableFunction<?, ?> function) {
Field field = null;
String fieldName = null;
try {
// 第1步 获取SerializedLambda
Method method = function.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(function);
// 第2步 implMethodName 即为Field对应的Getter方法名
String implMethodName = serializedLambda.getImplMethodName();
if (implMethodName.startsWith("get") && implMethodName.length() > 3) {
fieldName = Introspector.decapitalize(implMethodName.substring(3));
} else if (implMethodName.startsWith("is") && implMethodName.length() > 2) {
fieldName = Introspector.decapitalize(implMethodName.substring(2));
} else if (implMethodName.startsWith("lambda$")) {
throw new IllegalArgumentException("SerializableFunction不能传递lambda表达式,只能使用方法引用");
} else {
throw new IllegalArgumentException(implMethodName + "不是Getter方法引用");
}
// 第3步 获取的Class是字符串,并且包名是“/”分割,需要替换成“.”,才能获取到对应的Class对象
String declaredClass = serializedLambda.getImplClass().replace("/", ".");
Class<?> aClass = Class.forName(declaredClass, false, ClassUtils.getDefaultClassLoader());
// 第4步 Spring 中的反射工具类获取Class中定义的Field
field = ReflectionUtils.findField(aClass, fieldName);
} catch (Exception e) {
e.printStackTrace();
}
// 第5步 如果没有找到对应的字段应该抛出异常
if (field != null) {
return field;
}
throw new NoSuchFieldError(fieldName);
}
}Serializable classes that need to designate an alternative object to be used when writing an object to the stream should implement this special method with the exact signature:
ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
This writeReplace method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules.
System.setProperty("jdk.internal.lambda.dumpProxyClasses", ".");
这里的缓存Key应该选用SerializableFunction#Class还是SerializableFunction实例对象好呢?
看到有些实现使用SerializableFunction的Class作为缓存key,代码如下:
public static Field getField(SerializableFunction<?, ?> function) {
//使用SerializableFunction的Class作为缓存key,导致每次都调用function.getClass()
return cache.computeIfAbsent(function.getClass(), ReflectionUtil::findField);
}
但是个人建议采用SerializableFunction对象,因为无论方法被调用多少次,方法代码块内的方法引用对象始终是同一个,如果采用其Class做为缓存key,每次查询缓存时都需要调用native方法function.getClass()获取其Class,也是一种资源损耗。
总结:Java如何通过方法引用获取属性名实现及思考至此结束。直接使用ReflectionUtil即可
本文转载自:https://blog.csdn.net/u013202238/article/details/105779686
















| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |