@com.arsframework.annotation.Nonnull

Here are the examples of the java api @com.arsframework.annotation.Nonnull taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

126 Examples 7

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取数据流请求体
 *
 * @param request Http请求对象
 * @return 请求体字符串
 * @throws IOException IO操作异常
 */
@Nonnull
public static String getBody(HttpServletRequest request) throws IOException {
    try (InputStream is = request.getInputStream()) {
        return new String(Streams.getBytes(is));
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取请求资源地址(不包含应用上下文地址)
 *
 * @param request Http请求对象
 * @return 资源地址
 */
@Nonnull
public static String getUri(HttpServletRequest request) {
    String uri = request.getRequestURI();
    String context = request.getContextPath();
    return context == null ? uri : uri.substring(context.length());
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 视图渲染
 *
 * @param request  Http请求对象
 * @param response Http响应对象
 * @param template 视图模板名称
 * @param context  渲染上下文数据
 * @throws IOException      IO操作异常
 * @throws ServletException Servlet操作异常
 */
@Nonnull
public static void render(HttpServletRequest request, HttpServletResponse response, String template, Map<String, Object> context) throws IOException, ServletException {
    try (OutputStream os = response.getOutputStream()) {
        render(request, response, template, context, os);
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 请求转发
 *
 * @param request  Http请求对象
 * @param response Http响应对象
 * @param path     转发路径
 * @throws IOException      IO操作异常
 * @throws ServletException Servlet操作异常
 */
@Nonnull
public static void forward(HttpServletRequest request, HttpServletResponse response, String path) throws IOException, ServletException {
    String context = request.getContextPath();
    if (context == null) {
        request.getRequestDispatcher(path).forward(request, response);
    } else {
        request.getRequestDispatcher(context + path).forward(request, response);
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取URL参数
 *
 * @param url 资源地址
 * @return 参数键/值映射
 */
@Nonnull
public static Map<String, Object> getParameters(String url) {
    int division = url.indexOf('?');
    return division < 0 ? new LinkedHashMap<>(0) : string2param(url.substring(division + 1));
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 初始化文件响应头
 *
 * @param response Http响应对象
 * @param name     文件名称
 */
@Nonnull
public static void initializeFileResponseHeader(HttpServletResponse response, String name) {
    try {
        name = URLEncoder.encode(name, Strings.CHARSET_UTF8);
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + name);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取HTTP请求的URL地址(不包含资源地址)
 *
 * @param request Http请求对象
 * @return URL地址
 */
@Nonnull
public static String getUrl(HttpServletRequest request) {
    StringBuilder url = new StringBuilder(request.getScheme()).append("://").append(request.getServerName()).append(':').append(request.getServerPort());
    String context = request.getContextPath();
    return context == null ? url.toString() : url.append(context).toString();
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取安全的Html(过滤掉script标签后的html内容)
 *
 * @param html Html内容
 * @return Html内容
 */
@Nonnull
public static String getSafeHtml(String html) {
    return html.isEmpty() ? html : SCRIPT_PATTERN.matcher(html).replaceAll(Strings.EMPTY_STRING);
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 响应文件
 *
 * @param response Http响应对象
 * @param file     文件对象
 * @param name     文件名称
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(HttpServletResponse response, File file, String name) throws IOException {
    initializeFileResponseHeader(response, name);
    try (OutputStream os = response.getOutputStream()) {
        Streams.write(file, os);
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 响应字符串
 *
 * @param response Http响应对象
 * @param value    字符串
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(HttpServletResponse response, String value) throws IOException {
    response.setHeader("Content-type", "text/plain;charset=UTF-8");
    try (OutputStream os = response.getOutputStream()) {
        os.write(value.getBytes(Strings.CHARSET_UTF8));
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 响应字节数组
 *
 * @param response Http响应对象
 * @param bytes    字节数组
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(HttpServletResponse response, byte[] bytes) throws IOException {
    response.setHeader("Content-type", "text/plain;charset=UTF-8");
    try (OutputStream os = response.getOutputStream()) {
        os.write(bytes);
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 请求重定向
 *
 * @param request  Http请求对象
 * @param response Http响应对象
 * @param path     重定向路径
 * @throws IOException IO操作异常
 */
@Nonnull
public static void redirect(HttpServletRequest request, HttpServletResponse response, String path) throws IOException {
    String context = request.getContextPath();
    if (context == null) {
        response.sendRedirect(path);
    } else {
        response.sendRedirect(context + path);
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取html中纯文本
 *
 * @param html html文本
 * @return 纯文本
 */
@Nonnull
public static String getHtmlText(String html) {
    try {
        return html.isEmpty() ? html : getHtmlText(new StringReader(html));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取Cookie
 *
 * @param request Http请求对象
 * @param name    Cookie名称
 * @return Cookie值
 */
@Nonnull
public static String getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                String value = cookie.getValue();
                try {
                    return Strings.isEmpty(value) ? null : URLDecoder.decode(value, Strings.CHARSET_UTF8);
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    return null;
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 视图渲染
 *
 * @param request  Http请求对象
 * @param response Http响应对象
 * @param template 视图模板名称
 * @param context  渲染上下文数据
 * @param output   数据输出流
 * @throws IOException      IO操作异常
 * @throws ServletException Servlet操作异常
 */
@Nonnull
public static void render(HttpServletRequest request, HttpServletResponse response, String template, Map<String, Object> context, OutputStream output) throws IOException, ServletException {
    template = template.replace("\\", "/").replace("//", "/");
    if (template.charAt(0) != '/') {
        template = new StringBuilder(Strings.ROOT_URI).append(template).toString();
    }
    if (!new File(ROOT_PATH, template).exists()) {
        throw new IOException("Template does not exist: " + template);
    }
    RequestDispatcher dispatcher = request.getRequestDispatcher(template);
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output));
    for (Map.Entry<String, Object> entry : context.entrySet()) {
        request.setAttribute(entry.getKey(), entry.getValue());
    }
    dispatcher.include(request, new HttpServletResponseWrapper(response) {

        @Override
        public PrintWriter getWriter() {
            return writer;
        }
    });
    writer.flush();
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 获取html中纯文本
 *
 * @param reader html数据流
 * @return 纯文本
 * @throws IOException IO操作异常
 */
@Nonnull
public static String getHtmlText(Reader reader) throws IOException {
    StringBuilder text = new StringBuilder();
    parserDelegator.parse(reader, new HTMLEditorKit.ParserCallback() {

        @Override
        public void handleText(char[] data, int pos) {
            text.append(data);
        }
    }, true);
    return text.toString();
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 响应文件
 *
 * @param response Http响应对象
 * @param file     文件对象
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(HttpServletResponse response, File file) throws IOException {
    write(response, file, file.getName());
}

19 Source : Webs.java
with Apache License 2.0
from arsframework

/**
 * 响应输入流
 *
 * @param response Http响应对象
 * @param input    输入流
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(HttpServletResponse response, InputStream input) throws IOException {
    response.setHeader("Content-type", "text/plain;charset=UTF-8");
    try (OutputStream os = response.getOutputStream()) {
        Streams.write(input, os);
    }
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 获取目标字符串在指定字符串中出现的次数,非正则表达式匹配
 *
 * @param source 源字符串
 * @param sign   目标字符串
 * @return 次数
 */
@Nonnull
public static int count(CharSequence source, CharSequence sign) {
    int count = 0;
    if (source.length() > 0 && sign.length() > 0) {
        source: for (int i = 0, slen = source.length(), tlen = sign.length(); i < slen; i++) {
            for (int j = 0, k = i + 0; j < tlen; j++, k = i + j) {
                if (k >= slen || source.charAt(k) != sign.charAt(j)) {
                    continue source;
                }
            }
            count++;
            i += tlen - 1;
        }
    }
    return count;
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 获取目标字符串在指定字符串中出现的次数,非正则表达式匹配
 *
 * @param source 源字符串
 * @param sign   目标字符串
 * @return 次数
 */
@Nonnull
public static int count(CharSequence source, char sign) {
    int count = 0;
    if (source.length() > 0) {
        for (int i = 0, slen = source.length(); i < slen; i++) {
            if (source.charAt(i) == sign) {
                count++;
            }
        }
    }
    return count;
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 将unicode字符串转换成字符数组
 *
 * @param unicode unicode字符串
 * @return 字符数组
 */
@Nonnull
public static char[] unicode2char(String unicode) {
    int index = -1, _index = -1;
    StringBuilder buffer = new StringBuilder();
    while ((index = unicode.indexOf("\\u", index + 1)) > -1) {
        int offset;
        if (_index > -1 && (offset = index - _index - 6) > 0) {
            if (offset > 0) {
                buffer.append(unicode.substring(_index + 6, _index + 6 + offset));
            }
        } else if (index > 0 && _index < 0) {
            buffer.append(unicode.substring(0, index));
        }
        buffer.append(hex2char(unicode.substring(index + 2, index + 6), 4));
        _index = index;
    }
    if (_index < 0) {
        return unicode.toCharArray();
    }
    if (_index + 6 < unicode.length()) {
        buffer.append(unicode.substring(_index + 6));
    }
    char[] chars = new char[buffer.length()];
    buffer.getChars(0, buffer.length(), chars, 0);
    return chars;
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 将16进制字符串按照2字符一单元转换成10进制字符
 *
 * @param hex   16进制字符串
 * @param radix 单元长度
 * @return 10进制字符数组
 */
@Nonnull
public static char[] hex2char(String hex, @Min(1) int radix) {
    char[] chars = new char[hex.length() / radix];
    for (int i = 0; i < chars.length; i++) {
        chars[i] = (char) Integer.parseInt(hex.substring(i * radix, (i + 1) * radix), 16);
    }
    return chars;
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 将16进制字符串转换成字节数组
 *
 * @param hex 16进制字符串
 * @return 字节数组
 */
@Nonnull
public static byte[] hex2byte(String hex) {
    byte[] bytes = new byte[hex.length() / 2];
    for (int i = 0, pos = 0; i < bytes.length; i++, pos = i * 2) {
        bytes[i] = (byte) (char2byte(hex.charAt(pos)) << 4 | char2byte(hex.charAt(pos + 1)));
    }
    return bytes;
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 格式化数字(四舍五入,保留2为小数)
 *
 * @param number 数字对象
 * @return 格式化数字字符串
 */
@Nonnull
public static String formatNumber(Number number) {
    return DEFAULT_DECIMAL_FORMAT.get().format(number);
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 将字符按照2字符一单元转换成16进制
 *
 * @param chars 字符数组
 * @param radix 单元长度
 * @return 16进制字符串
 */
@Nonnull
public static String char2hex(char[] chars, @Min(1) int radix) {
    StringBuilder buffer = new StringBuilder();
    for (char c : chars) {
        String s = Integer.toHexString(c);
        for (int i = 0, offset = radix - s.length(); i < offset; i++) {
            buffer.append('0');
        }
        buffer.append(s);
    }
    return buffer.toString();
}

19 Source : Strings.java
with Apache License 2.0
from arsframework

/**
 * 将字节数组转换16进制
 *
 * @param bytes 字节数组
 * @return 16进制字符串
 */
@Nonnull
public static String byte2hex(byte[] bytes) {
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < bytes.length; i++) {
        String hex = Integer.toHexString(bytes[i] & 0xFF);
        if (hex.length() == 1) {
            buffer.append('0');
        }
        buffer.append(hex);
    }
    return buffer.toString();
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 将文件数据写入到套接字写通道中
 *
 * @param source 源文件对象
 * @param target 目标输出流
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(File source, WritableByteChannel target) throws IOException {
    try (InputStream is = new FileInputStream(source)) {
        write(is, target);
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 将输入流中的数据写入输出流
 *
 * @param source 源输入流
 * @param target 目标输出流
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(InputStream source, OutputStream target) throws IOException {
    int n;
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    while ((n = source.read(buffer)) > 0) {
        target.write(buffer, 0, n);
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 将输入流中的数据写入套接字写通道
 *
 * @param source 源输入流
 * @param target 目标输出流
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(InputStream source, WritableByteChannel target) throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    while (source.read(buffer) > 0) {
        target.write(ByteBuffer.wrap(buffer));
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 从输入流中获取字节
 *
 * @param input 输入流
 * @return 字节数组
 * @throws IOException IO操作异常
 */
@Nonnull
public static byte[] getBytes(InputStream input) throws IOException {
    int n;
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        while ((n = input.read(buffer)) > 0) {
            bos.write(buffer, 0, n);
        }
        return bos.toByteArray();
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 对象反序列化,将对象的字节数组转换成对象
 *
 * @param bytes 字节数组
 * @return 对象
 * @throws IOException            IO操作异常
 * @throws ClreplacedNotFoundException 类不存在异常
 */
@Nonnull
public static Serializable deserialize(byte[] bytes) throws IOException, ClreplacedNotFoundException {
    return bytes.length == 0 ? null : (Serializable) new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 将文件数据写入到输出流
 *
 * @param source 源文件对象
 * @param target 目标输出流
 * @param append 是否追加
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(File source, OutputStream target, boolean append) throws IOException {
    try (InputStream is = new FileInputStream(source)) {
        write(is, target);
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 从文件中获取字节
 *
 * @param file 文件对象
 * @return 字节数组
 * @throws IOException IO操作异常
 */
@Nonnull
public static byte[] getBytes(File file) throws IOException {
    try (InputStream is = new FileInputStream(file)) {
        return getBytes(is);
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 对象序列化,将对象转换成字节数组
 *
 * @param object 需要转换的对象
 * @return 字节数组
 * @throws IOException IO操作异常
 */
@Nonnull
public static byte[] serialize(Serializable object) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (ObjectOutputStream os = new ObjectOutputStream(bos)) {
        os.writeObject(object);
    }
    return bos.toByteArray();
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 将输入流中的数据写入文件
 *
 * @param source 源输入流
 * @param target 目标文件对象
 * @param append 是否追加
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(InputStream source, File target, boolean append) throws IOException {
    Files.mkdirs(target.getParent());
    try (OutputStream os = new FileOutputStream(target, append)) {
        write(source, os);
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 将字节数据写入到文件
 *
 * @param source 源字节数据
 * @param target 目标文件对象
 * @param append 是否追加
 * @throws IOException IO操作异常
 */
@Nonnull
public static void write(byte[] source, File target, boolean append) throws IOException {
    Files.mkdirs(target.getParent());
    try (FileOutputStream os = new FileOutputStream(target, append)) {
        os.write(source);
    }
}

19 Source : Streams.java
with Apache License 2.0
from arsframework

/**
 * 对象反序列化,从数据输入流中获取对象的字节数据,并将字节数据转换成对象
 *
 * @param input 输入流
 * @return 对象
 * @throws IOException            IO操作异常
 * @throws ClreplacedNotFoundException 类不存在异常
 */
@Nonnull
public static Serializable deserialize(InputStream input) throws IOException, ClreplacedNotFoundException {
    return (Serializable) new ObjectInputStream(input).readObject();
}

19 Source : Secrets.java
with Apache License 2.0
from arsframework

/**
 * 构建AES密码处理器
 *
 * @param key  密钥
 * @param mode 处理类型(加密/解密)
 * @return 密码处理器
 * @throws GeneralSecurityException 密码处理异常
 */
@Nonnull
public static Cipher buildAESCipher(String key, int mode) throws GeneralSecurityException {
    Map<Integer, Cipher> ciphers = getCacheModeCiphers(AES, key);
    Cipher cipher = ciphers.get(mode);
    if (cipher == null) {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
        keyGenerator.init(128, new SecureRandom(key.getBytes()));
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), AES);
        cipher = Cipher.getInstance(AES);
        cipher.init(mode, secretKeySpec);
        ciphers.put(mode, cipher);
    }
    return cipher;
}

19 Source : Secrets.java
with Apache License 2.0
from arsframework

/**
 * 构建DES密码处理器
 *
 * @param key  密钥
 * @param mode 处理类型(加密/解密)
 * @return 密码处理器
 * @throws GeneralSecurityException 密码处理异常
 */
@Nonnull
public static Cipher buildDESCipher(String key, int mode) throws GeneralSecurityException {
    Map<Integer, Cipher> ciphers = getCacheModeCiphers(DES, key);
    Cipher cipher = ciphers.get(mode);
    if (cipher == null) {
        DESKeySpec spec = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey secretKey = keyFactory.generateSecret(spec);
        cipher = Cipher.getInstance(DES);
        cipher.init(mode, secretKey, new SecureRandom());
        ciphers.put(mode, cipher);
    }
    return cipher;
}

19 Source : Secrets.java
with Apache License 2.0
from arsframework

/**
 * AES解密
 *
 * @param source 数据源(base64)
 * @param key    秘钥
 * @return 明文
 * @throws GeneralSecurityException 解密异常
 */
@Nonnull
public static String unaes(String source, String key) throws GeneralSecurityException {
    return new String(buildAESCipher(key, Cipher.DECRYPT_MODE).doFinal(Base64.decodeBase64(source)));
}

19 Source : Secrets.java
with Apache License 2.0
from arsframework

/**
 * DES解密
 *
 * @param source 密文
 * @param key    秘钥
 * @return 明文
 * @throws GeneralSecurityException 解密异常
 */
@Nonnull
public static String undes(String source, String key) throws GeneralSecurityException {
    return new String(buildDESCipher(key, Cipher.DECRYPT_MODE).doFinal(Base64.decodeBase64(source)));
}

19 Source : Randoms.java
with Apache License 2.0
from arsframework

/**
 * 随机生成日期
 *
 * @param min 最小日期
 * @param max 最大日期
 * @return 日期
 */
@Nonnull
public static Date randomDate(@Lt("max") Date min, Date max) {
    long start = min.getTime();
    // 相差毫秒数
    long time = max.getTime() - start;
    if (time <= 1000) {
        // 相差1秒内
        return new Date(start + buildRandom().nextInt((int) time));
    }
    return new Date(start + buildRandom().nextInt((int) (time / 1000)) * 1000);
}

19 Source : Randoms.java
with Apache License 2.0
from arsframework

/**
 * 随机生成字符串
 *
 * @param chars  随机字符数组
 * @param length 字符串长度
 * @return 字符串
 */
@Nonnull
public static String randomString(Character[] chars, @Min(1) int length) {
    Random random = buildRandom();
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < length; i++) {
        buffer.append(chars[random.nextInt(chars.length)]);
    }
    return buffer.toString();
}

19 Source : Randoms.java
with Apache License 2.0
from arsframework

/**
 * 随机生成枚举项
 *
 * @param <T>  枚举类型
 * @param type 枚举类型
 * @return 枚举项
 */
@Nonnull
public static <T extends Enum<?>> T randomEnum(Clreplaced<T> type) {
    try {
        Object[] values = (Object[]) type.getMethod("values").invoke(type);
        return values.length == 0 ? null : (T) values[buildRandom().nextInt(values.length)];
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}

19 Source : Randoms.java
with Apache License 2.0
from arsframework

/**
 * 随机生成字符
 *
 * @param chars 随机字符数组
 * @return 字符
 */
@Nonnull
public static Character randomCharacter(Character[] chars) {
    return chars[buildRandom().nextInt(chars.length)];
}

19 Source : Objects.java
with Apache License 2.0
from arsframework

/**
 * 构建对象类型数组
 *
 * @param type   对象类型
 * @param length 数组长度
 * @param <T>    数据类型
 * @return 数组对象
 */
@Nonnull
public static <T> T[] buildArray(Clreplaced<T> type, @Min(0) int length) {
    return (T[]) Array.newInstance(isBasicClreplaced(type) ? getBasicWrapClreplaced(type) : type, length);
}

19 Source : Objects.java
with Apache License 2.0
from arsframework

/**
 * 根据泛型参数类型获取泛型类型
 *
 * @param parameterizedType 泛型参数类型
 * @return 泛型类型数组
 */
@Nonnull
private static Clreplaced<?>[] getGenericTypes(ParameterizedType parameterizedType) {
    Type[] types = parameterizedType.getActualTypeArguments();
    List<Clreplaced<?>> clreplacedes = new ArrayList<>(types.length);
    for (Type type : types) {
        if (type instanceof Clreplaced) {
            clreplacedes.add((Clreplaced<?>) type);
        }
    }
    return clreplacedes.toArray(EMPTY_CLreplaced_ARRAY);
}

19 Source : Objects.java
with Apache License 2.0
from arsframework

/**
 * 初始化对象实例
 *
 * @param <T>  数据类型
 * @param type 对象类型
 * @return 对象实例
 */
@Nonnull
public static <T> T initialize(Clreplaced<T> type) {
    try {
        return type.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

19 Source : Objects.java
with Apache License 2.0
from arsframework

/**
 * 获取目标异常信息
 *
 * @param throwable 异常对象
 * @return 信息内容
 */
@Nonnull
public static String getThrowableMessage(Throwable throwable) {
    Throwable cause;
    while ((cause = throwable.getCause()) != null) {
        throwable = cause;
    }
    return throwable.getMessage();
}

19 Source : Objects.java
with Apache License 2.0
from arsframework

/**
 * 以文件的形式来获取包下的所有Clreplaced
 *
 * @param pack 包名
 * @param path 包路径
 * @return 对象列表
 */
@Nonnull
private static List<Clreplaced<?>> getClreplacedes(String pack, String path) {
    File dir = new File(path);
    if (!dir.exists() || !dir.isDirectory()) {
        return new ArrayList<>(0);
    }
    List<Clreplaced<?>> clreplacedes = new ArrayList<>();
    ClreplacedLoader clreplacedLoader = Thread.currentThread().getContextClreplacedLoader();
    for (File file : dir.listFiles((file) -> file.isDirectory() || file.getName().endsWith(".clreplaced"))) {
        if (file.isDirectory()) {
            clreplacedes.addAll(getClreplacedes(pack + '.' + file.getName(), file.getAbsolutePath()));
        } else {
            // 如果是java类文件 去掉后面的.clreplaced 只留下类名
            String clreplacedName = file.getName().substring(0, file.getName().length() - 6);
            try {
                clreplacedes.add(clreplacedLoader.loadClreplaced(pack + '.' + clreplacedName));
            } catch (ClreplacedNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return clreplacedes;
}

See More Examples