Skip to content

Commit

Permalink
fix: SimpleFocessReader cannot new Set instance
Browse files Browse the repository at this point in the history
  • Loading branch information
MidCoard committed Mar 24, 2022
1 parent 8d1ddc9 commit 650c76e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ latest.log
FocessQQ-*.jar
focess-qq-*.jar
config.yml
temp.json
temp.json
ts.obj
54 changes: 28 additions & 26 deletions src/main/java/top/focess/qq/core/serialize/SimpleFocessReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,58 @@ public class SimpleFocessReader extends FocessReader {

private static final PureJavaReflectionProvider PROVIDER = new PureJavaReflectionProvider();

private static final Map<Class<?>,ReaderT<?>> CLASS_READER_MAP = Maps.newHashMap();
private static final Map<Class<?>, Reader<?>> CLASS_READER_MAP = Maps.newHashMap();

static {
CLASS_READER_MAP.put(ArrayList.class, (Reader<ArrayList>) (t, reader) -> {
ArrayList list = new ArrayList();
int length = reader.readInt();
for (int i = 0;i<length;i++)
t.add(reader.readObject());
list.add(reader.readObject());
return list;
});

CLASS_READER_MAP.put(LinkedList.class, (Reader<LinkedList>) (t, reader) -> {
LinkedList list = new LinkedList();
int length = reader.readInt();
for (int i = 0;i<length;i++)
t.offer(reader.readObject());
list.offer(reader.readObject());
return list;
});

CLASS_READER_MAP.put(HashMap.class, (Reader<HashMap>) (t, reader) -> {
HashMap hashMap = new HashMap();
int length = reader.readInt();
for (int i = 0;i<length;i++)
t.put(reader.readObject(),reader.readObject());
hashMap.put(reader.readObject(),reader.readObject());
return hashMap;
});

CLASS_READER_MAP.put(TreeMap.class, (Reader<TreeMap>) (t, reader) -> {
TreeMap treeMap = new TreeMap();
int length = reader.readInt();
for (int i = 0;i<length;i++)
t.put(reader.readObject(),reader.readObject());
treeMap.put(reader.readObject(),reader.readObject());
return treeMap;
});

CLASS_READER_MAP.put(HashSet.class, (Reader<HashSet>) (t, reader) -> {
HashSet hashSet = new HashSet();
int length = reader.readInt();
for (int i = 0;i<length;i++)
t.add(reader.readObject());
hashSet.add(reader.readObject());
return hashSet;
});

CLASS_READER_MAP.put(TreeSet.class, (Reader<TreeSet>) (t, reader) -> {
TreeSet treeSet = new TreeSet();
int length = reader.readInt();
for (int i = 0;i<length;i++)
t.add(reader.readObject());
treeSet.add(reader.readObject());
return treeSet;
});

CLASS_READER_MAP.put(Class.class, (ReaderT<Class>) (t, reader) -> {
CLASS_READER_MAP.put(Class.class, (Reader<Class>) (t, reader) -> {
try {
String cls = reader.readString();
switch (cls) {
Expand Down Expand Up @@ -90,9 +102,11 @@ public class SimpleFocessReader extends FocessReader {
});

CLASS_READER_MAP.put(ConcurrentHashMap.KeySetView.class,(Reader<ConcurrentHashMap.KeySetView>) (t, reader)->{
ConcurrentHashMap.KeySetView keySetView = ConcurrentHashMap.newKeySet();
int length = reader.readInt();
for (int i = 0;i<length;i++)
t.add(reader.readObject());
keySetView.add(reader.readObject());
return keySetView;
});
}

Expand Down Expand Up @@ -265,9 +279,9 @@ private <T> Object readObject() {
String className = readString();
try {
Class<T> cls = (Class<T>) PluginCoreClassLoader.forName(className);
ReaderT<T> reader;
if ((reader = (ReaderT<T>) CLASS_READER_MAP.get(cls)) != null)
return reader.readT(cls, this);
Reader<T> reader;
if ((reader = (Reader<T>) CLASS_READER_MAP.get(cls)) != null)
return reader.read(cls, this);
} catch (ClassNotFoundException e) {
throw new SerializationParseException(e);
}
Expand All @@ -277,20 +291,8 @@ private <T> Object readObject() {
}
}


private interface Reader<T> extends ReaderT<T>{
void read(T t, SimpleFocessReader reader) throws SerializationParseException;

@Override
default T readT(Class<T> cls, SimpleFocessReader reader) {
T t = (T) PROVIDER.newInstance(cls);
read(t,reader);
return t;
}
}

private interface ReaderT<T> {
T readT(Class<T> cls, SimpleFocessReader reader) throws SerializationParseException;
private interface Reader<T> {
T read(Class<T> cls, SimpleFocessReader reader) throws SerializationParseException;
}

}

0 comments on commit 650c76e

Please sign in to comment.