Skip to content

Commit

Permalink
处理class匹配规则
Browse files Browse the repository at this point in the history
  • Loading branch information
Cjsah committed Sep 17, 2024
1 parent 18d61af commit 6a5307d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/main/java/dev/anvilcraft/rg/api/RGRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,17 @@ public RGRule(String namespace, Class<T> type, RGEnvironment environment, String
* @throws RuntimeException 当字段的类型不被支持时抛出
*/
public static Class<?> checkType(@NotNull Field field) {
// 检查并转换字段类型,从基本数据类型开始
if (field.getType() == boolean.class) return Boolean.class;
if (field.getType() == Boolean.class) return Boolean.class;
if (field.getType() == byte.class) return Byte.class;
if (field.getType() == Byte.class) return Byte.class;
if (field.getType() == short.class) return Short.class;
if (field.getType() == Short.class) return Short.class;
if (field.getType() == int.class) return Integer.class;
if (field.getType() == Integer.class) return Integer.class;
if (field.getType() == long.class) return Long.class;
if (field.getType() == Long.class) return Long.class;
if (field.getType() == float.class) return Float.class;
if (field.getType() == Float.class) return Float.class;
if (field.getType() == double.class) return Double.class;
if (field.getType() == Double.class) return Double.class;
// 支持String类型
if (field.getType() == String.class) return String.class;
// 对于不支持的类型,抛出异常
throw new RuntimeException("Field %s has unsupported type %s".formatted(field.getName(), field.getType()));
return switch (field.getType().getTypeName()) {
case "boolean", "java.lang.Boolean" -> Boolean.class;
case "byte", "java.lang.Byte" -> Byte.class;
case "short", "java.lang.Short" -> Short.class;
case "int", "java.lang.Integer" -> Integer.class;
case "long", "java.lang.Long" -> Long.class;
case "float", "java.lang.Float" -> Float.class;
case "double", "java.lang.Double" -> Double.class;
case "java.lang.String" -> String.class;
default -> throw new RGRuleException("Field %s has unsupported type %s", field.getName(), field.getType().getTypeName());
};
}


Expand Down
7 changes: 7 additions & 0 deletions src/main/java/dev/anvilcraft/rg/api/RGRuleException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.anvilcraft.rg.api;

public class RGRuleException extends RuntimeException {
public RGRuleException(String msg, Object... args) {
super(String.format(msg, args));
}
}

0 comments on commit 6a5307d

Please sign in to comment.