langtools/test/tools/javac/lambda/8074381/T8074381b.java
author mcimadamore
Thu, 19 Mar 2015 11:39:36 +0000
changeset 29553 716d47f5a45e
permissions -rw-r--r--
8074381: java.lang.AssertionError during compiling Summary: Add extra functional interface check to prevent crash during code generation Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29553
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     1
/*
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     2
 * @test /nodynamiccopyright/
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     3
 * @bug 8074381
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     4
 * @summary java.lang.AssertionError during compiling
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     5
 * @compile/fail/ref=T8074381b.out -XDrawDiagnostics T8074381b.java
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     6
 */
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     7
import java.util.function.BiConsumer;
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     8
import java.util.function.Consumer;
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
     9
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    10
class T8074381b {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    11
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    12
    @SuppressWarnings("unchecked")
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    13
    public Invocation resolve(Handler handler) {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    14
        return new Invocation((t) -> handler.handle((String) t));
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    15
    }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    16
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    17
    public static class Handler {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    18
        public void handle(String s) {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    19
            System.out.println(s);
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    20
        }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    21
    }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    22
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    23
    public static class Invocation<T> {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    24
        public final ThrowingConsumer<T> consumer;
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    25
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    26
        public Invocation(final ThrowingConsumer<T> consumer) {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    27
            this.consumer = consumer;
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    28
        }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    29
    }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    30
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    31
    @FunctionalInterface
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    32
    public interface ThrowingConsumer<T> extends BiConsumer<T,Consumer<Throwable>> {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    33
        @Override
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    34
        default void accept(final T elem, final Consumer<Throwable> errorHandler) {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    35
            try {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    36
                acceptThrows(elem);
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    37
            } catch (final Throwable e) {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    38
                errorHandler.accept(e);
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    39
            }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    40
        }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    41
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    42
        void acceptThrows(T elem) throws Throwable;
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    43
    }
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents:
diff changeset
    44
}