Skip to content

Commit

Permalink
NON-NLS tag ignored for specific method references targeting a string…
Browse files Browse the repository at this point in the history
… literal (#3044)

* Fixes #3025
  • Loading branch information
srikanth-sankaran authored Oct 2, 2024
1 parent 03c1de7 commit 81ab185
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private ReferenceExpression copy() {
char [] source = new char [this.sourceEnd+1];
System.arraycopy(this.text, 0, source, this.sourceStart, this.sourceEnd - this.sourceStart + 1);
parser.scanner = this.scanner;
ReferenceExpression copy = (ReferenceExpression) parser.parseExpression(source, this.sourceStart, this.sourceEnd - this.sourceStart + 1,
ReferenceExpression copy = (ReferenceExpression) parser.parseReferenceExpression(source, this.sourceStart, this.sourceEnd - this.sourceStart + 1,
this.enclosingScope.referenceCompilationUnit(), false /* record line separators */);
copy.original = this;
copy.sourceStart = this.sourceStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ protected int actFromTokenOrSynthetic(int previousAct) {
private boolean tolerateDefaultClassMethods = false;
private boolean processingLambdaParameterList = false;
private boolean expectTypeAnnotation = false;
private boolean reparsingLambdaExpression = false;
private boolean reparsingFunctionalExpression = false;

private Map<TypeDeclaration, Integer[]> recordNestedMethodLevels;
private Map<Integer, Boolean> recordPatternSwitches;
Expand Down Expand Up @@ -9406,7 +9406,7 @@ private void consumeTextBlock() {
private TextBlock createTextBlock(char[] allchars, int start, int end) {
TextBlock textBlock;
if (this.recordStringLiterals &&
!this.reparsingLambdaExpression &&
!this.reparsingFunctionalExpression &&
this.checkExternalizeStrings &&
this.lastPosistion < this.scanner.currentPosition &&
!this.statementRecoveryActivated) {
Expand Down Expand Up @@ -9873,7 +9873,7 @@ protected void consumeToken(int type) {
case TokenNameStringLiteral :
StringLiteral stringLiteral;
if (this.recordStringLiterals &&
!this.reparsingLambdaExpression &&
!this.reparsingFunctionalExpression &&
this.checkExternalizeStrings &&
this.lastPosistion < this.scanner.currentPosition &&
!this.statementRecoveryActivated) {
Expand Down Expand Up @@ -13133,7 +13133,7 @@ public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {

public Expression parseLambdaExpression(char[] source, int offset, int length, CompilationUnitDeclaration unit, boolean recordLineSeparators) {
this.haltOnSyntaxError = true; // unexposed/unshared object, no threading concerns.
this.reparsingLambdaExpression = true;
this.reparsingFunctionalExpression = true;
return parseExpression(source, offset, length, unit, recordLineSeparators);
}

Expand All @@ -13160,6 +13160,10 @@ public char[][] parsePackageDeclaration(char[] source, CompilationResult result)
return this.compilationUnit.currentPackage == null ? null : this.compilationUnit.currentPackage.getImportName();

}
public Expression parseReferenceExpression(char[] source, int offset, int length, CompilationUnitDeclaration unit, boolean recordLineSeparators) {
this.reparsingFunctionalExpression = true;
return parseExpression(source, offset, length, unit, recordLineSeparators);
}
public Expression parseExpression(char[] source, int offset, int length, CompilationUnitDeclaration unit, boolean recordLineSeparators) {

initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,39 @@ public void test007() {
true,
customOptions);
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3025
// NON-NLS tag ignored for specific method references targeting a string literal
public void testIssue3025() {
Map customOptions = getCompilerOptions();
customOptions.put(CompilerOptions.OPTION_ReportNonExternalizedStringLiteral, CompilerOptions.ERROR);
this.runNegativeTest(
new String[] {
"X.java",
"""
import java.util.List;
import java.util.function.Predicate;
public class X {
public static void main(String[] args) {
List<String> list = List.<String>of();
list.removeIf("."::equals); //$NON-NLS-1$
list.removeIf(e -> "..".equals(e)); //$NON-NLS-1$
list.removeIf((Predicate<String>) "..."::equals); //$NON-NLS-1$
System.out.println("....");
}
}
""",
},
"----------\n" +
"1. ERROR in X.java (at line 10)\n" +
" System.out.println(\"....\");\n" +
" ^^^^^^\n" +
"Non-externalized string literal; it should be followed by //$NON-NLS-<n>$\n" +
"----------\n",
null,
true,
customOptions);
}
public static Class testClass() {
return ExternalizeStringLiteralsTest_1_5.class;
}
Expand Down

0 comments on commit 81ab185

Please sign in to comment.