eclipse-jdt / eclipse-jdt/eclipse.jdt.ui

Optional requiring a cast yields strange quickfixes

Open
#759 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
59
Forks
127
Avg merge
22h 47m
Merged PRs (30d)
28

Description

Assume the following code

```
public T hmmmm(Callable callme) throws Exception {
return callme.call();
}
```

JDT offers me to cast the type to `T`:

![grafik](https://github.com/eclipse-jdt/eclipse.jdt.ui/assets/1331477/f93347c0-c177-4594-8a7d-94dda93d0eec)

but as soon as this is wrapped in an `Optional` call this does not work anymore:

![grafik](https://github.com/eclipse-jdt/eclipse.jdt.ui/assets/1331477/793e7190-1025-4010-9c60-a81e27398c2e)

Choosing then first or second quickfix simply wraps the optional but does not resolve the compile error

```
public Optional hmmmm(Callable callme) {
try {
return Optional.ofNullable(Optional.of(callme.call()));
} catch(Exception e) {
return Optional.empty();
}
}
```

Choosing the third option simply deletes all code an replace it with empty optional:

```
public Optional hmmmm(Callable callme) {
try {
return Optional.empty();
} catch(Exception e) {
return Optional.empty();
}
}
```

Choosing the last option cast the optional instead of the argument:

```
public Optional hmmmm(Callable callme) {
try {
return (Optional)Optional.of(callme.call());
} catch(Exception e) {
return Optional.empty();
}
}
```

Instead something like this is desired:
```
public Optional hmmmm(Callable callme) {

try {
return Optional.of((T)callme.call());
} catch(Exception e) {
return Optional.empty();
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.