this post was submitted on 12 Jun 2023
1 points (100.0% liked)

Java™ Community

508 readers
1 users here now

Write once, run anywhere (WORA)

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

The community

News, Technical discussions, research papers and assorted things of interest related to the Java Platform.

Rules
  1. Follow the Code of Conduct.
  2. Please make sure its a question/topic/post that would be new to most members. In Lemmy the publications are not closed, so you can continue to participate in them, regardless of the time they were published.
  3. Lemmy doesn’t have flags yet, so please tag your posts with [Question], [Help], [Learning], [JVM], [JSE], [OpenJDK], [Eclipse] or other you may think is appropriate.

Related communities

Resources

founded 5 years ago
MODERATORS
 

I am wondering if anyone can help me.

I have an issue with compiling some code in Eclipse but not with IntelliJ or javac.

I am using sneakyThrow to bubble a checked exception up through 2 streams.

Here is the smallest reproducible code I can make:

import java.io.IOException;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Example {
	public static void main(String[] args)
	throws IOException {
		List<List<Integer>> input = List.of(List.of(1, 2), List.of(2, 4));

		// Should return any List whose elements are all even.
		List<List<Integer>> output = input.stream()
			.filter(bubblePredicate(o -> o.stream()
				.allMatch(bubblePredicate(i -> {
					if (i > 10) {
						throw new IOException("Number too large.");
					}
					return i % 2 == 0;
				}))))
			.collect(Collectors.toList());

		System.out.println(output);
	}

	private interface ThrowingPredicate<S, E extends Exception> {
		boolean test(S s) throws E;
	}

	private static <S, E extends Exception> Predicate<S> bubblePredicate(ThrowingPredicate<S, E> callable)
	throws E {
		return s -> {
			try {
				return callable.test(s);
			}
			catch (Exception e) {
				sneakyThrow(e);
				return false;
			}
		};
	}

	private static <E extends Throwable> void sneakyThrow(Exception exception)
	throws E {
		throw (E)exception;
	}
}

Compiles and runs completely fine with javac 11.0.12, but doesn't on Eclipse 4.21.0.I20210906-0500 nor Eclipse 4.27.0.20230309-1200.

Has anyone encountered this before, or have any idea what I am misunderstanding?

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 1 points 1 year ago (1 children)

"Doesn't work", is a fairly useless description of the problem. Does it cause your computer to reboot? Does it spout haiku at you? Are you getting some kind of feedback that might indicate the nature of the problem? If so, you should share it, too.

[–] [email protected] 1 points 1 year ago (1 children)

Yeah mate, you are right "doesn't work" is pretty useless, lucky I didn't say that

I said I have code that does compile in one specific compiler but doesn't in another specific one, and asked if anyone had ever encountered that before

And 2 days ago when someone mentioned that I didn't include the actual error, I apologised and then included it

[–] [email protected] 1 points 1 year ago

I'm not going argue semantics with you - but you're making it harder to get help if you don't explain what "doesn't compile" means?

[–] [email protected] 0 points 1 year ago (1 children)

Can you share the actual error message that you get in Eclipse?

[–] [email protected] 0 points 1 year ago (1 children)

Oh yes, sorry

The error it gives is that the inner bubblePredicate has an Unhandled exception type IOException.

[–] [email protected] 2 points 1 year ago

Hm, that's really interesting. I downloaded Eclipse (haven't used it in years, IDEA ftw) and yes I can reproduce it. To me it looks like a compiler bug. From what I understand E should be inferred to RuntimeException, but it obviously is not. Maybe you could try to file a bug report on https://bugs.eclipse.org/