mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-27 16:24:51 +00:00
perf[protocol]: 变量名称校验及说明
This commit is contained in:
@@ -41,6 +41,7 @@ import com.zfoo.net.task.TaskManager;
|
||||
import com.zfoo.net.task.model.ReceiveTask;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.exception.ExceptionUtils;
|
||||
import com.zfoo.protocol.util.AssertionUtils;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
@@ -351,11 +352,12 @@ public class PacketDispatcher implements IPacketDispatcher {
|
||||
public void registerPacketReceiverDefinition(Object bean) {
|
||||
var clazz = bean.getClass();
|
||||
|
||||
if (!ReflectionUtils.isPOJOClass(clazz)) {
|
||||
return;
|
||||
var methods = ReflectionUtils.getMethodsByAnnoInPOJOClass(clazz, PacketReceiver.class);
|
||||
|
||||
if (CollectionUtils.isNotEmpty(methods) && !ReflectionUtils.isPojoClass(clazz)) {
|
||||
logger.warn("消息注册类不是POJO类,父类的不会被扫描到");
|
||||
}
|
||||
|
||||
var methods = ReflectionUtils.getMethodsByAnnoInPOJOClass(clazz, PacketReceiver.class);
|
||||
for (var method : methods) {
|
||||
var paramClazzs = method.getParameterTypes();
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.exception;
|
||||
|
||||
/**
|
||||
* 不是一个POJO对象,POJO对象不应该继承别的类
|
||||
*
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class POJOException extends RuntimeException {
|
||||
|
||||
private static final String MESSAGE = "not a POJO object, can't extend other object";
|
||||
|
||||
private static final String HYPHEN = "-";//连接号,连接号与破折号的区别是,连接号的两头不用空格
|
||||
|
||||
private static final String LEFT_SQUARE_BRACKET = "[";//左方括号
|
||||
|
||||
private static final String RIGHT_SQUARE_BRACKET = "]";//右方括号
|
||||
|
||||
public POJOException() {
|
||||
super(POJOException.MESSAGE);
|
||||
}
|
||||
|
||||
public POJOException(String message) {
|
||||
super(POJOException.MESSAGE + POJOException.HYPHEN + POJOException.LEFT_SQUARE_BRACKET + message + POJOException.RIGHT_SQUARE_BRACKET);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -233,7 +233,7 @@ public class ProtocolAnalysis {
|
||||
|
||||
private static short checkProtocol(Class<?> clazz) throws IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
// 是否为一个简单的javabean
|
||||
AssertionUtils.isTrue(clazz.getSuperclass().equals(Object.class), "[class:{}]不是简单的javabean,不能继承别的类", clazz.getCanonicalName());
|
||||
ReflectionUtils.assertIsPojoClass(clazz);
|
||||
// 是否实现了IPacket接口
|
||||
AssertionUtils.isTrue(IPacket.class.isAssignableFrom(clazz), "[class:{}]没有实现接口[IPacket:{}]", clazz.getCanonicalName(), IPacket.class.getCanonicalName());
|
||||
// 不能是泛型类
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
package com.zfoo.protocol.util;
|
||||
|
||||
import com.zfoo.protocol.exception.POJOException;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -70,13 +70,24 @@ public abstract class ReflectionUtils {
|
||||
} while (targetClass != null && targetClass != Object.class);
|
||||
}
|
||||
|
||||
public static boolean isPOJOClass(Class<?> clazz) {
|
||||
public static boolean isPojoClass(Class<?> clazz) {
|
||||
return clazz.getSuperclass().equals(Object.class);
|
||||
}
|
||||
|
||||
public static void assertIsPOJOClass(Class<?> clazz) {
|
||||
if (!isPOJOClass(clazz)) {
|
||||
throw new POJOException(clazz.getName() + "不是简单的javabean");
|
||||
public static void assertIsPojoClass(Class<?> clazz) {
|
||||
if (!isPojoClass(clazz)) {
|
||||
throw new RunException("[class:{}]不是简单的javabean(POJO类不能继承别的类,但是可以继承其它接口)", clazz.getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标准的属性名称更加通用,前缀不能是is,否则属性名称在不同语言很难去统一get和set方法
|
||||
*/
|
||||
public static void assertIsStandardFieldName(Field field) {
|
||||
var fieldName = field.getName();
|
||||
if (fieldName.startsWith("is")) {
|
||||
throw new RunException("to avoid different get or set method in different language, [field:{}] can not be started with name of 'is' in class:[{}]"
|
||||
, field.getName(), field.getDeclaringClass().getCanonicalName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,9 +287,7 @@ public abstract class ReflectionUtils {
|
||||
public static String fieldToGetMethod(Class<?> clazz, Field field) {
|
||||
var fieldName = field.getName();
|
||||
|
||||
if (fieldName.startsWith("is")) {
|
||||
throw new RuntimeException(StringUtils.format("field:[{}] can not be name of 'is' in class:[{}]", field.getName(), clazz.getCanonicalName()));
|
||||
}
|
||||
assertIsStandardFieldName(field);
|
||||
|
||||
var methodName = "get" + StringUtils.capitalize(fieldName);
|
||||
|
||||
@@ -286,6 +295,7 @@ public abstract class ReflectionUtils {
|
||||
clazz.getDeclaredMethod(methodName, null);
|
||||
return methodName;
|
||||
} catch (NoSuchMethodException e) {
|
||||
// java的get方法对boolean值有可能对应get或者is,所以尝试获取两种不同的get方法,当两种都获取不到才抛异常
|
||||
}
|
||||
|
||||
methodName = "is" + StringUtils.capitalize(fieldName);
|
||||
@@ -300,9 +310,7 @@ public abstract class ReflectionUtils {
|
||||
public static String fieldToSetMethod(Class<?> clazz, Field field) {
|
||||
var fieldName = field.getName();
|
||||
|
||||
if (fieldName.startsWith("is")) {
|
||||
throw new RuntimeException(StringUtils.format("field:[{}] can not be name of 'is' in class:[{}]", field.getName(), clazz.getCanonicalName()));
|
||||
}
|
||||
assertIsStandardFieldName(field);
|
||||
|
||||
var methodName = "set" + StringUtils.capitalize(fieldName);
|
||||
try {
|
||||
|
||||
@@ -48,7 +48,7 @@ public class JsonToObjectConverter implements ConditionalGenericConverter {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ReflectionUtils.isPOJOClass(targetType.getType());
|
||||
return ReflectionUtils.isPojoClass(targetType.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user