Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 501975

Summary: switch statement no NPE guard for String
Product: [Modeling] TMF Reporter: Dietmar Stoll <btickets>
Component: XtextAssignee: Karsten Thoms <karsten.thoms>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: karsten.thoms, lieven.lemiengre, tmf.xtext-inbox
Version: 2.10.0Keywords: 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

Description Dietmar Stoll CLA 2016-09-22 07:50:47 EDT
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) {
      }
    }
  }
-----
Comment 1 Lieven Lemiengre CLA 2016-09-23 04:48:08 EDT
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?
Comment 2 Lieven Lemiengre CLA 2016-09-23 04:48:29 EDT
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?
Comment 3 Karsten Thoms CLA 2016-09-28 04:59:42 EDT
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()
	}
}
Comment 4 Eclipse Genie CLA 2016-09-28 06:07:29 EDT
GitHub Pull Request 59 created by [kthoms]
https://github.com/eclipse/xtext-extras/pull/59
Comment 5 Karsten Thoms CLA 2016-09-28 10:41:17 EDT
Merged 7b8c045c434ffbbc7c8be7beca9cc01e205e5764
Comment 6 Eclipse Genie CLA 2016-12-01 05:14:04 EST
GitHub Pull Request 130 created by [kthoms]
https://github.com/eclipse/xtext-eclipse/pull/130
Comment 7 Eclipse Genie CLA 2016-12-01 05:53:16 EST
GitHub Pull Request 75 created by [kthoms]
https://github.com/eclipse/xtext-extras/pull/75
Comment 8 Eclipse Genie CLA 2016-12-01 06:14:40 EST
GitHub Pull Request 18 created by [kthoms]
https://github.com/eclipse/xtext-idea/pull/18
Comment 9 Eclipse Genie CLA 2016-12-01 06:24:34 EST
GitHub Pull Request 80 created by [kthoms]
https://github.com/eclipse/xtext-xtend/pull/80
Comment 10 Eclipse Genie CLA 2016-12-02 04:25:48 EST
GitHub Pull Request 21 created by [kthoms]
https://github.com/eclipse/xtext-web/pull/21