| Summary: | switch statement no NPE guard for String | ||
|---|---|---|---|
| Product: | [Modeling] TMF | Reporter: | Dietmar Stoll <btickets> |
| Component: | Xtext | Assignee: | Karsten Thoms <karsten.thoms> |
| Status: | RESOLVED FIXED | QA Contact: | |
| Severity: | normal | ||
| Priority: | P3 | CC: | karsten.thoms, lieven.lemiengre, tmf.xtext-inbox |
| Version: | 2.10.0 | Keywords: | triaged |
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Windows 7 | ||
| See Also: |
https://github.com/eclipse/xtext-extras/pull/59 https://github.com/eclipse/xtext-eclipse/pull/130 https://github.com/eclipse/xtext-extras/pull/75 https://github.com/eclipse/xtext-idea/pull/18 https://github.com/eclipse/xtext-xtend/pull/80 https://github.com/eclipse/xtext-web/pull/21 |
||
| Whiteboard: | v2.11 | ||
I think it should be the other way around. What is the reason for the special null-check in the case of the Integer type? I think it should be the other way around. What is the reason for the special null-check in the case of the Integer type? If the null-check would be missing, executing the switch with null would lead to a NPE.
This class produces a NPE for the String switch:
class TestBug501975 {
def static void foo() {
val Integer a = null
switch (a) {
case 1: {}
}
val String b = null
switch (b) {
case "World": {}
}
}
def static void main(String[] args) {
foo()
}
}
GitHub Pull Request 59 created by [kthoms] https://github.com/eclipse/xtext-extras/pull/59 Merged 7b8c045c434ffbbc7c8be7beca9cc01e205e5764 GitHub Pull Request 130 created by [kthoms] https://github.com/eclipse/xtext-eclipse/pull/130 GitHub Pull Request 75 created by [kthoms] https://github.com/eclipse/xtext-extras/pull/75 GitHub Pull Request 18 created by [kthoms] https://github.com/eclipse/xtext-idea/pull/18 GitHub Pull Request 80 created by [kthoms] https://github.com/eclipse/xtext-xtend/pull/80 GitHub Pull Request 21 created by [kthoms] https://github.com/eclipse/xtext-web/pull/21 |
The Java generator produces a NullPointerException guard for a switch over an Object, e.g. an Integer - but not for a String. The customer's expectation was that both should be treated in a similar way. ----- Xtend: def void foo() { val Integer a = null switch (a) { } } generated Java: public void foo() { final Integer a = null; if (a != null) { switch (a) { } } } ----- Xtend: def void foo() { val String a = null switch (a) { } } generated Java: public void foo() { final String a = null; switch (a) { } } Customer expected Java (similar to the Integer case): public void foo() { final String a = null; if (a != null) { switch (a) { } } } -----