Skip to content

Commit

Permalink
ECJ crashes when an embedded expression contains broken code (#1702)
Browse files Browse the repository at this point in the history
Set haltOnSyntaxError when parsing for embedded expressions. Also using the correct delimiters for text blocks in printExpression() methods.
  • Loading branch information
jarthana authored and noopur2507 committed Dec 15, 2023
1 parent e281317 commit eb4846d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean
public StringBuilder printExpression(int indent, StringBuilder output) {
int length = this.fragments.length;
output.append('\"');
if (this.isMultiline)
output.append("\"\"\n"); //$NON-NLS-1$
for (int i = 0; i < length; i++) {
char[] source = this.fragments[i].source;
for (int j = 0; j < source.length; j++) {
Expand All @@ -111,6 +113,8 @@ public StringBuilder printExpression(int indent, StringBuilder output) {
}
}
output.append('\"');
if (this.isMultiline)
output.append("\"\""); //$NON-NLS-1$
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
import org.eclipse.jdt.internal.compiler.util.Util;

public class TextBlock extends StringLiteral {

Expand Down Expand Up @@ -296,4 +297,13 @@ private static boolean getLineContent(StringBuilder result, char[] line, int sta
result.append(chars);
return (!merge && !lastLine);
}
@Override
public StringBuilder printExpression(int indent, StringBuilder output) {
output.append("\"\"\"\n"); //$NON-NLS-1$
for (int i = 0; i < this.source.length; i++) {
Util.appendEscapedChar(output, this.source[i], true);
}
output.append("\"\"\""); //$NON-NLS-1$
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13808,6 +13808,7 @@ private Expression parseExpression(char[] source, int offset, int length, Compil
this.scanner.resetTo(offset, offset + length - 1);
try {
if (embeddedExpression) {
this.haltOnSyntaxError = true;
int nextToken;
try {
nextToken = this.scanner.getNextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,22 @@ public void test002() throws JavaModelException {
}
public void test003() throws JavaModelException {
String string =
"public class X<R> {\n"
+ "@SuppressWarnings(\"nls\")\n"
+ "public static void main(String[] args) {\n"
+ " String name = \"Joan Smith\";\n"
+ " String phone = \"555-123-4567\";\n"
+ " String address = \"1 Maple Drive, Anytown\";\n"
+ " String doc = STR.\"\"\"\n"
+ " {\n"
+ " \"name\": \"\\{STR.\"\\{name}\"}\",\n"
+ " \"phone\": \"\\{phone}\",\n"
+ " \"address\": \"\\{address}\" \n"
+ " };\"\"\";\n"
+ " System.out.println(doc);\n"
+ " } \n"
+ "} ";
"""
public class X<R> {
@SuppressWarnings("nls")
public static void main(String[] args) {
String name = "Joan Smith";
String phone = "555-123-4567";
String address = "1 Maple Drive, Anytown";
String doc = STR.\"""
{
"name": "\\{STR."\\{name}"}",
"phone": "\\{phone}",
"address": "\\{address}"
};\""";
System.out.println(doc);
}
}""";

String selection = "name";
String expectedSelection = "<SelectOnName:" + "name" + ">";
Expand All @@ -127,7 +128,7 @@ public X() {
String name;
String phone;
String address;
String doc = STR.\"{\\n \\\"name\\\": \\\"\\{STR.\"\\{<SelectOnName:name>}\"}\\\",\\n \\\"phone\\\": \\\"\\{phone}\\\",\\n \\\"address\\\": \\\"\\{address}\\\"\\n};";
String doc = STR.\"\"\"\n{\\n \\\"name\\\": \\\"\\{STR.\"\\{<SelectOnName:name>}\"}\\\",\\n \\\"phone\\\": \\\"\\{phone}\\\",\\n \\\"address\\\": \\\"\\{address}\\\"\\n};\"\"\";
}
}
""";
Expand All @@ -140,4 +141,43 @@ public X() {
checkMethodParse(string.toCharArray(), selectionStart, selectionEnd, expectedSelection, expectedUnitDisplayString,
selectionIdentifier, expectedReplacedSource, testName);
}
public void test004() throws JavaModelException {
String str =
"""
public class X<R> {
public static void main(String[] args) {
System.out.println(\"""
Hello\""");
}
}""";

String selectionStartBehind = "System.out.";
String selectionEndBehind = "println";

String expectedCompletionNodeToString = "<SelectOnMessageSend:System.out.println(\"\"\"\nHello\"\"\")>";
String completionIdentifier = "println";
String expectedUnitDisplayString =
"public class X<R> {\n" +
" public X() {\n" +
" }\n" +
" public static void main(String[] args) {\n" +
" <SelectOnMessageSend:System.out.println(\"\"\"\nHello\"\"\")>;\n" +
" }\n" +
"}\n";
String expectedReplacedSource = "System.out.println(\"\"\"\n\t\t\tHello\"\"\")";
String testName = "<select message send>";

int selectionStart = str.indexOf(selectionStartBehind) + selectionStartBehind.length();
int selectionEnd = str.indexOf(selectionEndBehind) + selectionEndBehind.length() - 1;

this.checkMethodParse(
str.toCharArray(),
selectionStart,
selectionEnd,
expectedCompletionNodeToString,
expectedUnitDisplayString,
completionIdentifier,
expectedReplacedSource,
testName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1610,15 +1610,10 @@ public static void main(String argv[]) {
"""
},
"----------\n" +
"1. ERROR in X.java (at line 6)\n" +
"1. ERROR in X.java (at line 1)\n" +
" s=\"Jay\";\n" +
" ^\n" +
"Syntax error on token \";\", delete this token\n" +
"----------\n" +
"2. ERROR in X.java (at line 7)\n" +
" }\n" +
" ^\n" +
"Text block is not properly closed with the delimiter\n" +
"----------\n"
);
}
Expand Down Expand Up @@ -1713,4 +1708,27 @@ public static void main(String argv[]) {
false,
options);
}
public void test0062() {
runNegativeTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String argv[]) {
String name = "Jay";
String greet = STR."Hello \\{name + foo())}!";
System.out.println(greet);
}
private static String foo() {
return "A";
}
}"""
},
"----------\n" +
"1. ERROR in X.java (at line 1)\n" +
" String greet = STR.\"Hello \\{name + foo())}!\";\n" +
" ^\n" +
"Syntax error on token \")\", delete this token\n" +
"----------\n");
}
}

0 comments on commit eb4846d

Please sign in to comment.