0

I've encountered a weird behaviour in Eclipse when trying to chain method calls. The code behaves as expected, however I lose access to Open Declaration (the F3 / Ctrl+Left Click shortcut). I appear to only lose access to Open Declaration for calls that follow Optional::orElseThrow, i.e. the B::getC call in my example code below.

public class Main {

    public static void main(String[] args) {
        new A().getB()
                .stream()
                .filter(b -> b.getC().containsKey("key"))
                .findFirst()
                .orElseThrow(RuntimeException::new)
                .getC();
    }
}

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class A {
    public Collection<? extends B> getB() {
        List<B> l = new ArrayList<B>();
        l.add(new B());
        return l;
    }
}

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class B {
    public Map<String, ? extends Collection<? extends C>> getC() {
        Map<String, List<C>> m = new HashMap<>();
        m.put("key", List.of(new C()));
        return m;
    }
}

public class C {

}

When I use Ctrl+Left Click, nothing happens. But when I use F3 or highlight + right click + orElseThrow I at least get the message Current text selection cannot be opened in an editor. Maybe the problem is related to this How do I get rid of "Current text selection cannot be opened in an editor" in Eclipse?, but my test classes I created above are all in the same module, so the answer to that thread doesn't seem relevant...

What is even weirder is that if I change .orElseThrow(RuntimeException::new) to .orElseThrow(() -> new RuntimeException()), Open Declaration on .getC() begins to work again!

I tried creating a simpler example, further from my original code, but the code

new ArrayList<Integer>()
        .stream()
        .filter(v -> v > 2)
        .findFirst()
        .orElseThrow(RuntimeException::new)
        .intValue();

lets me use Open Declaration on .intValue() - but maybe this is because Integer is part of java.lang?

So, to summarize: is there a reason why I lose access to Open Declaration for method calls following Optional::orElseThrow when using my example classes A,B,C?

I run Eclipse IDE for Enterprise Java and Web Developers and version Version: 2022-03 (4.23.0)

C7K
  • 16
  • 3
  • It does sometimes seem to loose track of things in stuff like that. There may be some fixes in the current Eclipse 2022-09. – greg-449 Sep 16 '22 at 10:03
  • Make sure it has been reported [here](https://github.com/eclipse-jdt/eclipse.jdt.ui/issues), best with a patch. – howlger Sep 16 '22 at 11:51

0 Answers0