Community
Participate
Working Groups
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
*** Bug 347961 has been marked as a duplicate of this bug. ***
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.
*** Bug 355383 has been marked as a duplicate of this bug. ***
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...
see also CodeGenUtil#isJavaReservedWord
(In reply to comment #5) > see also CodeGenUtil#isJavaReservedWord Nice one! didn't know this, thanks
pushed to master
*** Bug 370111 has been marked as a duplicate of this bug. ***
Closing all bugs that were set to RESOLVED before Neon.0