Skip to content

Commit

Permalink
improve 处理sure方法会报黄警告的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhan committed Sep 22, 2022
1 parent d121c12 commit 98c6a16
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions com/robot/LambdaExceptionUtil.java → com/robot/LambdaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* Lambda异常处理工具类
*
* @author luozhan
* @create 2019-05
* @date 2019-05
*/
public final class LambdaExceptionUtil {
public class LambdaUtil {

@FunctionalInterface
public interface ConsumerWithExceptions<T, E extends Exception> {
Expand Down Expand Up @@ -54,9 +54,10 @@ public interface BiPredicateWithExceptions<T, U, E extends Exception> {
/**
* 包装普通函数(Function)
* <p>
* 注:方法签名中的" throws E "编译器会提示多余,但其实是为了将具体的异常向外传递,如果不抛出的话
* 注:方法签名中的" throws E "编译器会提示多余,但其实是为了将实际的异常向外传递,如果不这么做
* 1.外层代码中编译器将无法提示有异常需要处理
* 2.也无法主动在外层捕获具体的异常,如果尝试try一个具体的异常,编译器将提示:在try语句体中永远不会抛出相应异常(Exception 'XXX' is never thrown in the corresponding try block)
* 2.也无法主动在外层捕获具体的异常,如果尝试try一个具体的异常,编译器将提示:
* 在try语句体中永远不会抛出相应异常(Exception 'XXX' is never thrown in the corresponding try block)
*/
public static <T, R, E extends Exception> Function<T, R> wrapFunction(FunctionWithExceptions<T, R, E> function) throws E {
return t -> {
Expand Down Expand Up @@ -164,12 +165,14 @@ public static <E extends Exception> void wrapRunnable(RunnableWithExceptions<E>
}

/**
* 如果一个方法绝对不会抛出所申明的异常,可以使用该方法进行包装
* 如果一段代码确保不会抛出所申明的异常,可以使用该方法进行包装
* 如:new String(byteArr, "UTF-8")申明了UnsupportedEncodingException,
* 但编码"UTF-8"是必定不会抛异常的,所以可以使用uncheck()进行包装
* String text = uncheck(() -> new String(byteArr, "UTF-8"))
* 但编码"UTF-8"是必定不会抛异常的,使用sure进行包装
* String text = sure(() -> new String(byteArr, "UTF-8"))
* <p>
* 注: sure方法有一定的风险,因为它隐藏了可能的异常申明,所以请谨慎使用,确保(sure)不会抛出异常才可以使用
*/
public static <R, E extends Exception> R uncheck(SupplierWithExceptions<R, E> supplier) {
public static <R, E extends Exception> R sure(SupplierWithExceptions<R, E> supplier) {
try {
return supplier.get();
} catch (Exception exception) {
Expand All @@ -180,11 +183,11 @@ public static <R, E extends Exception> R uncheck(SupplierWithExceptions<R, E> su
}

@SuppressWarnings("unchecked")
private static <E extends Throwable> void throwAsUnchecked(Exception exception) throws E {
private static <E extends Exception> void throwAsUnchecked(Exception exception) throws E {
throw (E) exception;
}
private static class UnreachableException extends RuntimeException{

private static class UnreachableException extends RuntimeException {

}

Expand Down

0 comments on commit 98c6a16

Please sign in to comment.