JDK 11的汇编失败,并为JDK 8编译



用JDK 8(1.8.0_212(编译代码,但无法使用JDK 11(11.0.3(校正Oracle JDK和Open JDK(AWS Corretto(

尝试使用Javac和Maven(Maven版本3.6.1和Maven-Compiler-Plugin版本3.8.0(编辑它的JDK 8,而JDK 11。

import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;
public class AppDemo {
    public static void main(String[] args) {
        // NO error here
        giveMeStream("http://foo.com").map(wrap(url -> new URL(url)));
        List<String> list = new ArrayList<String>();
        list.add("http://foo.com/, http://bar.com/");
        // error: unreported exception MalformedURLException;
        // must be caught or declared to be thrown
        list.stream().flatMap(
            urls -> Arrays.<String>stream(urls.split(",")).map(wrap(url -> new URL(url)))
        );
        // error: unreported exception MalformedURLException;
        // must be caught or declared to be thrown
        Stream.concat(
            giveMeStream("http://foo.com").map(wrap(url -> new URL(url))),
            giveMeStream("http://bar.com").map(wrap(url -> new URL(url))));
    }

    static Stream<String> giveMeStream(String s) {
        return Arrays.stream(new String[]{s});
    }
    static <T, R, E extends Throwable> Function<T, R>
    wrap(FunException<T, R, E> fn) {
        return t -> {
            try {
                return fn.apply(t);
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        };
    }
    interface FunException<T, R, E extends Throwable> {
        R apply(T t) throws E;
    }
}

错误:

Expected : No compilation error
Actual : compilation error for JDK11
Error message with JDK 11:
s.<String>stream(urls.split(",")).map(wrap(url -> new URL(url)))
                                                               ^
AppDemo.java:24: error: unreported exception MalformedURLException; must be caught or declared to be thrown
            giveMeStream("http://foo.com").map(wrap(url -> new URL(url))),
                                                           ^
AppDemo.java:25: error: unreported exception MalformedURLException; must be caught or declared to be thrown
            giveMeStream("http://bar.com").map(wrap(url -> new URL(url))));
                                                           ^
3 errors

,因为可能对规格的略有更新是这样说的。有关系吗?它不会这样工作。

在此处将异常变成参数化类型没有真正的目的。另外,您将使用此代码制作RuntimeException的链条。试试看:

static <T, R> Function<T, R> wrap(FunException<T, R> fn) {
    return t -> {
        try {
            return fn.apply(t);
        } catch (Error | RuntimeException ex) {
            throw ex;
        } catch (Throwable throwable) {
            throw new RuntimeException("Checked exception in lambda", throwable);
        }
    };
}
interface FunException<T, R> {
    R apply(T t) throws Throwable;
}

现在它将汇编罚款。

读者:不要这样做。处理Java规则(例如检查例外(的正确方法是处理它们。使用hacks来解决一种语言的本质,这仅意味着您的代码是非异常的(其他阅读您的代码的人都不会得到它,并且您将很难阅读他人的代码。这很糟糕(,倾向于与其他库的互动方式不好,并且应该有所帮助的各种功能(例如:在这里您会得到很多因果异常链,这些链使您的日志阅读和异常痕迹比所需的更加困难(。另外,这远处"脱离人迹罕至的路"会导致有趣的时期,例如用于编译不再编译的代码。

最新更新