mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-13 08:16:04 +00:00
perf[storage]: orm和storage的注解的语法校验,避免犯小错误
This commit is contained in:
1 parent
3cdf9b6131
commit
9ae201abf6
4 files changed
+49
-20
No files matched your search
@@ -368,7 +368,8 @@ public class OrmManager implements IOrmManager {
|
||||
|
||||
// 校验id字段和id()方法的格式
|
||||
var idFields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Id.class);
|
||||
AssertionUtils.isTrue(ArrayUtils.isNotEmpty(idFields) && idFields.length == 1, "实体类Entity[{}]必须只有且仅有一个Id注解", clazz.getSimpleName());
|
||||
AssertionUtils.isTrue(ArrayUtils.isNotEmpty(idFields) && idFields.length == 1
|
||||
, "实体类Entity[{}]必须只有且仅有一个Id注解(如果确实已经被Id注解标注,注意不要使用Storage的Id注解)", clazz.getSimpleName());
|
||||
var idField = idFields[0];
|
||||
// idField必须用private修饰
|
||||
AssertionUtils.isTrue(Modifier.isPrivate(idField.getModifiers()), "实体类Entity[{}]的id必须是private私有的", clazz.getSimpleName());
|
||||
@@ -442,6 +443,11 @@ public class OrmManager implements IOrmManager {
|
||||
// 必须要有一个空的构造器
|
||||
ReflectionUtils.publicEmptyConstructor(clazz);
|
||||
|
||||
// 不能使用Storage的Index注解
|
||||
var storageIndexes = ReflectionUtils.getFieldsByAnnoNameInPOJOClass(clazz, "com.zfoo.storage.model.anno.Index");
|
||||
if (ArrayUtils.isNotEmpty(storageIndexes)) {
|
||||
throw new RunException("在Orm中只能使用Orm的Index注解,不能使用Storage的Index注解,为了避免不必要的误解和增强项目的健壮性,禁止这样使用");
|
||||
}
|
||||
|
||||
var filedList = Arrays.stream(clazz.getDeclaredFields())
|
||||
.filter(it -> !Modifier.isTransient(it.getModifiers()))
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@@ -65,8 +64,8 @@ public abstract class ReflectionUtils {
|
||||
// Keep backing up the inheritance hierarchy.
|
||||
Class<?> targetClass = clazz;
|
||||
do {
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
var fields = clazz.getDeclaredFields();
|
||||
for (var field : fields) {
|
||||
ReflectionUtils.filterField(field, fieldFilter, fieldCallback);
|
||||
}
|
||||
targetClass = targetClass.getSuperclass();
|
||||
@@ -118,9 +117,9 @@ public abstract class ReflectionUtils {
|
||||
* @return 数组,可能长度为0
|
||||
*/
|
||||
public static Field[] getFieldsByAnnoInPOJOClass(Class<?> clazz, Class<? extends Annotation> annotation) {
|
||||
List<Field> list = new ArrayList<Field>();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
var list = new ArrayList<Field>();
|
||||
var fields = clazz.getDeclaredFields();
|
||||
for (var field : fields) {
|
||||
if (field.isAnnotationPresent(annotation)) {
|
||||
list.add(field);
|
||||
}
|
||||
@@ -128,6 +127,24 @@ public abstract class ReflectionUtils {
|
||||
return ArrayUtils.listToArray(list, Field.class);
|
||||
}
|
||||
|
||||
public static Field[] getFieldsByAnnoNameInPOJOClass(Class<?> clazz, String annotationName) {
|
||||
var list = new ArrayList<Field>();
|
||||
var fields = clazz.getDeclaredFields();
|
||||
for (var field : fields) {
|
||||
var annotations = field.getAnnotations();
|
||||
if (ArrayUtils.isEmpty(annotations)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var annotation : annotations) {
|
||||
if (annotation.annotationType().getName().equals(annotationName)) {
|
||||
list.add(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ArrayUtils.listToArray(list, Field.class);
|
||||
}
|
||||
|
||||
public static Field getFieldByNameInPOJOClass(Class<?> clazz, String fieldName) {
|
||||
try {
|
||||
return clazz.getDeclaredField(fieldName);
|
||||
@@ -144,9 +161,9 @@ public abstract class ReflectionUtils {
|
||||
* @return 数组,可能长度为0
|
||||
*/
|
||||
public static Method[] getMethodsByAnnoInPOJOClass(Class<?> clazz, Class<? extends Annotation> annotation) {
|
||||
List<Method> list = new ArrayList<>();
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
var list = new ArrayList<Method>();
|
||||
var methods = clazz.getDeclaredMethods();
|
||||
for (var method : methods) {
|
||||
if (method.isAnnotationPresent(annotation)) {
|
||||
list.add(method);
|
||||
}
|
||||
@@ -155,9 +172,9 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
|
||||
public static Method[] getMethodsByNameInPOJOClass(Class<?> clazz, String methodName) {
|
||||
List<Method> list = new ArrayList<>();
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
var list = new ArrayList<Method>();
|
||||
var methods = clazz.getDeclaredMethods();
|
||||
for (var method : methods) {
|
||||
if (method.getName().equalsIgnoreCase(methodName)) {
|
||||
list.add(method);
|
||||
}
|
||||
@@ -174,10 +191,10 @@ public abstract class ReflectionUtils {
|
||||
*/
|
||||
public static Method[] getAllMethods(Class<?> clazz) {
|
||||
AssertionUtils.notNull(clazz, "Class must not be null");
|
||||
List<Method> list = new ArrayList<>();
|
||||
Class<?> superClazz = clazz;
|
||||
var list = new ArrayList<Method>();
|
||||
var superClazz = clazz;
|
||||
while (superClazz != null) {
|
||||
Method[] methods = superClazz.getDeclaredMethods();
|
||||
var methods = superClazz.getDeclaredMethods();
|
||||
Collections.addAll(list, methods);
|
||||
superClazz = superClazz.getSuperclass();
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ public class IdDef {
|
||||
private Field field;
|
||||
|
||||
public static IdDef valueOf(Class<?> clazz) {
|
||||
Field[] fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Id.class);
|
||||
var fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Id.class);
|
||||
if (fields.length <= 0) {
|
||||
throw new RunException("class[{}]没有主键Id注解", clazz.getName());
|
||||
throw new RunException("class[{}]没有被Id注解标识的主键(如果确实已经被Id注解标注,注意不要使用ORM的Id注解)", clazz.getName());
|
||||
}
|
||||
if (fields.length > 1) {
|
||||
throw new RunException("类[{}]的主键Id注解重复", clazz.getName());
|
||||
@@ -38,9 +38,9 @@ public class IdDef {
|
||||
if (fields[0] == null) {
|
||||
throw new RunException("不合法的Id资源映射对象:" + clazz.getName());
|
||||
}
|
||||
Field idField = fields[0];
|
||||
var idField = fields[0];
|
||||
ReflectionUtils.makeAccessible(idField);
|
||||
IdDef idDef = new IdDef();
|
||||
var idDef = new IdDef();
|
||||
idDef.setField(idField);
|
||||
return idDef;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
package com.zfoo.storage.model.vo;
|
||||
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import com.zfoo.protocol.util.ReflectionUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.model.anno.Index;
|
||||
@@ -44,6 +46,10 @@ public class IndexDef {
|
||||
var fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Index.class);
|
||||
var indexes = new ArrayList<IndexDef>(fields.length);
|
||||
|
||||
var ormIndexes = ReflectionUtils.getFieldsByAnnoNameInPOJOClass(clazz, "com.zfoo.orm.model.anno.Index");
|
||||
if (ArrayUtils.isNotEmpty(ormIndexes)) {
|
||||
throw new RunException("在Storage中只能使用Storage的Index注解,不能使用Orm的Index注解,为了避免不必要的误解和增强项目的健壮性,禁止这样使用");
|
||||
}
|
||||
|
||||
for (var field : fields) {
|
||||
IndexDef indexDef = new IndexDef(field);
|
||||
|
||||
Reference in new issue
Block a user