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

Bug 346826

Summary: [Xtend] handle usage of reserved Java keywords
Product: [Modeling] TMF Reporter: Serano Colameo <serano.colameo>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: clay, Holger.Schill, ingo.meyer, karsten.thoms, rvonmassow, sven.efftinge
Version: unspecifiedFlags: sven.efftinge: juno+
Target Milestone: M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Serano Colameo CLA 2011-05-23 03:04:59 EDT
Build Identifier: Xtend2 2.0.0.v201105171404

xtend2:
	def assert(boolean condition) {
		assertTrue(condition, "Condition does not match!")
	}

java:
  public void assert(final boolean condition) {
    this.assertTrue(condition, "Condition does not match!");
  }

of course this is not possible because assert is a reserved word in java (>=1.5).

serano


Reproducible: Always
Comment 1 Sven Efftinge CLA 2011-06-01 13:37:29 EDT
*** Bug 347961 has been marked as a duplicate of this bug. ***
Comment 2 Sven Efftinge CLA 2011-06-01 13:39:48 EDT
In some situations (i.e signatures) we should forbid using reserved Java keywords. For identifiers which are not visible from outside, like local variables or even parameter names, we could allow java keywords but need to improve the escaping strategy.
Comment 3 Robert von Massow CLA 2011-08-22 08:52:57 EDT
*** Bug 355383 has been marked as a duplicate of this bug. ***
Comment 4 Ingo Meyer CLA 2011-10-26 10:16:13 EDT
Here is the solution I'm using in my compiler:

	/**
	 * @see org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler#makeJavaIdentifier(java.lang.String)
	 */
	@Override
	protected String makeJavaIdentifier(
			String name )
	{
		/*
		 * If the final name is a reserved word, prepend a underscore, but "super" and "this" are OK for internal names!
		 * Must come at last!
		 */
		if (JAVA_RESERVED_WORDS.contains( name.toLowerCase() ) //
				&& !name.equalsIgnoreCase( "super" ) && !name.equalsIgnoreCase( "this" ))
			name = "_" + name;
		return name.equals( "this" ) ? "_this" : name;
	}

	/**
	 * Reserved words for java // TODO move to string tools
	 */
	private static final List<String> JAVA_RESERVED_WORDS = new ArrayList<String>();
	static
	{
		JAVA_RESERVED_WORDS.add( "abstract" );
		JAVA_RESERVED_WORDS.add( "assert" ); // added in 1.4
		JAVA_RESERVED_WORDS.add( "boolean" );
		JAVA_RESERVED_WORDS.add( "break" );
		JAVA_RESERVED_WORDS.add( "byte" );
		JAVA_RESERVED_WORDS.add( "case" );
		JAVA_RESERVED_WORDS.add( "catch" );
		JAVA_RESERVED_WORDS.add( "char" );
		JAVA_RESERVED_WORDS.add( "class" );
		JAVA_RESERVED_WORDS.add( "const" ); // not used
		JAVA_RESERVED_WORDS.add( "continue" );
		JAVA_RESERVED_WORDS.add( "default" );
		JAVA_RESERVED_WORDS.add( "do" );
		JAVA_RESERVED_WORDS.add( "double" );
		JAVA_RESERVED_WORDS.add( "else" );
		JAVA_RESERVED_WORDS.add( "enum" ); // added in 5.0
		JAVA_RESERVED_WORDS.add( "extends" );
		JAVA_RESERVED_WORDS.add( "final" );
		JAVA_RESERVED_WORDS.add( "finally" );
		JAVA_RESERVED_WORDS.add( "float" );
		JAVA_RESERVED_WORDS.add( "for" );
		JAVA_RESERVED_WORDS.add( "goto" ); // not used
		JAVA_RESERVED_WORDS.add( "if" );
		JAVA_RESERVED_WORDS.add( "implements" );
		JAVA_RESERVED_WORDS.add( "import" );
		JAVA_RESERVED_WORDS.add( "instanceof" );
		JAVA_RESERVED_WORDS.add( "int" );
		JAVA_RESERVED_WORDS.add( "interface" );
		JAVA_RESERVED_WORDS.add( "long" );
		JAVA_RESERVED_WORDS.add( "native" );
		JAVA_RESERVED_WORDS.add( "new" );
		JAVA_RESERVED_WORDS.add( "package" );
		JAVA_RESERVED_WORDS.add( "private" );
		JAVA_RESERVED_WORDS.add( "protected" );
		JAVA_RESERVED_WORDS.add( "public" );
		JAVA_RESERVED_WORDS.add( "return" );
		JAVA_RESERVED_WORDS.add( "short" );
		JAVA_RESERVED_WORDS.add( "static" );
		JAVA_RESERVED_WORDS.add( "strictfp" ); // added in 1.2
		JAVA_RESERVED_WORDS.add( "super" );
		JAVA_RESERVED_WORDS.add( "switch" );
		JAVA_RESERVED_WORDS.add( "synchronized" );
		JAVA_RESERVED_WORDS.add( "this" );
		JAVA_RESERVED_WORDS.add( "throw" );
		JAVA_RESERVED_WORDS.add( "throws" );
		JAVA_RESERVED_WORDS.add( "transient" );
		JAVA_RESERVED_WORDS.add( "try" );
		JAVA_RESERVED_WORDS.add( "void" );
		JAVA_RESERVED_WORDS.add( "volatile" );
		JAVA_RESERVED_WORDS.add( "while" );
	}


Feel free to use this ;-)
Would be great to get this into the official release...
Comment 5 Michael Clay CLA 2011-10-26 15:43:13 EDT
see also CodeGenUtil#isJavaReservedWord
Comment 6 Ingo Meyer CLA 2011-10-27 05:03:26 EDT
(In reply to comment #5)
> see also CodeGenUtil#isJavaReservedWord

Nice one! didn't know this, thanks
Comment 7 Sven Efftinge CLA 2012-01-09 10:51:48 EST
pushed to master
Comment 8 Sven Efftinge CLA 2012-01-30 06:16:10 EST
*** Bug 370111 has been marked as a duplicate of this bug. ***
Comment 9 Karsten Thoms CLA 2017-09-19 16:54:05 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 10 Karsten Thoms CLA 2017-09-19 17:04:55 EDT
Closing all bugs that were set to RESOLVED before Neon.0