Skip to content

Commit

Permalink
fix: Remove escaping of ' in a CtLiteral<String> (#5070)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Apr 6, 2023
1 parent fa62788 commit 8d5f42d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/main/java/spoon/reflect/visitor/LiteralHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static <T> String getLiteralToken(CtLiteral<T> literal) {
}
StringBuilder sb = new StringBuilder(10);
sb.append('\'');
appendCharLiteral(sb, (Character) literal.getValue(), mayContainsSpecialCharacter);
appendCharLiteral(sb, (Character) literal.getValue(), mayContainsSpecialCharacter, false);
sb.append('\'');
return sb.toString();
} else if (literal.getValue() instanceof String) {
Expand All @@ -120,7 +120,7 @@ public static <T> String getLiteralToken(CtLiteral<T> literal) {
}
}

static void appendCharLiteral(StringBuilder sb, Character c, boolean mayContainsSpecialCharacter) {
static void appendCharLiteral(StringBuilder sb, Character c, boolean mayContainsSpecialCharacter, boolean isInsideString) {
if (!mayContainsSpecialCharacter) {
sb.append(c);
} else {
Expand All @@ -144,7 +144,11 @@ static void appendCharLiteral(StringBuilder sb, Character c, boolean mayContains
sb.append("\\\""); //$NON-NLS-1$
break;
case '\'':
sb.append("\\'"); //$NON-NLS-1$
if (isInsideString) {
sb.append("'"); //$NON-NLS-1$
} else {
sb.append("\\'"); //$NON-NLS-1$
}
break;
case '\\': // take care not to display the escape as a potential
// real char
Expand All @@ -159,12 +163,11 @@ static void appendCharLiteral(StringBuilder sb, Character c, boolean mayContains
static String getStringLiteral(String value, boolean mayContainsSpecialCharacter) {
if (!mayContainsSpecialCharacter) {
return value;
} else {
StringBuilder sb = new StringBuilder(value.length() * 2);
for (int i = 0; i < value.length(); i++) {
appendCharLiteral(sb, value.charAt(i), mayContainsSpecialCharacter);
}
return sb.toString();
}
StringBuilder sb = new StringBuilder(value.length() * 2);
for (int i = 0; i < value.length(); i++) {
appendCharLiteral(sb, value.charAt(i), true, true);
}
return sb.toString();
}
}
27 changes: 26 additions & 1 deletion src/test/java/spoon/test/literal/LiteralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.factory.CodeFactory;
import spoon.test.GitHubIssue;
import spoon.test.literal.testclasses.Tacos;
import spoon.Launcher;
import spoon.reflect.code.LiteralBase;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.factory.TypeFactory;
import spoon.reflect.declaration.CtClass;
import org.junit.jupiter.api.Test;
import spoon.testing.utils.ModelTest;

import java.util.List;
import java.util.TreeSet;

import static spoon.testing.utils.ModelUtils.buildClass;
Expand Down Expand Up @@ -257,4 +259,27 @@ public void testLiteralBasePrinter(Factory factory) {
assertEquals("'c'", ctClass.getField("c1").getDefaultExpression().toString());
assertEquals("\"hello\"", ctClass.getField("s1").getDefaultExpression().toString());
}

@GitHubIssue(issueNumber = 5070, fixed = true)
void tooStrictEscaping() {
// contract: inside a string without a position ' are not escaped.
List<CtLiteral<?>> literals = Launcher.parseClass("class Foo { String s = \"'\"; }")
.getElements(new TypeFilter<>(CtLiteral.class));
CtLiteral<?> ctLiteral = literals.get(0);
ctLiteral.setPosition(SourcePosition.NOPOSITION);
String literal = (String) ctLiteral.getValue();
assertEquals("'", literal);
assertEquals("\"'\"", ctLiteral.toString());
}

@GitHubIssue(issueNumber = 5070, fixed = true)
void tooStrictEscapingCharTest() {
// contract: inside a string with a position ' are escaped.
List<CtLiteral<?>> literals = Launcher.parseClass("class Foo { char c = \'\\'\'; }")
.getElements(new TypeFilter<>(CtLiteral.class));
CtLiteral<?> ctLiteral = literals.get(0);
char literal = (char) ctLiteral.getValue();
assertEquals('\'', literal);
assertEquals("\'\\'\'", ctLiteral.toString());
}
}

0 comments on commit 8d5f42d

Please sign in to comment.