mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 00:17:04 +00:00
perf[orm]: throw en exception
This commit is contained in:
1 parent
6f33e8bfcb
commit
99a77172f6
1 file changed
+59
-38
@@ -163,7 +163,7 @@ public class OrmManager implements IOrmManager {
|
||||
var indexTextDefMap = entityDef.getIndexTextDefMap();
|
||||
if (CollectionUtils.isNotEmpty(indexTextDefMap)) {
|
||||
AssertionUtils.isTrue(indexTextDefMap.size() == 1
|
||||
, StringUtils.format("一个集合的text索引[{}]只能有一个", JsonUtils.object2String(indexTextDefMap.keySet())));
|
||||
, StringUtils.format("A collection can have only one text index [{}]", JsonUtils.object2String(indexTextDefMap.keySet())));
|
||||
var collection = getCollection(entityDef.getClazz());
|
||||
for (var indexTextDef : indexTextDefMap.entrySet()) {
|
||||
var fieldName = indexTextDef.getKey();
|
||||
@@ -193,7 +193,8 @@ public class OrmManager implements IOrmManager {
|
||||
Type type = field.getGenericType();
|
||||
|
||||
if (!(type instanceof ParameterizedType)) {
|
||||
throw new RuntimeException(StringUtils.format("变量[{}]的类型不是泛型类", field.getName()));
|
||||
// 注入的变量类型需要是泛型类
|
||||
throw new RuntimeException(StringUtils.format("The variable [{}] is not of type generic", field.getName()));
|
||||
}
|
||||
|
||||
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
|
||||
@@ -202,7 +203,8 @@ public class OrmManager implements IOrmManager {
|
||||
IEntityCache<?, ?> entityCaches = entityCachesMap.get(entityClazz);
|
||||
|
||||
if (entityCaches == null) {
|
||||
throw new RunException("实体缓存对象不存在,请检查配置[entity-package:{}]和[entityCaches:{}]的位置是否正确", ormConfig.getEntityPackage(), entityClazz);
|
||||
// entity-package需要配置到可以扫描到EntityCache注解的包
|
||||
throw new RunException("The EntityCache object does not exist, please check that [entity-package:{}] and [entityCaches:{}] are configured in the correct position", ormConfig.getEntityPackage(), entityClazz);
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
@@ -229,10 +231,11 @@ public class OrmManager implements IOrmManager {
|
||||
public <E extends IEntity<?>> IEntityCache<?, E> getEntityCaches(Class<E> clazz) {
|
||||
var usable = allEntityCachesUsableMap.get(clazz);
|
||||
if (usable == null) {
|
||||
throw new RunException("没有定义[]的EntityCaches,无法获取", clazz.getCanonicalName());
|
||||
throw new RunException("EntityCaches that do not have [] defined", clazz.getCanonicalName());
|
||||
}
|
||||
if (!usable) {
|
||||
throw new RunException("Orm没有使用[]的EntityCaches,为了节省内存提前释放了它;只有使用EntityCachesInjection注解的Entity才能被动态获取", clazz.getCanonicalName());
|
||||
// Orm没有使用EntityCacheAutowired,为了节省内存提前释放了它;只有使用EntityCacheAutowired注解的Entity才能被动态获取
|
||||
throw new RunException("Orm does not use [] EntityCacheAutowired annotation, which are released in advance to save memory", clazz.getCanonicalName());
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
var entityCache = (IEntityCache<?, E>) entityCachesMap.get(clazz);
|
||||
@@ -311,7 +314,7 @@ public class OrmManager implements IOrmManager {
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("无法读取实体信息:" + e);
|
||||
throw new RuntimeException("No Entity was scanned:" + e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,7 +330,8 @@ public class OrmManager implements IOrmManager {
|
||||
var entityCache = clazz.getAnnotation(com.zfoo.orm.anno.EntityCache.class);
|
||||
var cache = entityCache.cache();
|
||||
var cacheStrategyOptional = cacheStrategies.stream().filter(it -> it.getStrategy().equals(cache.value())).findFirst();
|
||||
AssertionUtils.isTrue(cacheStrategyOptional.isPresent(), "实体类Entity[{}]没有找到缓存策略[{}]", clazz.getSimpleName(), cache.value());
|
||||
// Entity需要有@Cache注解的缓存策略
|
||||
AssertionUtils.isTrue(cacheStrategyOptional.isPresent(), "No Entity[{}] @Cache policy found[{}]", clazz.getSimpleName(), cache.value());
|
||||
|
||||
var cacheStrategy = cacheStrategyOptional.get();
|
||||
var cacheSize = cacheStrategy.getSize();
|
||||
@@ -338,7 +342,8 @@ public class OrmManager implements IOrmManager {
|
||||
|
||||
var persister = entityCache.persister();
|
||||
var persisterStrategyOptional = persisterStrategies.stream().filter(it -> it.getStrategy().equals(persister.value())).findFirst();
|
||||
AssertionUtils.isTrue(persisterStrategyOptional.isPresent(), "实体类Entity[{}]没有找到持久化策略[{}]", clazz.getSimpleName(), persister.value());
|
||||
// 实体类Entity需要有持久化策略
|
||||
AssertionUtils.isTrue(persisterStrategyOptional.isPresent(), "Entity[{}] No persistence strategy found[{}]", clazz.getSimpleName(), persister.value());
|
||||
|
||||
var persisterStrategy = persisterStrategyOptional.get();
|
||||
var indexDefMap = new HashMap<String, IndexDef>();
|
||||
@@ -349,7 +354,8 @@ public class OrmManager implements IOrmManager {
|
||||
if (indexAnnotation.ttlExpireAfterSeconds() > 0) {
|
||||
var fieldType = field.getGenericType();
|
||||
if (!(fieldType == Date.class || field.getGenericType().toString().equals("java.util.List<java.util.Date>"))) {
|
||||
throw new IllegalArgumentException(StringUtils.format("MongoDB规定TTL类型[{}]必须是Date,List<Date>的其中一种类型", field.getName()));
|
||||
// MongoDB规定TTL类型必须是Date,List<Date>的其中一种类型
|
||||
throw new IllegalArgumentException(StringUtils.format("MongoDB TTL type:[{}] must be Date or List<Date>", field.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,20 +375,21 @@ public class OrmManager implements IOrmManager {
|
||||
|
||||
private void analyze(Class<?> clazz) {
|
||||
// 是否实现了IEntity接口
|
||||
AssertionUtils.isTrue(IEntity.class.isAssignableFrom(clazz), "被[{}]注解标注的实体类[{}]没有实现接口[{}]", com.zfoo.orm.anno.EntityCache.class.getName(), clazz.getCanonicalName(), IEntity.class.getCanonicalName());
|
||||
AssertionUtils.isTrue(IEntity.class.isAssignableFrom(clazz), "The entity:[{}] annotated by the [{}] annotation does not implement the interface [{}]"
|
||||
, com.zfoo.orm.anno.EntityCache.class.getName(), clazz.getCanonicalName(), IEntity.class.getCanonicalName());
|
||||
// 实体类Entity必须被注解EntityCache标注
|
||||
AssertionUtils.notNull(clazz.getAnnotation(com.zfoo.orm.anno.EntityCache.class), "实体类Entity[{}]必须被注解[{}]标注", clazz.getCanonicalName(), com.zfoo.orm.anno.EntityCache.class.getName());
|
||||
AssertionUtils.notNull(clazz.getAnnotation(com.zfoo.orm.anno.EntityCache.class), "The Entity[{}] must be annotated with the annotation [{}].", clazz.getCanonicalName(), com.zfoo.orm.anno.EntityCache.class.getName());
|
||||
|
||||
// 校验entity格式
|
||||
checkEntity(clazz);
|
||||
|
||||
// 校验id字段和id()方法的格式
|
||||
// 校验id字段和id()方法的格式,一个Entity类只能有一个@Id注解
|
||||
var idFields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Id.class);
|
||||
AssertionUtils.isTrue(ArrayUtils.isNotEmpty(idFields) && idFields.length == 1
|
||||
, "实体类Entity[{}]必须只有且仅有一个Id注解(如果确实已经被Id注解标注,注意不要使用Storage的Id注解)", clazz.getSimpleName());
|
||||
, "The Entity[{}] must have only one Id annotation (if it is indeed marked with an Id annotation, be careful not to use the Stored Id annotation)", clazz.getSimpleName());
|
||||
var idField = idFields[0];
|
||||
// idField必须用private修饰
|
||||
AssertionUtils.isTrue(Modifier.isPrivate(idField.getModifiers()), "实体类Entity[{}]的id必须是private私有的", clazz.getSimpleName());
|
||||
AssertionUtils.isTrue(Modifier.isPrivate(idField.getModifiers()), "The id of the Entity[{}] must be private", clazz.getSimpleName());
|
||||
|
||||
// 随机给id字段赋值,然后调用id()方法,看看两者的返回值是不是一样的,避免出错
|
||||
var entityInstance = ReflectionUtils.newInstance(clazz);
|
||||
@@ -399,7 +406,7 @@ public class OrmManager implements IOrmManager {
|
||||
} else if (idFieldType.equals(String.class)) {
|
||||
idFiledValue = RandomUtils.randomString(10);
|
||||
} else {
|
||||
throw new RunException("orm现在仅支持int long float double String");
|
||||
throw new RunException("orm only supports int long float double String");
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(idField);
|
||||
@@ -407,11 +414,13 @@ public class OrmManager implements IOrmManager {
|
||||
var idMethodOptional = Arrays.stream(ReflectionUtils.getAllMethods(clazz)).filter(it -> it.getName().equalsIgnoreCase("id"))
|
||||
.filter(it -> it.getParameterCount() <= 0)
|
||||
.findFirst();
|
||||
AssertionUtils.isTrue(idMethodOptional.isPresent(), "实体类Entity[{}]必须重写id()方法", clazz.getSimpleName());
|
||||
// 实体类Entity必须重写id()方法
|
||||
AssertionUtils.isTrue(idMethodOptional.isPresent(), "The Entity[{}] must override the id() method", clazz.getSimpleName());
|
||||
var idMethod = idMethodOptional.get();
|
||||
ReflectionUtils.makeAccessible(idMethod);
|
||||
var idMethodReturnValue = ReflectionUtils.invokeMethod(entityInstance, idMethod);
|
||||
AssertionUtils.isTrue(idFiledValue.equals(idMethodReturnValue), "实体类Entity[{}]的id字段的返回值[field:{}]和id方法的返回值[method:{}]不相等,请检查id()方法实现是否正确"
|
||||
// 实体类Entity的id字段的返回值field和id方法的返回值method必须相等
|
||||
AssertionUtils.isTrue(idFiledValue.equals(idMethodReturnValue), "The return value id [field:{}] of the Entity[{}] and the return value id [method:{}] are not equal, please check whether the id() method is implemented correctly"
|
||||
, clazz.getSimpleName(), idFiledValue, idMethodReturnValue);
|
||||
|
||||
// 校验gvs()方法和svs()方法的格式
|
||||
@@ -427,7 +436,9 @@ public class OrmManager implements IOrmManager {
|
||||
.findFirst();
|
||||
// gvs和svs要实现都实现,不实现都不实现
|
||||
if (gvsMethodOptional.isEmpty() || svsMethodOptional.isEmpty()) {
|
||||
AssertionUtils.isTrue(gvsMethodOptional.isEmpty() && svsMethodOptional.isEmpty(), "实体类Entity[{}]的gvs和svs方法要实现都实现,不实现都不实现", clazz.getSimpleName());
|
||||
// 实体类Entity的gvs和svs方法要实现都实现,不实现都不实现
|
||||
AssertionUtils.isTrue(gvsMethodOptional.isEmpty() && svsMethodOptional.isEmpty()
|
||||
, "The gvs and svs methods of the Entity[{}] should be implemented together", clazz.getSimpleName());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -436,21 +447,23 @@ public class OrmManager implements IOrmManager {
|
||||
var vsValue = RandomUtils.randomLong();
|
||||
ReflectionUtils.invokeMethod(entityInstance, svsMethod, vsValue);
|
||||
var gvsReturnValue = ReflectionUtils.invokeMethod(entityInstance, gvsMethod);
|
||||
AssertionUtils.isTrue(gvsReturnValue.equals(vsValue), "实体类Entity[{}]的gvs方法和svs方法定义格式不正确", clazz.getSimpleName());
|
||||
// 实体类Entity的gvs方法和svs方法定义格式不正确
|
||||
AssertionUtils.isTrue(gvsReturnValue.equals(vsValue), "The gvs and svs methods of the Entity[{}] are not correctly", clazz.getSimpleName());
|
||||
}
|
||||
|
||||
private void checkEntity(Class<?> clazz) {
|
||||
// 是否为一个简单的javabean,为了防止不同层对象混用造成潜在的并发问题,特别是网络层和po层混用
|
||||
ReflectionUtils.assertIsPojoClass(clazz);
|
||||
// 不能是泛型类
|
||||
AssertionUtils.isTrue(ArrayUtils.isEmpty(clazz.getTypeParameters()), "[class:{}]不能是泛型类", clazz.getCanonicalName());
|
||||
AssertionUtils.isTrue(ArrayUtils.isEmpty(clazz.getTypeParameters()), "[class:{}] can't be a generic class", clazz.getCanonicalName());
|
||||
// 必须要有一个空的构造器
|
||||
ReflectionUtils.publicEmptyConstructor(clazz);
|
||||
|
||||
// 不能使用Storage的Index注解
|
||||
var storageIndexes = ReflectionUtils.getFieldsByAnnoNameInPOJOClass(clazz, "com.zfoo.storage.anno.Index");
|
||||
if (ArrayUtils.isNotEmpty(storageIndexes)) {
|
||||
throw new RunException("在Orm中只能使用Orm的Index注解,不能使用Storage的Index注解,为了避免不必要的误解和增强项目的健壮性,禁止这样使用");
|
||||
// 在Orm中只能使用Orm的Index注解,不能使用Storage的Index注解,为了避免不必要的误解和增强项目的健壮性,禁止这样使用
|
||||
throw new RunException("only the Index annotation of Orm can be used, not the Index annotation of Storage, and it is forbidden to use it in order to avoid unnecessary misunderstandings and enhance the robustness of the project");
|
||||
}
|
||||
|
||||
var filedList = ReflectionUtils.notStaticAndTransientFields(clazz);
|
||||
@@ -467,50 +480,56 @@ public class OrmManager implements IOrmManager {
|
||||
} else if (fieldType.isArray()) {
|
||||
// 是一个数组
|
||||
Class<?> arrayClazz = fieldType.getComponentType();
|
||||
AssertionUtils.isTrue(arrayClazz == byte.class, "ORM[class:{}]的数组类型只支持byte[]", clazz.getCanonicalName());
|
||||
// ORM的数组类型只支持byte[]
|
||||
AssertionUtils.isTrue(arrayClazz == byte.class, "The array type of ORM[class:{}] only supports byte[]", clazz.getCanonicalName());
|
||||
} else if (Set.class.isAssignableFrom(fieldType)) {
|
||||
AssertionUtils.isTrue(fieldType.equals(Set.class), "ORM[class:{}]类型声明不正确,必须是Set接口类型", clazz.getCanonicalName());
|
||||
// 必须是Set接口类型
|
||||
AssertionUtils.isTrue(fieldType.equals(Set.class), "[class:{}] type declaration is incorrect, and it must be of the Set interface type", clazz.getCanonicalName());
|
||||
|
||||
var type = field.getGenericType();
|
||||
AssertionUtils.isTrue(type instanceof ParameterizedType, "ORM[class:{}]类型声明不正确,不是泛型类[field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
// field必须泛型类
|
||||
AssertionUtils.isTrue(type instanceof ParameterizedType, "[class:{}] type declaration is incorrect, not a generic class[field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
|
||||
var types = ((ParameterizedType) type).getActualTypeArguments();
|
||||
AssertionUtils.isTrue(types.length == 1, "ORM[class:{}]中Set类型声明不正确,[field:{}]必须声明泛型类", clazz.getCanonicalName(), field.getName());
|
||||
|
||||
// 必须声明Set的泛型类
|
||||
AssertionUtils.isTrue(types.length == 1, "Set type declaration in [class:{}] is incorrect, and the generic class must be declared in [field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
checkSubEntity(clazz, types[0]);
|
||||
} else if (List.class.isAssignableFrom(fieldType)) {
|
||||
// 是一个List
|
||||
AssertionUtils.isTrue(fieldType.equals(List.class), "ORM[class:{}]类型声明不正确,必须是List接口类型", clazz.getCanonicalName());
|
||||
AssertionUtils.isTrue(fieldType.equals(List.class), "[class:{}] type declaration is incorrect, and it must be of the List interface type", clazz.getCanonicalName());
|
||||
|
||||
var type = field.getGenericType();
|
||||
AssertionUtils.isTrue(type instanceof ParameterizedType, "ORM[class:{}]类型声明不正确,不是泛型类[field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
// field必须泛型类
|
||||
AssertionUtils.isTrue(type instanceof ParameterizedType, "[class:{}] type declaration is incorrect, not a generic class[field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
|
||||
// 必须声明List的泛型类
|
||||
var types = ((ParameterizedType) type).getActualTypeArguments();
|
||||
AssertionUtils.isTrue(types.length == 1, "ORM[class:{}]中List类型声明不正确,[field:{}]必须声明泛型类", clazz.getCanonicalName(), field.getName());
|
||||
AssertionUtils.isTrue(types.length == 1, "List type declaration in [class:{}] is incorrect, and the generic class must be declared in [field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
|
||||
checkSubEntity(clazz, types[0]);
|
||||
} else if (Map.class.isAssignableFrom(fieldType)) {
|
||||
// 必须是Map接口类型
|
||||
if (!fieldType.equals(Map.class)) {
|
||||
throw new RunException("ORM[class:{}]类型声明不正确,必须是Map接口类型", clazz.getCanonicalName());
|
||||
throw new RunException("[class:{}] type declaration is incorrect, and it must be a Map interface type", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
var type = field.getGenericType();
|
||||
|
||||
if (!(type instanceof ParameterizedType)) {
|
||||
throw new RunException("ORM[class:{}]中数组类型声明不正确,[field:{}]不是泛型类", clazz.getCanonicalName(), field.getName());
|
||||
throw new RunException("Map type declaration in [class:{}] is incorrect, and the generic class must be declared in [field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
}
|
||||
|
||||
var types = ((ParameterizedType) type).getActualTypeArguments();
|
||||
|
||||
if (types.length != 2) {
|
||||
throw new RunException("ORM[class:{}]中数组类型声明不正确,[field:{}]必须声明泛型类", clazz.getCanonicalName(), field.getName());
|
||||
throw new RunException("Map type declaration in [class:{}] is incorrect, and the generic class must be declared in [field:{}]", clazz.getCanonicalName(), field.getName());
|
||||
}
|
||||
|
||||
var keyType = types[0];
|
||||
var valueType = types[1];
|
||||
|
||||
if (!String.class.isAssignableFrom((Class<?>) keyType)) {
|
||||
throw new RunException("ORM[class:{}]类型声明不正确,Map的key类型必须为String类型", clazz.getCanonicalName());
|
||||
throw new RunException("[class:{}] type declaration is incorrect, and the key type of the Map must be the String type", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
checkSubEntity(clazz, valueType);
|
||||
@@ -539,7 +558,8 @@ public class OrmManager implements IOrmManager {
|
||||
var keyType = types[0];
|
||||
var valueType = types[1];
|
||||
if (!String.class.isAssignableFrom((Class<?>) keyType)) {
|
||||
throw new RunException("ORM中Map的key必须是String类型");
|
||||
// ORM中Map的key必须是String类型
|
||||
throw new RunException("The key of the map in the ORM must be of the String type");
|
||||
}
|
||||
checkSubEntity(currentEntityClass, valueType);
|
||||
return;
|
||||
@@ -550,16 +570,17 @@ public class OrmManager implements IOrmManager {
|
||||
// do nothing
|
||||
return;
|
||||
} else if (clazz.getComponentType() != null) {
|
||||
// 是一个二维以上数组
|
||||
throw new RunException("ORM不支持多维数组或集合嵌套数组[type:{}]类型,仅支持一维数组", type);
|
||||
// ORM不支持多维数组或集合嵌套数组类型,仅支持一维数组
|
||||
throw new RunException("[type:{}] does not support multi-dimensional arrays or nested arrays, and only supports one-dimensional arrays", type);
|
||||
} else if (clazz.equals(List.class) || clazz.equals(Set.class) || clazz.equals(Map.class)) {
|
||||
throw new RunException("ORM不支持数组和集合联合使用[type:{}]类型", type);
|
||||
// ORM不支持集合嵌套数组类型
|
||||
throw new RunException("ORMs do not support the combination of arrays and collections with the [type:{}] type", type);
|
||||
} else {
|
||||
checkEntity(clazz);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new RunException("[type:{}]类型不正确", type);
|
||||
throw new RunException("[type:{}] is incorrect", type);
|
||||
}
|
||||
|
||||
private boolean isBaseType(Class<?> clazz) {
|
||||
|
||||
Reference in new issue
Block a user