From dc270d35c48aa8bf7808cdcf43e876a7df0b00cf Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Thu, 23 Jun 2022 15:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=8C=B9=E9=85=8D=E5=99=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4=E3=80=81=E7=B2=BE=E7=AE=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../asgc/neutrino/core/type/TypeDistance.java | 107 ------------------ .../neutrino/core/type/TypeMatchInfo.java | 2 +- .../neutrino/core/type/TypeMatchLevel.java | 18 ++- .../asgc/neutrino/core/util/ClassUtil.java | 28 +++++ .../asgc/neutrino/core/util/ReflectUtil.java | 6 +- .../fun/asgc/neutrino/core/util/TypeUtil.java | 5 +- .../asgc/neutrino/core/util/TypeUtilTest.java | 4 +- 7 files changed, 52 insertions(+), 118 deletions(-) delete mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeDistance.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeDistance.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeDistance.java deleted file mode 100644 index 37b2d88c..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeDistance.java +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.type; - -/** - * 类型距离 - * @author: aoshiguchen - * @date: 2022/6/16 - */ -public interface TypeDistance { - - /** - * 最小值,表示完全匹配 - */ - int MIN = TypeMatchLevel.NULL.getDistanceMin(); - - /** - * 最大值,表示完全不匹配 - */ - int MAX = Integer.MAX_VALUE; - - /** - * 空匹配 - */ - int NULL = MIN; - - /** - * 完全匹配 - */ - int PERFECT = TypeMatchLevel.PERFECT.getDistanceMin(); - - /** - * 完全不匹配 - */ - int NOT = MAX; - - /** - * 包装匹配 - */ - int PACKING = TypeMatchLevel.PACKING.getDistanceMin(); - - /** - * 解包装匹配 - */ - int UNPACKING = TypeMatchLevel.UNPACKING.getDistanceMin(); - - /** - * 类型提升起始值 - */ - int ASCENSION_MIN = TypeMatchLevel.ASCENSION.getDistanceMin(); - - /** - * 类型提升最大值 - */ - int ASCENSION_MAX = TypeMatchLevel.ASCENSION.getDistanceMax(); - - /** - * 超类匹配起始值 - * 每增加1层,值加1 - */ - int SUPER_MIN = TypeMatchLevel.SUPER.getDistanceMin(); - - /** - * 超类匹配结束值 - */ - int SUPER_MAX = TypeMatchLevel.SUPER.getDistanceMax(); - - /** - * 内部扩展起始值 - */ - int EXTENSION_MIN = TypeMatchLevel.EXTENSION.getDistanceMin(); - - /** - * 内部扩展结束值 - */ - int EXTENSION_MAX = TypeMatchLevel.EXTENSION.getDistanceMax(); - - /** - * 自定义类型转换起始值 - */ - int CUSTOM_MIN = TypeMatchLevel.CUSTOM.getDistanceMin(); - - /** - * 自定义类型转换结束值 - */ - int CUSTOM_MAX = TypeMatchLevel.CUSTOM.getDistanceMax(); -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchInfo.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchInfo.java index 0e4def94..b7771e72 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchInfo.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchInfo.java @@ -63,7 +63,7 @@ public class TypeMatchInfo { this.valueType = valueType; this.targetType = targetType; this.typeMatcher = typeMatcher; - this.typeDistance = TypeDistance.NOT; + this.typeDistance = TypeMatchLevel.NOT.getDistanceMin(); } public boolean isNotMatch() { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchLevel.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchLevel.java index 6eb15cc9..1f383ed2 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchLevel.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/type/TypeMatchLevel.java @@ -50,14 +50,28 @@ public enum TypeMatchLevel { // 超类匹配,当且仅当目标类是匹配类的超类时 SUPER(5, 1000000, 2000000, "超类匹配"), // 内置的扩展匹配 - EXTENSION(6, 10000000, 20000000, "扩展匹配"), + EXTENSION(6, 10000000, 50000000, "扩展匹配"), // 自定义匹配 - CUSTOM(7, 100000000, 200000000, "自定义匹配"); + CUSTOM(7, 100000000, 500000000, "自定义匹配"), + // 不匹配 + NOT(8, Integer.MAX_VALUE, Integer.MAX_VALUE, "不匹配"); private static Map levelMap = Stream.of(TypeMatchLevel.values()).collect(Collectors.toMap(TypeMatchLevel::getLevel, Function.identity())); + /** + * 匹配级别 + */ private int level; + /** + * 类型距离最小值 + */ private int distanceMin; + /** + * 类型距离最大值 + */ private int distanceMax; + /** + * 描述 + */ private String desc; public static TypeMatchLevel byLevel(int level) { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java index 0713eaa1..19123d4b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java @@ -210,4 +210,32 @@ public class ClassUtil { } return annotation; } + + public static boolean isPublic(Class clazz) { + return Modifier.isPublic(clazz.getModifiers()); + } + + public static boolean isPrivate(Class clazz) { + return Modifier.isPrivate(clazz.getModifiers()); + } + + public static boolean isProtected(Class clazz) { + return Modifier.isProtected(clazz.getModifiers()); + } + + public static boolean isFinal(Class clazz) { + return Modifier.isFinal(clazz.getModifiers()); + } + + public static boolean isAbstract(Class clazz) { + return Modifier.isAbstract(clazz.getModifiers()); + } + + public static boolean isStatic(Class clazz) { + return Modifier.isStatic(clazz.getModifiers()); + } + + public static boolean isInterface(Class clazz) { + return clazz.isInterface(); + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java index e0a56724..ee5810e6 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java @@ -25,7 +25,7 @@ package fun.asgc.neutrino.core.util; import com.google.common.collect.Sets; import fun.asgc.neutrino.core.cache.Cache; import fun.asgc.neutrino.core.cache.MemoryCache; -import fun.asgc.neutrino.core.type.TypeDistance; +import fun.asgc.neutrino.core.type.TypeMatchLevel; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -236,7 +236,7 @@ public class ReflectUtil { if (methodOptional.isPresent()) { return methodOptional.get(); } - int level = TypeDistance.MAX; + int level = TypeMatchLevel.NOT.getDistanceMax(); Method method = null; for (Method m : getMethods(field.getDeclaringClass())) { if (m.getName().equals(setMethodName) && m.getParameters().length == 1) { @@ -247,7 +247,7 @@ public class ReflectUtil { } } } - if (level < TypeDistance.MAX && method != null) { + if (level < TypeMatchLevel.NOT.getDistanceMin() && method != null) { return method; } return null; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/TypeUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/TypeUtil.java index b3cd45ce..7ec33816 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/TypeUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/TypeUtil.java @@ -25,7 +25,6 @@ package fun.asgc.neutrino.core.util; import com.google.common.collect.Lists; import fun.asgc.neutrino.core.type.TypeMatchInfo; import fun.asgc.neutrino.core.type.TypeMatchers; -import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Field; import java.util.*; @@ -74,7 +73,7 @@ public class TypeUtil { /** * 基本类型的映射 */ - public static final Map, Class> basicTypeMap = new HashMap, Class>() { + private static final Map, Class> basicTypeMap = new HashMap, Class>() { { this.put(byte.class, byte.class); this.put(short.class, short.class); @@ -99,7 +98,7 @@ public class TypeUtil { /** * 包装类型的映射 */ - public static final Map, Class> wrapTypeMap = new HashMap, Class>() { + private static final Map, Class> wrapTypeMap = new HashMap, Class>() { { this.put(byte.class, Byte.class); this.put(short.class, Short.class); diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/util/TypeUtilTest.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/util/TypeUtilTest.java index cd34bf2b..76e6de3c 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/util/TypeUtilTest.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/util/TypeUtilTest.java @@ -36,7 +36,7 @@ public class TypeUtilTest { public TypeMatchInfo match(Class clazz, Class targetClass) { TypeMatchInfo typeMatchInfo = new TypeMatchInfo(clazz, targetClass, this); if (clazz == String.class && targetClass == Animal.class) { - typeMatchInfo.setTypeDistance(TypeDistance.CUSTOM_MIN); + typeMatchInfo.setTypeDistance(TypeMatchLevel.CUSTOM.getDistanceMin() + 1); typeMatchInfo.setTypeConverter(((value, targetType) -> new Animal().setName((String)value))); } return typeMatchInfo; @@ -47,7 +47,7 @@ public class TypeUtilTest { public TypeMatchInfo match(Class clazz, Class targetClass) { TypeMatchInfo typeMatchInfo = new TypeMatchInfo(clazz, targetClass, this); if (clazz == String.class && targetClass == Cat.class) { - typeMatchInfo.setTypeDistance(TypeDistance.CUSTOM_MIN); + typeMatchInfo.setTypeDistance(TypeMatchLevel.CUSTOM.getDistanceMin() + 2); typeMatchInfo.setTypeConverter((value, targetType) -> { String[] tmp = ((String)value).split(":"); Cat cat = new Cat();