出現異常的代碼(省略了與本文無關的代碼)
package utils;
import org.apache.commons.javaflow.utils.ReflectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import play.Play;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisConnectionException;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class RedisClient {
/* 封裝好的redis util方法。。。*/
public static <T> T queryByCache(String key, RedisCacheQuery<T> cacheQuery) throws IOException {
int second = 24 * 60 * 60;
String value = getAndExpireIfPresent(key, second);
T result;
if (value == null) {
result = cacheQuery.queryDatabase();
value = JsonHelper.toString(result);
setAndExpire(key, value, second);
}else {
// 這裏是調用了工具類方法,內部實現爲jackson
// ObjectMapper.readValue()
result = JsonHelper.fromString(value, cacheQuery.getGenericClass());
}
return result;
}
public interface RedisCacheQuery<T> {
@SuppressWarnings("unchecked")
default Class <T> getGenericClass() {
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
return (Class)params[0];
}
T queryDatabase();
}
}
調用方法,使用了lambda
public static void getAllCountry() throws Exception {
List list = RedisClient.queryByCache(REDIS_KEY,
()-> dataService.findAllGeoCountryIso();
});
ok(list);
}
然後調用時出現異常,然後百度了很多錯誤分析,有一篇分析的很好,https://www.cnblogs.com/GaiDynasty/p/3938288.html,但是這篇文章是以hibernate.baseDao介紹的,不符合我的要求。
但是裏面的一句話給了我啓發:範式應該在編譯的時候就指定,而不是運行時。
我猜想是不是動態的lambda造成的? 去查了相關的lambda資料,也沒有很明白的介紹,於是就根據自己的想法修改,用回了匿名類,發現果然比lambda要好些,然後一步一步debug,因爲時間關係,具體步驟就不上了,最終OK代碼如下(只貼了interface):
public interface RedisCacheQuery<T> {
@SuppressWarnings("unchecked")
default Class <T> getGenericClass() {
ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericInterfaces()[0];
Type type = parameterizedType.getActualTypeArguments()[0];
return (Class<T>) (type instanceof Class ? type : ((ParameterizedType) type).getRawType());
}
T queryDatabase();
}