From 2afb565f44f87a15f0a9c7cecffeedb35138743a Mon Sep 17 00:00:00 2001 From: zhouzh_zzz Date: Tue, 22 Aug 2023 15:09:44 +0800 Subject: [PATCH] Protobuf support: Avoid reflection Signed-off-by: zhouzh_zzz --- .../org/redisson/codec/ProtobufCodec.java | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/redisson/src/main/java/org/redisson/codec/ProtobufCodec.java b/redisson/src/main/java/org/redisson/codec/ProtobufCodec.java index 2291e8579..d6e440f3f 100644 --- a/redisson/src/main/java/org/redisson/codec/ProtobufCodec.java +++ b/redisson/src/main/java/org/redisson/codec/ProtobufCodec.java @@ -1,5 +1,6 @@ package org.redisson.codec; +import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig; import com.fasterxml.jackson.databind.ser.BasicSerializerFactory; import com.google.protobuf.MessageLite; import io.netty.buffer.ByteBuf; @@ -13,18 +14,13 @@ import org.redisson.client.codec.Codec; import org.redisson.client.handler.State; import org.redisson.client.protocol.Decoder; import org.redisson.client.protocol.Encoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.HashSet; -import java.util.Map; import java.util.Set; public class ProtobufCodec extends BaseCodec { - private static final Logger log = LoggerFactory.getLogger(ProtobufCodec.class); private final Class mapKeyClass; private final Class mapValueClass; private final Class valueClass; @@ -34,21 +30,6 @@ public class ProtobufCodec extends BaseCodec { //default value is JsonJacksonCodec private final Codec blacklistCodec; - private final static Set CLASSES_NOT_SUITABLE_FOR_PROTOBUF = new HashSet<>(); - - static { - try { - Field concreteField = BasicSerializerFactory.class.getDeclaredField("_concrete"); - concreteField.setAccessible(true); - CLASSES_NOT_SUITABLE_FOR_PROTOBUF.addAll(((Map) concreteField.get(BasicSerializerFactory.class)).keySet()); - Field _concreteLazyField = BasicSerializerFactory.class.getDeclaredField("_concreteLazy"); - _concreteLazyField.setAccessible(true); - CLASSES_NOT_SUITABLE_FOR_PROTOBUF.addAll(((Map) concreteField.get(BasicSerializerFactory.class)).keySet()); - } catch (NoSuchFieldException | IllegalAccessException ignored) { - log.warn("ProtobufCodec failed to retrieve classes not suitable for protobuf.Maybe some objects (like String which using StringSerializer is better) will be serialized with protobuf unless the protobuf blacklist is explicitly set."); - } - } - public ProtobufCodec(Class mapKeyClass, Class mapValueClass) { this(mapKeyClass, mapValueClass, null, null); } @@ -86,7 +67,7 @@ public class ProtobufCodec extends BaseCodec { } protobufBlacklist = new HashSet<>(); - protobufBlacklist.addAll(CLASSES_NOT_SUITABLE_FOR_PROTOBUF); + protobufBlacklist.addAll(BasicSerializerFactoryConcreteGetter.getConcreteKeySet()); protobufBlacklist.add("java.util.ArrayList"); protobufBlacklist.add("java.util.HashSet"); protobufBlacklist.add("java.util.HashMap"); @@ -206,5 +187,20 @@ public class ProtobufCodec extends BaseCodec { } + private static abstract class BasicSerializerFactoryConcreteGetter extends BasicSerializerFactory { + protected BasicSerializerFactoryConcreteGetter(SerializerFactoryConfig config) { + super(config); + } + private static Set getConcreteKeySet() { + Set concreteKeySet = new HashSet<>(); + if (_concrete != null && !_concrete.isEmpty()) { + concreteKeySet.addAll(_concrete.keySet()); + } + if (_concreteLazy != null && !_concreteLazy.isEmpty()) { + concreteKeySet.addAll(_concreteLazy.keySet()); + } + return concreteKeySet; + } + } } \ No newline at end of file