mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 00:17:04 +00:00
perf[orm]: 循环对象校验和格式语法分析校验
This commit is contained in:
1 parent
4eeba6d6d4
commit
1647b99ccf
7 files changed
+259
-116
No files matched your search
@@ -73,6 +73,8 @@ cpu: i9900k
|
||||
如果考虑支持修改协议类属性名称,必须让字段的读写顺序可控,这就需要注解来标识属性的顺序(protostuff就是这样做的),但是感觉这样不优雅。
|
||||
如果考虑支持字段增加和减少,需要消耗5%左右的性能(预估),并且增加一倍的包体积大小(写入字段的顺序),感觉不是非常划算。
|
||||
因为可以通过协议版本号来解决这个问题,所以去支持这样的增删操作动力并不是非常的大。
|
||||
|
||||
对于服务器来说协议一般有对内和对外的协议,对外protobuf用起来还行,但是服务器的内部调用protobuf用的就少了,用zfoo就可以统一对内和对外的协议。
|
||||
```
|
||||
|
||||
### Ⅵ. 协议规范
|
||||
|
||||
@@ -255,12 +255,8 @@ public class ProtocolAnalysis {
|
||||
AssertionUtils.isTrue(clazz.getSimpleName().matches("[a-zA-Z0-9_]*"), "[class:{}]的命名只能包含字母,数字,下划线", clazz.getCanonicalName(), PROTOCOL_ID);
|
||||
|
||||
// 必须要有一个空的构造器
|
||||
Constructor<?> constructor;
|
||||
try {
|
||||
constructor = clazz.getDeclaredConstructor();
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new UnknownException(e, "[class:{}]协议序列号[{}]必须有一个空的构造器", clazz.getCanonicalName(), PROTOCOL_ID);
|
||||
}
|
||||
Constructor<?> constructor = ReflectionUtils.publicEmptyConstructor(clazz);
|
||||
|
||||
ReflectionUtils.makeAccessible(protocolIdField);
|
||||
IPacket packet = (IPacket) constructor.newInstance();
|
||||
|
||||
@@ -468,7 +464,7 @@ public class ProtocolAnalysis {
|
||||
} else if (List.class.equals(clazz)) {
|
||||
// List<List<String>>
|
||||
IFieldRegistration registration = typeToRegistration(currentProtocolClass, ((ParameterizedType) type).getActualTypeArguments()[0]);
|
||||
return ListField.valueOf(registration, (ParameterizedType) type);
|
||||
return ListField.valueOf(registration, type);
|
||||
} else if (Map.class.equals(clazz)) {
|
||||
// Map<List<String>, List<String>>
|
||||
IFieldRegistration keyRegistration = typeToRegistration(currentProtocolClass, ((ParameterizedType) type).getActualTypeArguments()[0]);
|
||||
|
||||
@@ -14,6 +14,7 @@ package com.zfoo.protocol.util;
|
||||
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import com.zfoo.protocol.exception.UnknownException;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -26,6 +27,7 @@ import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
|
||||
/**
|
||||
* 反射工具类
|
||||
*
|
||||
@@ -81,6 +83,22 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Constructor<?> publicEmptyConstructor(Class<?> clazz) {
|
||||
Constructor<?> constructor;
|
||||
|
||||
try {
|
||||
constructor = clazz.getDeclaredConstructor();
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new UnknownException(e, "[class:{}] should have exactly one public zero-argument constructor", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
if (!Modifier.isPublic(constructor.getModifiers())) {
|
||||
throw new UnknownException("[class:{}] should have exactly one public zero-argument constructor", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
return constructor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 标准的属性名称更加通用,前缀不能是is,否则属性名称在不同语言很难去统一get和set方法
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user