Skip to content

Commit

Permalink
refactor StringLiteral: use getter/setter
Browse files Browse the repository at this point in the history
and fix StringLiteralConcatenation lineNumber
  • Loading branch information
EcljpseB0T authored and jukzi committed Jan 15, 2024
1 parent f700559 commit 45b012d
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 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 @@ -712,7 +712,7 @@ private void reportNLSProblems() {
int i = 0;
stringLiteralsLoop: for (; i < stringLiteralsLength; i++) {
literal = this.stringLiterals[i];
final int literalLineNumber = literal instanceof TextBlock ? ((TextBlock)literal).endLineNumber : literal.lineNumber;
final int literalLineNumber = literal instanceof TextBlock ? ((TextBlock)literal).endLineNumber : literal.getLineNumber();
if (lastLineNumber != literalLineNumber) {
indexInLine = 1;
lastLineNumber = literalLineNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,17 @@

public class ExtendedStringLiteral extends StringLiteral {

/**
* Build a string+char literal
*/
public ExtendedStringLiteral(StringLiteral str, CharLiteral character) {

super(str.source, str.sourceStart, str.sourceEnd, str.lineNumber);
extendWith(character);
}

/**
* Build a two-strings literal
* */
public ExtendedStringLiteral(StringLiteral str1, StringLiteral str2) {

super(str1.source, str1.sourceStart, str1.sourceEnd, str1.lineNumber);
extendWith(str2);
}

/**
* Add the lit source to mine, just as if it was mine
*/
@Override
public ExtendedStringLiteral extendWith(CharLiteral lit) {

//update the source
int length = this.source.length;
System.arraycopy(this.source, 0, (this.source = new char[length + 1]), 0, length);
this.source[length] = lit.value;
//position at the end of all literals
this.sourceEnd = lit.sourceEnd;
return this;
}

/**
* Add the lit source to mine, just as if it was mine
*/
@Override
public ExtendedStringLiteral extendWith(StringLiteral lit) {

//uddate the source
int length = this.source.length;
System.arraycopy(
this.source,
0,
this.source = new char[length + lit.source.length],
0,
length);
System.arraycopy(lit.source, 0, this.source, length, lit.source.length);
//position at the end of all literals
this.sourceEnd = lit.sourceEnd;
return this;
public ExtendedStringLiteral(char[] token, int start, int end, int lineNumber) {
super(token, start, end, lineNumber);
}

@Override
public StringBuilder printExpression(int indent, StringBuilder output) {

return output.append("ExtendedStringLiteral{").append(this.source).append('}'); //$NON-NLS-1$
return output.append("ExtendedStringLiteral{").append(this.source()).append('}'); //$NON-NLS-1$
}

@Override
public void traverse(ASTVisitor visitor, BlockScope scope) {

visitor.visit(this, scope);
visitor.endVisit(this, scope);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;

import java.util.Arrays;

import org.eclipse.jdt.internal.compiler.ASTVisitor;
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
import org.eclipse.jdt.internal.compiler.impl.StringConstant;
Expand All @@ -22,51 +24,52 @@

public class StringLiteral extends Literal {

public char[] source;
int lineNumber;
private char[] source;
private final int lineNumber;

public StringLiteral(char[] token, int start, int end, int lineNumber) {

this(start,end);
super(start, end);
this.source = token;
this.lineNumber = lineNumber - 1; // line number is 1 based
}

public StringLiteral(int s, int e) {

super(s,e);
this(null, s, e, 1);
}

@Override
public void computeConstant() {

this.constant = StringConstant.fromValue(String.valueOf(this.source));
}

public ExtendedStringLiteral extendWith(CharLiteral lit){

//add the lit source to mine, just as if it was mine
return new ExtendedStringLiteral(this,lit);
public ExtendedStringLiteral extendWith(CharLiteral lit) {
return new ExtendedStringLiteral(append(this.source(), new char[] { lit.value }),
this.sourceStart, lit.sourceEnd, this.getLineNumber() + 1);
}

public ExtendedStringLiteral extendWith(StringLiteral lit){

//add the lit source to mine, just as if it was mine
return new ExtendedStringLiteral(this,lit);
public ExtendedStringLiteral extendWith(StringLiteral lit) {
return new ExtendedStringLiteral(append(this.source(), lit.source()), this.sourceStart,
lit.sourceEnd, this.getLineNumber() + 1);
}
protected static char[] append(char[] source, char[] source2) {
char[] result = Arrays.copyOfRange(source, 0, source.length + source2.length);
System.arraycopy(source2, 0, result, source.length, source2.length);
return result;
}


/**
* Add the lit source to mine, just as if it was mine
* Add the lit source to mine, just as if it was mine
*/
public StringLiteralConcatenation extendsWith(StringLiteral lit) {
return new StringLiteralConcatenation(this, lit);
}

/**
* Code generation for string literal
*/
@Override
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {

int pc = codeStream.position;
if (valueRequired)
codeStream.ldc(this.constant.stringValue());
Expand All @@ -75,31 +78,36 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean

@Override
public TypeBinding literalType(BlockScope scope) {

return scope.getJavaLangString();
}

@Override
public StringBuilder printExpression(int indent, StringBuilder output) {

// handle some special char.....
output.append('\"');
for (int i = 0; i < this.source.length; i++) {
Util.appendEscapedChar(output, this.source[i], true);
for (char s : source()) {
Util.appendEscapedChar(output, s, true);
}
output.append('\"');
return output;
}

@Override
public char[] source() {

return this.source;
return Arrays.copyOf(this.source, this.source.length);
}

@Override
public void traverse(ASTVisitor visitor, BlockScope scope) {
visitor.visit(this, scope);
visitor.endVisit(this, scope);
}

public void setSource(char[] source) {
this.source = source;
}

public int getLineNumber() {
return this.lineNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;

import java.util.Arrays;

import org.eclipse.jdt.internal.compiler.ASTVisitor;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;

Expand All @@ -21,66 +23,47 @@
*/
public class StringLiteralConcatenation extends StringLiteral {
private static final int INITIAL_SIZE = 5;
public Expression[] literals;
public int counter;
/**
* Build a two-strings literal
* */
public StringLiteralConcatenation(StringLiteral str1, StringLiteral str2) {
super(str1.sourceStart, str1.sourceEnd);
this.source = str1.source;
this.literals = new StringLiteral[INITIAL_SIZE];
this.counter = 0;
this.literals[this.counter++] = str1;
extendsWith(str2);
}
private final StringLiteral[] literals;
private final int counter;

/**
* Add the lit source to mine, just as if it was mine
* Build a two-strings literal
*/
@Override
public StringLiteralConcatenation extendsWith(StringLiteral lit) {
this.sourceEnd = lit.sourceEnd;
final int literalsLength = this.literals.length;
if (this.counter == literalsLength) {
// resize
System.arraycopy(this.literals, 0, this.literals = new StringLiteral[literalsLength + INITIAL_SIZE], 0, literalsLength);
public StringLiteralConcatenation(StringLiteral str1, StringLiteral str2) {
super(StringLiteral.append(str1.source(), str2.source()), str1.sourceStart, str2.sourceEnd,
str1.getLineNumber() + 1);
if (str1 instanceof StringLiteralConcatenation s1) {
this.literals = Arrays.copyOf(s1.literals, s1.literals.length + 1);
this.counter = s1.counter + 1;
} else {
this.literals = new StringLiteral[INITIAL_SIZE];
this.literals[0] = str1;
this.counter = 2;
}
//uddate the source
int length = this.source.length;
System.arraycopy(
this.source,
0,
this.source = new char[length + lit.source.length],
0,
length);
System.arraycopy(lit.source, 0, this.source, length, lit.source.length);
this.literals[this.counter++] = lit;
return this;
this.literals[this.counter - 1] = str2;
}

@Override
public StringBuilder printExpression(int indent, StringBuilder output) {
output.append("StringLiteralConcatenation{"); //$NON-NLS-1$
for (int i = 0, max = this.counter; i < max; i++) {
this.literals[i].printExpression(indent, output);
for (StringLiteral lit : getLiterals()) {
lit.printExpression(indent, output);
output.append("+\n");//$NON-NLS-1$
}
return output.append('}');
}

@Override
public char[] source() {
return this.source;
}

@Override
public void traverse(ASTVisitor visitor, BlockScope scope) {
if (visitor.visit(this, scope)) {
for (int i = 0, max = this.counter; i < max; i++) {
this.literals[i].traverse(visitor, scope);
for (StringLiteral lit : getLiterals()) {
lit.traverse(visitor, scope);
}
}
visitor.endVisit(this, scope);
}

public StringLiteral[] getLiterals() {
return Arrays.copyOf(this.literals, this.counter);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 IBM Corporation and others.
* Copyright (c) 2023, 2024 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 @@ -100,7 +100,7 @@ public StringBuilder printExpression(int indent, StringBuilder output) {
if (this.isMultiline)
output.append("\"\"\n"); //$NON-NLS-1$
for (int i = 0; i < length; i++) {
char[] source = this.fragments[i].source;
char[] source = this.fragments[i].source();
for (int j = 0; j < source.length; j++) {
Util.appendEscapedChar(output, source[j], true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ private static boolean getLineContent(StringBuilder result, char[] line, int sta
@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);
for (char c:this.source()) {
Util.appendEscapedChar(output, c, true);
}
output.append("\"\"\""); //$NON-NLS-1$
return output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ private void addJUnitMethodSourceValues(SimpleSetOfCharArray junitMethodSourceVa

private char[] getValueAsChars(Expression value) {
if (value instanceof StringLiteral) { // e.g. "someMethod"
return ((StringLiteral) value).source;
return ((StringLiteral) value).source();
} else if (value.constant instanceof StringConstant) { // e.g. SOME_CONSTANT + "value"
return ((StringConstant) value.constant).stringValue().toCharArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10026,7 +10026,7 @@ protected void consumeTemplate(int token) {
StringLiteral literal = fragments[index];
char[][] lines = TextBlock.convertTextBlockToLines(fragments[index].source());
char[] formattedText = TextBlock.formatTextBlock(lines, textBlockIndent, index > 0, index+1 < fragments.length);
literal.source = formattedText;
literal.setSource(formattedText);
}
}
// get rid of all the cached values
Expand Down

0 comments on commit 45b012d

Please sign in to comment.