Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 346826 - [Xtend] handle usage of reserved Java keywords
Summary: [Xtend] handle usage of reserved Java keywords
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: M5   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 347961 355383 370111 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-05-23 03:04 EDT by Serano Colameo CLA
Modified: 2017-09-19 17:04 EDT (History)
6 users (show)

See Also:
sven.efftinge: juno+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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