Compiler-Fehler Lambda als Argument kompiliert nicht

Rudolf

Bekanntes Mitglied
Hi,
ich habe einen Kompiler-Hinweis, obwohl der Code kompiliert. Komischerweise tritt dieser Fehler nur in Verbindung mit folgendem Code auf, leider konnte ich es nicht weiter runterbrechen. Mir ist der genaue Grund unklar. Vielleicht ist es nur ein IDE Problem. Ich benutze IntelliJ, wobei das ja nicht IDE-abhängig sein sollte.

Für den Code sind folgende Dependencies zu benutzen

Java:
@FunctionalInterface
public interface CheckedFunction<T, R, E extends Throwable> {
    R apply(T t) throws E;
}

Java:
@FunctionalInterface
public interface CheckedSupplier<T, E extends Throwable> {
    T get() throws E;
}

Java:
public class Possible<T> {

    private static final Possible<?> EMPTY = new Possible<>();
    private final T value;

    private Possible() {
        this.value = null;
    }

    private Possible(T value) {
        this.value = requireNonNull(value);
    }

    @SuppressWarnings("unchecked")
    public static <T> Possible<T> empty() {
        return (Possible<T>) EMPTY;
    }

    public static <T> Possible<T> of(T value) {
        return value == null ? empty() : new Possible<>(value);
    }

    public static <T> Possible<T> of(Optional<T> value) {
        return value.isPresent() ? new Possible<>(value.get()) : empty();
    }

    public <E extends Throwable> Possible<T> checkedOr(Class<E> exception, CheckedSupplier<Possible<T>, E> supplier) throws E {
        requireNonNull(exception);
        requireNonNull(supplier);
        if (value != null) {
            return supplier.get();
        } else {
            return this;
        }
    }

    public <U, E extends Throwable> Possible<U> checkedMap(Class<E> exception, CheckedFunction<? super T, ? extends U, E> mapper) throws E {
        requireNonNull(exception);
        requireNonNull(mapper);
        if (value != null) {
            return empty();
        } else {
            return Possible.of(mapper.apply(value));
        }
    }
}

Folgender Code zeigt einen Kompilierfehler an

Java:
public class App {

    public static void main(String[] args) throws Exception {
        Possible.of(new Object())
            .checkedOr(Exception.class, () -> Possible
                .of("")
                .checkedMap(Exception.class, s -> throwException()));
    }

    private static Object throwException() throws Exception {
        throw new Exception();
    }


}

Der gleiche Code ohne das Lambda zeigt keinen Fehler an

Java:
public class App {

    public static void main(String[] args) throws Exception {
        Possible.of(new Object())
            .checkedOr(Exception.class, () -> Possible.of("")
                .checkedMap(Exception.class, new CheckedFunction<String, Object, Exception>() {
                        @Override
                        public Object apply(String s) throws Exception {
                            return throwException();
                        }
                    }
                ));
    }

    private static Object throwException() throws Exception {
        throw new Exception();
    }
}

Beide Code Snippets kompilieren, obwohl ein Kompilierfehler in der IDE angezeigt wird.

Kann sich das jemand erklären?
 
Ist in der IDE Java 8 eingestellt?
(Mein Gedanke war, dass die IDE vllt auf Java 7 oder so eingestellt ist.)

Google meint:
In IntelliJ IDEA:

In File Menu → Project Structure → Project, change Project Language Level to 8.0 - Lambdas, type annotations etc.

Nur ne Idee, kein Anspruch auf Wahrheit oder ähnliches.
(Erst recht nicht zu dieser Uhrzeit ;-) )
 

Neue Themen


Zurück
Oben