Search in sources :

Example 6 with ErrorCode

use of org.rx.annotation.ErrorCode in project rxlib by RockyLOMO.

the class YamlConfiguration method readAs.

@ErrorCode("keyError")
@ErrorCode("partialKeyError")
public synchronized <T> T readAs(String key, Class<T> type, boolean throwOnEmpty) {
    Map<String, Object> tmp = yaml;
    if (key == null) {
        return convert(tmp, type);
    }
    Object val;
    if ((val = tmp.get(key)) != null) {
        return convert(val, type);
    }
    StringBuilder buf = new StringBuilder();
    String[] splits = Strings.split(key, Constants.CONFIG_KEY_SPLITS);
    int c = splits.length - 1;
    for (int i = 0; i <= c; i++) {
        if (buf.length() > 0) {
            buf.append(Constants.CONFIG_KEY_SPLITS);
        }
        String k = buf.append(splits[i]).toString();
        if ((val = tmp.get(k)) == null) {
            continue;
        }
        if (i == c) {
            return convert(val, type);
        }
        if ((tmp = as(val, Map.class)) == null) {
            throw new ApplicationException("partialKeyError", values(k, type));
        }
        buf.setLength(0);
    }
    if (throwOnEmpty) {
        throw new ApplicationException("keyError", values(key, type));
    }
    return null;
}
Also used : ApplicationException(org.rx.exception.ApplicationException) JSONObject(com.alibaba.fastjson.JSONObject) ErrorCode(org.rx.annotation.ErrorCode)

Aggregations

ErrorCode (org.rx.annotation.ErrorCode)6 ApplicationException (org.rx.exception.ApplicationException)5 InvalidException (org.rx.exception.InvalidException)3 JSONObject (com.alibaba.fastjson.JSONObject)1 Annotation (java.lang.annotation.Annotation)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 MessageFormat (java.text.MessageFormat)1 ParseException (java.text.ParseException)1 Map (java.util.Map)1 AccessLevel (lombok.AccessLevel)1 NoArgsConstructor (lombok.NoArgsConstructor)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Test (org.junit.jupiter.api.Test)1 Tuple (org.rx.bean.Tuple)1 org.rx.core (org.rx.core)1 Extends.as (org.rx.core.Extends.as)1