Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove escaping of ' in a CtLiteral<String> #5070

Merged
merged 8 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -92,7 +92,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 @@ -113,7 +113,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 @@ -137,7 +137,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 @@ -152,12 +156,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)
I-Al-Istannen marked this conversation as resolved.
Show resolved Hide resolved
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());
}
}