-- Leo's gemini proxy

-- Connecting to republic.circumlunar.space:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

Avoid backslashes anywhere in Java code (Java error "illegal unicode escape")


Did you know you can insert unicode-escaped characters, anywhere in a Java program?


Most of us are familiar with using unicode escapes like this:


String pound = "\u00A3";

but in fact constructs like \u00A3 can go anywhere, including in a comment.


This is all fine so long as they're valid, but what if you're generating Java code without due care and attention?


And what if you're inserting file paths into your generated code?


And what if one of your directories has a name starting with a "u"?


Then you get code like this (edited example from our real project!):


// DO NOT EDIT!  This file was generated from:
// C:\usr\foo.xml


And, only on the machine with the dir called "usr", we got a compile error like this:


MyClass.java:14: illegal unicode escape
// C:\usr\foo.xml
      ^
1 error


Which took me a while to track down.


Reference: JLS-3.3 Unicode Escapes.


JLS-3.3 Unicode Escapes


Note this only applies to unicode escapes, not others like \n or \t - they are only processed within character or string literals (JLS-3.10.6).


Originally posted at 2013-10-10 09:16:43+00:00. Automatically generated from the original post : apologies for the errors introduced.


JLS-3.10.6

original post

-- Response ended

-- Page fetched on Sun May 19 04:17:14 2024