mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-05 12:24:17 +00:00
fix[storage]: Fix record class generation protocol field sort bug.
This commit is contained in:
@@ -27,6 +27,7 @@ import io.netty.buffer.ByteBuf;
|
||||
import javassist.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
@@ -182,9 +183,6 @@ public abstract class EnhanceUtils {
|
||||
var fieldRegistrations = registration.getFieldRegistrations();
|
||||
|
||||
var packetClazz = constructor.getDeclaringClass();
|
||||
if (packetClazz.isRecord()) {
|
||||
fields = registration.getOriginalFields();
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
builder.append("{").append(packetClazz.getCanonicalName() + " packet = (" + packetClazz.getCanonicalName() + ")$2;");
|
||||
@@ -214,8 +212,10 @@ public abstract class EnhanceUtils {
|
||||
builder.append("{").append("if(!" + EnhanceUtils.byteBufUtilsReadBoolean + "){").append("return null;}");
|
||||
var packetClazz = constructor.getDeclaringClass();
|
||||
if (packetClazz.isRecord()) {
|
||||
var fields = registration.getOriginalFields();
|
||||
List<String> constructorParam = new ArrayList<>(fields.length);
|
||||
var fields = registration.getFields();
|
||||
var fieldNames = ProtocolAnalysis.getFields(packetClazz).stream().map(Field::getName).toList();
|
||||
List<String> constructorParam = fieldNames.stream().collect(Collectors.toList());
|
||||
|
||||
for (var i = 0; i < fields.length; i++) {
|
||||
var field = fields[i];
|
||||
var fieldRegistration = fieldRegistrations[i];
|
||||
@@ -225,7 +225,8 @@ public abstract class EnhanceUtils {
|
||||
}
|
||||
|
||||
var readObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration);
|
||||
constructorParam.add(readObject);
|
||||
int index = fieldNames.indexOf(field.getName());
|
||||
constructorParam.set(index, readObject);
|
||||
}
|
||||
|
||||
builder.append(packetClazz.getCanonicalName() + " packet=new " + packetClazz.getCanonicalName() + "(" + constructorParam.stream().collect(Collectors.joining(StringUtils.COMMA)) + ");");
|
||||
|
||||
@@ -378,10 +378,8 @@ public class ProtocolAnalysis {
|
||||
GenerateProtobufUtils.clear();
|
||||
}
|
||||
|
||||
private static Entry<ArrayList<Field>, List<Field>> customFieldOrder(Class<?> clazz) {
|
||||
var notCompatibleFields = new ArrayList<Field>();
|
||||
var compatibleFieldMap = new HashMap<Integer, Field>();
|
||||
List<Field> originalFields = new ArrayList<>();
|
||||
public static List<Field> getFields(Class<?> clazz) {
|
||||
var fields = new ArrayList<Field>();
|
||||
for (var field : clazz.getDeclaredFields()) {
|
||||
var modifiers = field.getModifiers();
|
||||
if (Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers)) {
|
||||
@@ -395,6 +393,15 @@ public class ProtocolAnalysis {
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
fields.add(field);
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
private static List<Field> customFieldOrder(Class<?> clazz, List<Field> fields) {
|
||||
var notCompatibleFields = new ArrayList<Field>();
|
||||
var compatibleFieldMap = new HashMap<Integer, Field>();
|
||||
for (var field : fields) {
|
||||
if (field.isAnnotationPresent(Compatible.class)) {
|
||||
var order = field.getAnnotation(Compatible.class).order();
|
||||
var oldField = compatibleFieldMap.put(order, field);
|
||||
@@ -402,7 +409,6 @@ public class ProtocolAnalysis {
|
||||
throw new RunException("[{}]协议号中的[field:{}]和[field:{}]不能有相同的Compatible顺序[order:{}]", clazz.getCanonicalName(), oldField.getName(), field.getName(), oldField, order);
|
||||
}
|
||||
} else {
|
||||
originalFields.add(field);
|
||||
notCompatibleFields.add(field);
|
||||
}
|
||||
}
|
||||
@@ -418,28 +424,25 @@ public class ProtocolAnalysis {
|
||||
.map(Map.Entry::getValue)
|
||||
.toList();
|
||||
notCompatibleFields.addAll(compatibleFields);
|
||||
return Map.entry(notCompatibleFields, originalFields);
|
||||
return notCompatibleFields;
|
||||
}
|
||||
|
||||
private static ProtocolRegistration parseProtocolRegistration(Class<?> clazz, ProtocolModule module) {
|
||||
var protocolId = ProtocolManager.protocolId(clazz);
|
||||
var declaredFields = getFields(clazz);
|
||||
// 对象需要被序列化的属性
|
||||
var fieldsEntry = customFieldOrder(clazz);
|
||||
var fields = customFieldOrder(clazz, declaredFields);
|
||||
|
||||
try {
|
||||
var registrationList = new ArrayList<IFieldRegistration>();
|
||||
List<Field> fields = fieldsEntry.getKey();
|
||||
boolean isRecord = clazz.isRecord();
|
||||
if (isRecord) {
|
||||
fields = fieldsEntry.getValue();
|
||||
}
|
||||
for (var field : fields) {
|
||||
registrationList.add(toRegistration(clazz, field));
|
||||
}
|
||||
|
||||
Constructor constructor;
|
||||
if (isRecord) {
|
||||
constructor = ReflectionUtils.getConstructor(clazz, fields.stream().map(p -> p.getType()).toList().toArray(new Class[]{}));
|
||||
constructor = ReflectionUtils.getConstructor(clazz, declaredFields.stream().map(p -> p.getType()).toList().toArray(new Class[]{}));
|
||||
} else {
|
||||
constructor = clazz.getDeclaredConstructor();
|
||||
}
|
||||
@@ -448,12 +451,7 @@ public class ProtocolAnalysis {
|
||||
var protocol = new ProtocolRegistration();
|
||||
protocol.setId(protocolId);
|
||||
protocol.setConstructor(constructor);
|
||||
if (isRecord) {
|
||||
protocol.setFields(ArrayUtils.listToArray(fieldsEntry.getValue(), Field.class));
|
||||
protocol.setOriginalFields(ArrayUtils.listToArray(fieldsEntry.getValue(), Field.class));
|
||||
} else {
|
||||
protocol.setFields(ArrayUtils.listToArray(fieldsEntry.getKey(), Field.class));
|
||||
}
|
||||
protocol.setFields(ArrayUtils.listToArray(fields, Field.class));
|
||||
protocol.setFieldRegistrations(ArrayUtils.listToArray(registrationList, IFieldRegistration.class));
|
||||
protocol.setModule(module.getId());
|
||||
return protocol;
|
||||
|
||||
@@ -38,8 +38,6 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
*/
|
||||
private IFieldRegistration[] fieldRegistrations;
|
||||
|
||||
private Field[] originalFields;
|
||||
|
||||
public ProtocolRegistration() {
|
||||
|
||||
}
|
||||
@@ -59,7 +57,6 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
return constructor;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void write(ByteBuf buffer, Object packet) {
|
||||
if (packet == null) {
|
||||
@@ -99,7 +96,6 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
public short getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -124,14 +120,6 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
public Field[] getOriginalFields() {
|
||||
return originalFields;
|
||||
}
|
||||
|
||||
public void setOriginalFields(Field[] originalFields) {
|
||||
this.originalFields = originalFields;
|
||||
}
|
||||
|
||||
public IFieldRegistration[] getFieldRegistrations() {
|
||||
return fieldRegistrations;
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K> List<V> getIndexes(Func1<V, ?> func, K key) {
|
||||
public <INDEX> List<V> getIndexes(Func1<V, ?> func, INDEX key) {
|
||||
String indexName = LambdaUtils.getFieldName(func);
|
||||
var indexValues = indexMap.get(indexName);
|
||||
AssertionUtils.notNull(indexValues, "The index of [indexName:{}] does not exist in the static resource [resource:{}]", indexName, clazz.getSimpleName());
|
||||
@@ -172,7 +172,7 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <K, V> V getUniqueIndex(Func1<V, ?> func, K key) {
|
||||
public <INDEX, V> V getUniqueIndex(Func1<V, ?> func, INDEX key) {
|
||||
String uniqueIndexName = LambdaUtils.getFieldName(func);
|
||||
var indexValueMap = uniqueIndexMap.get(uniqueIndexName);
|
||||
AssertionUtils.notNull(indexValueMap, "There is no a unique index for [uniqueIndexName:{}] in the static resource [resource:{}]", uniqueIndexName, clazz.getSimpleName());
|
||||
|
||||
@@ -50,11 +50,10 @@ public interface IStorage<K, V> {
|
||||
|
||||
IdDef getIdDef();
|
||||
|
||||
@Nullable
|
||||
<K> List<V> getIndexes(Func1<V, ?> function, K key);
|
||||
<INDEX> List<V> getIndexes(Func1<V, ?> function, INDEX key);
|
||||
|
||||
@Nullable
|
||||
<K, V> V getUniqueIndex(Func1<V, ?> function, K key);
|
||||
<INDEX, V> V getUniqueIndex(Func1<V, ?> function, INDEX key);
|
||||
|
||||
int size();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user