diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index d061fd7b071..6de53e3bb95 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -1150,7 +1150,7 @@ public void visitCtLiteral(CtLiteral literal) { // if the string in the source is not the same as the string in the literal, the string may contains special characters mayContainsSpecialCharacter = stringLength != 1; } - printer.writeStringLiteral(new String(new char[] { (Character) literal.getValue() }), mayContainsSpecialCharacter); + printer.writeCharLiteral((Character)literal.getValue(), mayContainsSpecialCharacter); printer.write("'"); } else if (literal.getValue() instanceof String) { diff --git a/src/main/java/spoon/reflect/visitor/printer/PrinterHelper.java b/src/main/java/spoon/reflect/visitor/printer/PrinterHelper.java index e4c01edfd19..9034061ae8b 100644 --- a/src/main/java/spoon/reflect/visitor/printer/PrinterHelper.java +++ b/src/main/java/spoon/reflect/visitor/printer/PrinterHelper.java @@ -337,54 +337,58 @@ public PrinterHelper writeOperator(BinaryOperatorKind o) { return this; } - public void writeStringLiteral(String value, boolean mayContainsSpecialCharacter) { + public void writeCharLiteral(Character c, boolean mayContainsSpecialCharacter) { if (!mayContainsSpecialCharacter) { - write(value); - return; - } - // handle some special char..... - for (int i = 0; i < value.length(); i++) { - char c = value.charAt(i); - if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) { - if (c < 0x10) { - write("\\u000" + Integer.toHexString(c)); - } else if (c < 0x100) { - write("\\u00" + Integer.toHexString(c)); - } else if (c < 0x1000) { - write("\\u0" + Integer.toHexString(c)); - } else { - write("\\u" + Integer.toHexString(c)); - } - continue; + write(c); + } else if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) { + if (c < 0x10) { + write("\\u000" + Integer.toHexString(c)); + } else if (c < 0x100) { + write("\\u00" + Integer.toHexString(c)); + } else if (c < 0x1000) { + write("\\u0" + Integer.toHexString(c)); + } else { + write("\\u" + Integer.toHexString(c)); } + } else { switch (c) { - case '\b': - write("\\b"); //$NON-NLS-1$ - break; - case '\t': - write("\\t"); //$NON-NLS-1$ - break; - case '\n': - write("\\n"); //$NON-NLS-1$ - break; - case '\f': - write("\\f"); //$NON-NLS-1$ - break; - case '\r': - write("\\r"); //$NON-NLS-1$ - break; - case '\"': - write("\\\""); //$NON-NLS-1$ - break; - case '\'': - write("\\'"); //$NON-NLS-1$ - break; - case '\\': // take care not to display the escape as a potential - // real char - write("\\\\"); //$NON-NLS-1$ - break; - default: - write(value.charAt(i)); + case '\b': + write("\\b"); //$NON-NLS-1$ + break; + case '\t': + write("\\t"); //$NON-NLS-1$ + break; + case '\n': + write("\\n"); //$NON-NLS-1$ + break; + case '\f': + write("\\f"); //$NON-NLS-1$ + break; + case '\r': + write("\\r"); //$NON-NLS-1$ + break; + case '\"': + write("\\\""); //$NON-NLS-1$ + break; + case '\'': + write("\\'"); //$NON-NLS-1$ + break; + case '\\': // take care not to display the escape as a potential + // real char + write("\\\\"); //$NON-NLS-1$ + break; + default: + write(Character.isISOControl(c) ? String.format("\\u%04x", (int) c) : Character.toString(c)); + } + } + } + + public void writeStringLiteral(String value, boolean mayContainsSpecialCharacter) { + if (!mayContainsSpecialCharacter) { + write(value); + } else { + for (int i = 0; i < value.length(); i++) { + writeCharLiteral(value.charAt(i), mayContainsSpecialCharacter); } } }