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

Bug 362651

Summary: name collision w.r.t. '_class' and closures in Xtend2
Product: [Modeling] TMF Reporter: Meinte Boersma <meinte.boersma>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: sebastian.zarnekow, sven.efftinge
Version: unspecifiedFlags: sebastian.zarnekow: juno+
Target Milestone: M4   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Meinte Boersma CLA 2011-11-02 06:30:18 EDT
Build Identifier: 20110615-0604

Use of Java's Object#getClass() method leads to creation of statement "Class<? extends Object> _class = item.getClass();" after compilation to Java. This may shadow a pre-existing method parameter without chance of accessing the original one in the Xtend code when using closures, which is hard to debug. As an example:

	def <T> Iterable<T> asIterable(JSONArray jsonArray, Class<T> _class) {
		jsonArray.asIterableInternal( [ item, list |
			if( _class.isAssignableFrom(item.^class) ) {
				list.add(_class.cast(item))
			} else {
				throw new IllegalArgumentException('''item is of type «item.^class.name» and cannot be cast to «_class.name»'''.toString)
			}
		] )
	}

And the dependency:

	def <T> Iterable<T> asIterableInternal(JSONArray jsonArray, org.eclipse.xtext.xbase.lib.Functions$Function2<Object, List<T>, ?> action) {
		val len = jsonArray.length
		val iterable = new ArrayList<T>(len)
		var i = 0
		while( i < len ) {
			try {
				val item = jsonArray.get(i)
				action.apply(item, iterable)
			} catch( Exception e ) {
				throw new RuntimeException('''cannot obtain item with index «i» from JSONArray'''.toString, e)
			}
			i = i+1
		}
		iterable
	}

(Renaming the parameter '_class' to e.g. 'clazz' resolves the problem.)

Reproducible: Always

Steps to Reproduce:
1. see details
2.
3.
Comment 1 Sebastian Zarnekow CLA 2011-11-02 09:14:23 EDT
_xyz is the reserved namespace for the synthetic variables that the compiler creates. We need a validation that user variables don't start with an underscore.
Comment 2 Meinte Boersma CLA 2011-11-02 09:18:35 EDT
Alternatively, you could scan for things prefixed with _ and rig the mechanism for giving internal synthetic variables a unique name with that. The validation is much simpeler, but especially people coming from JS coding have a tendency to use the _ prefix. Also, the 'class' name is a bit cumbersome in itself.
Comment 3 Sven Efftinge CLA 2011-11-07 10:18:42 EST
pushed to master
Comment 4 Sven Efftinge CLA 2011-11-08 09:03:20 EST
We should check whether a given name is used by a user variable, such that we end up with as little
numbered variable names as possible.
Comment 5 Sven Efftinge CLA 2011-11-09 06:54:26 EST
pushed to master
Comment 6 Sven Efftinge CLA 2011-11-09 06:55:01 EST
should have been "pushed to 2.1.x_Maintenance" :-)
Comment 7 Karsten Thoms CLA 2017-09-19 17:09:15 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 8 Karsten Thoms CLA 2017-09-19 17:21:10 EDT
Closing all bugs that were set to RESOLVED before Neon.0