All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feiniaojin.gracefulresponse.ExceptionAliasRegister Maven / Gradle / Ivy

package com.feiniaojin.gracefulresponse;

import com.feiniaojin.gracefulresponse.api.ExceptionAliasFor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author qinyujie
 */
public class ExceptionAliasRegister {

    private final Logger logger = LoggerFactory.getLogger(ExceptionAliasRegister.class);

    private ConcurrentHashMap, ExceptionAliasFor> aliasForMap = new ConcurrentHashMap<>();

    /**
     * 注册
     *
     * @param throwableClass
     * @return
     */
    public ExceptionAliasRegister doRegisterExceptionAlias(Class throwableClass) {

        ExceptionAliasFor exceptionAliasFor = throwableClass.getAnnotation(ExceptionAliasFor.class);
        if (exceptionAliasFor == null) {
            logger.error("注册了未加ExceptionAliasFor的异常,throwableClass={}", throwableClass);
            throw new GracefulResponseException();
        }

        Class[] classes = exceptionAliasFor.aliasFor();
        for (Class c : classes) {
            aliasForMap.put(c, exceptionAliasFor);
        }

        return this;
    }

    /**
     * 获取
     *
     * @param tClazz
     * @return
     */
    public ExceptionAliasFor getExceptionAliasFor(Class tClazz) {
        return aliasForMap.get(tClazz);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy