Class
@FunctionalInterfacepublic interface CacheFunction extends Serializable {/*** 这里只接收无参方法** @return*/Object apply();/*** 这个方法返回的SerializedLambda是重点** @return* @throws Exception*/default SerializedLambda getSerializedLambda() throws Exception {SerializedLambda obj = CacheCommonUtils.cacheFunctionClassMap.get(this.getClass().getName());if (null == obj) {//writeReplace改了好像会报异常Method write = this.getClass().getDeclaredMethod("writeReplace");write.setAccessible(true);obj = (SerializedLambda) write.invoke(this);CacheCommonUtils.cacheFunctionClassMap.put(this.getClass().getName(), obj);}return obj;}/*** getImplClass** @return*/default String getImplClass() {try {return getSerializedLambda().getImplClass();} catch (Exception e) {return null;}}/*** getImplMethodName** @return*/default String getImplMethodName() {try {return getSerializedLambda().getImplMethodName();} catch (Exception e) {return null;}}/*** 获取方法名称** @return*/default String fnToFnName() {try {String[] split = getSerializedLambda().getImplMethodName().split("\\$", 3);return split[1];} catch (Exception e) {}return "";}/*** 获取方法参数,反射已缓存,每次拿到的参数都是第一次调用时的** @return*/@Deprecateddefault List<Object> getFuncFields() {try {SerializedLambda serializedLambda = getSerializedLambda();List<Object> list = new ArrayList<>(serializedLambda.getCapturedArgCount());for (int i = 0; i < serializedLambda.getCapturedArgCount(); i++) {if (!serializedLambda.getCapturedArg(i).toString().startsWith("com.")) {list.add(serializedLambda.getCapturedArg(i));}}return list;} catch (Exception e) {}return new ArrayList<>();}}
介绍
在Lcache中,对cache的封装都是通过CacheFunction进行传递执行,大量的场景需要获取方法名称,单纯的反射即可获取;但是性能太低,因此通过Map将反射对象进行缓存,大大提高性能
Map
使用
String commands = function.fnToFnName();//通过数据结构Util还可获取方法的数据结构等信息String dataStructure = CommandsDataTypeUtil.getCommandsDataType(commands).name()
