From 5197d63c70cdb7bbf59566042f1c39c9d03845a2 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 9 Apr 2018 17:28:26 +0800 Subject: [PATCH] =?UTF-8?q?RetLabel=E6=94=AF=E6=8C=81=E5=A4=9A=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=89=88=EF=BC=8C=20RetLoader=E7=9A=84load=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=BF=87=E6=9C=9F=EF=BC=8C=20=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E4=B8=BAloadMap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/service/RetLabel.java | 76 +++++++++++---------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/src/org/redkale/service/RetLabel.java b/src/org/redkale/service/RetLabel.java index 4844693b2..fb5d61f4d 100644 --- a/src/org/redkale/service/RetLabel.java +++ b/src/org/redkale/service/RetLabel.java @@ -6,8 +6,9 @@ package org.redkale.service; import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.reflect.*; -import java.text.MessageFormat; import java.util.*; /** @@ -23,59 +24,46 @@ import java.util.*; */ @Inherited @Documented -@Target(value = {ElementType.FIELD}) -@Retention(value = RetentionPolicy.RUNTIME) +@Target({FIELD}) +@Retention(RUNTIME) +@Repeatable(RetLabel.RetLabels.class) public @interface RetLabel { String value(); - public static class RetCode { + String locale() default ""; - protected final Map rets = new HashMap(); + @Inherited + @Documented + @Target({FIELD}) + @Retention(RUNTIME) + @interface RetLabels { - protected final Class clazz; - - protected boolean inited; - - public RetCode(Class clazz) { - this.clazz = clazz; - } - - public RetResult retResult(int retcode) { - if (retcode == 0) return RetResult.success(); - return new RetResult(retcode, retInfo(retcode)); - } - - public RetResult retResult(int retcode, Object... args) { - if (retcode == 0) return RetResult.success(); - if (args == null || args.length < 1) return new RetResult(retcode, retInfo(retcode)); - String info = MessageFormat.format(retInfo(retcode), args); - return new RetResult(retcode, info); - } - - public RetResult set(RetResult result, int retcode, Object... args) { - if (retcode == 0) return result.retcode(0).retinfo(""); - if (args == null || args.length < 1) return result.retcode(retcode).retinfo(retInfo(retcode)); - String info = MessageFormat.format(retInfo(retcode), args); - return result.retcode(retcode).retinfo(info); - } - - public String retInfo(int retcode) { - if (retcode == 0) return "成功"; - if (!this.inited) { - synchronized (this) { - if (!this.inited) { - rets.putAll(RetLoader.load(clazz)); - } - this.inited = true; - } - } - return rets.getOrDefault(retcode, "未知错误"); - } + RetLabel[] value(); } public static abstract class RetLoader { + public static Map> loadMap(Class clazz) { + final Map> rets = new HashMap<>(); + for (Field field : clazz.getFields()) { + if (!Modifier.isStatic(field.getModifiers())) continue; + if (field.getType() != int.class) continue; + RetLabel info = field.getAnnotation(RetLabel.class); + if (info == null) continue; + int value; + try { + value = field.getInt(null); + } catch (Exception ex) { + ex.printStackTrace(); + continue; + } + rets.computeIfAbsent(info.locale(), (k) -> new HashMap<>()).put(value, info.value()); + } + return rets; + } + + @Deprecated public static Map load(Class clazz) { final Map rets = new HashMap<>(); for (Field field : clazz.getFields()) {