Skip to content

Commit

Permalink
Handle cases of a String literal containing '\u0001' or '\u0002'
Browse files Browse the repository at this point in the history
  • Loading branch information
jarthana committed Jun 14, 2023
1 parent 0b2e296 commit eab96b9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ public abstract class ASTNode implements TypeConstants, TypeIds {
// for all reference context entries.
public static final int HasFunctionalInterfaceTypes = ASTNode.Bit22;

// Applicable only for StringLiteral and CharLiteral
public static final int HasStringConcatMarkers = ASTNode.Bit30;

public static final Argument [] NO_ARGUMENTS = new Argument [0];
public static final RecordComponent [] NO_RECORD_COMPONENTS = new RecordComponent [0];
public static final TypePattern[] NO_TYPE_PATTERNS = new TypePattern[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2009 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -40,6 +40,8 @@ private void computeValue() {
//The source is a char[3] first and last char are '
//This is true for both regular char AND unicode char
//BUT not for escape char like '\b' which are char[4]....
if (this.source[1] == '\u0001' || this.source[1] == '\u0002')
this.bits |= ASTNode.HasStringConcatMarkers;
if ((this.value = this.source[1]) != '\\')
return;
char digit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public void buildStringForConcatation(BlockScope blockScope, CodeStream codeStre
case TypeIds.T_boolean :
generateCode(blockScope, codeStream, true);
argTypes.add(this.resolvedType);
builder.append(STRING_CONCAT_FACTORY_ARG);
builder.append(STRING_CONCAT_MARKER_1);
break;
default :
if (this.resolvedType.id == TypeIds.T_null) {
Expand All @@ -956,12 +956,19 @@ public void buildStringForConcatation(BlockScope blockScope, CodeStream codeStre
generateCode(blockScope, codeStream, true);
codeStream.invokeStringValueOf(typeID);
argTypes.add(blockScope.getJavaLangString());
builder.append(STRING_CONCAT_FACTORY_ARG);
builder.append(STRING_CONCAT_MARKER_1);
}
break;
}
} else {
builder.append(this.constant.stringValue());
// StringLiteral and CharLiteral may contain special characters
if ((this.bits & ASTNode.HasStringConcatMarkers) != 0) {
codeStream.ldc(this.constant.stringValue());
builder.append(STRING_CONCAT_MARKER_1);
argTypes.add(blockScope.getJavaLangString());
} else {
builder.append(this.constant.stringValue());
}
}
}
private MethodBinding[] getAllOriginalInheritedMethods(ReferenceBinding binding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,7 @@ public void generateStringConcatenationAppend(BlockScope blockScope, Expression
// Operand is already on the stack
invokeStringValueOf(TypeIds.T_JavaLangObject);
arguments.add(blockScope.getJavaLangString());
builder.append(TypeConstants.STRING_CONCAT_FACTORY_ARG);
builder.append(TypeConstants.STRING_CONCAT_MARKER_1);
} else {
oper1.buildStringForConcatation(blockScope, this, oper1.implicitConversion & TypeIds.COMPILE_TYPE_MASK, builder, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public interface TypeConstants {
char[] UPPER_RECORD_COMPONENT = "RECORD_COMPONENT".toCharArray(); //$NON-NLS-1$
char[] YIELD = "yield".toCharArray(); //$NON-NLS-1$
// Duplicated since java.lang.invoke.StringConcatFactory.TAG_ARG isn't public
char[] STRING_CONCAT_FACTORY_ARG = new char[] {'\u0001'};
char[] STRING_CONCAT_MARKER_1 = new char[] {'\u0001'};

// JEP 286
char[] VAR = "var".toCharArray(); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10030,6 +10030,9 @@ protected void consumeTextBlock() {
0,
0);
}
if (this.scanner.containsMarkers) {
textBlock.bits |= ASTNode.HasStringConcatMarkers;
}
pushOnExpressionStack(textBlock);
// Regardless of the error reported above, we have to push the ast node accordingly
}
Expand Down Expand Up @@ -10475,6 +10478,9 @@ protected void consumeToken(int type) {
this.scanner.currentPosition - 1,
0);
}
if (this.scanner.containsMarkers) {
stringLiteral.bits |= ASTNode.HasStringConcatMarkers;
}
pushOnExpressionStack(stringLiteral);
break;
case TokenNameTextBlock :
Expand Down Expand Up @@ -14334,6 +14340,9 @@ public void recoveryTokenCheck() {
this.scanner.startPosition,
this.scanner.currentPosition - 1,
Util.getLineNumber(this.scanner.startPosition, this.scanner.lineEnds, 0, this.scanner.linePtr));
if (this.scanner.containsMarkers) {
stringLiteral.bits |= ASTNode.HasStringConcatMarkers;
}
this.compilationUnit.recordStringLiteral(stringLiteral, this.currentElement != null);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public class Scanner implements TerminalTokens {

public boolean fakeInModule = false;
public int caseStartPosition = -1;
boolean inCondition = false;
/* package */ int yieldColons = -1;
boolean breakPreviewAllowed = false;
/**
Expand Down Expand Up @@ -213,6 +212,8 @@ enum ScanContext {
//Java 15 - first _ keyword appears
Map<String, Integer> _Keywords = null;

// Contains '/u0001' or '/u0002'
boolean containsMarkers = false;
private CharDeduplication deduplication = CharDeduplication.getThreadLocalInstance();

public Scanner() {
Expand Down Expand Up @@ -2035,6 +2036,7 @@ protected int scanForStringLiteral() throws InvalidInputException {

// consume next character
this.unicodeAsBackSlash = false;
this.containsMarkers = false;
boolean isUnicode = false;
isTextBlock = scanForTextBlockBeginning();
if (isTextBlock) {
Expand Down Expand Up @@ -2119,6 +2121,9 @@ protected int scanForStringLiteral() throws InvalidInputException {
getNextUnicodeChar();
isUnicode = true;
} else {
if (this.currentCharacter == '\u0001' || this.currentCharacter == '\u0002') {
this.containsMarkers = true;
}
isUnicode = false;
if (this.withoutUnicodePtr != 0) {
unicodeStore();
Expand Down Expand Up @@ -2170,6 +2175,8 @@ protected int scanForTextBlock() throws InvalidInputException {
if (this.recordLineSeparator) {
pushLineSeparator();
}
} else if (this.currentCharacter == '\u0001' || this.currentCharacter == '\u0002') {
this.containsMarkers = true;
}
}
outer: if (this.currentCharacter == '\\') {
Expand Down Expand Up @@ -2290,6 +2297,8 @@ public void getNextUnicodeChar()
throw new InvalidInputException(INVALID_UNICODE_ESCAPE);
}
this.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
if (this.currentCharacter == '\u0001' || this.currentCharacter == '\u0002')
this.containsMarkers = true;
//need the unicode buffer
if (this.withoutUnicodePtr == 0) {
//buffer all the entries that have been left aside....
Expand Down

0 comments on commit eab96b9

Please sign in to comment.