eclipse-jdt / eclipse-jdt/eclipse.jdt.ui
Optional requiring a cast yields strange quickfixes
- 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`:

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

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
Assessment
This issue has not been assessed yet.