Skip to content

Commit d4c3a60

Browse files
authored
Fix remove unnecessary array creation clean-up with non-element non-nls (eclipse-jdt#2204)
- fix UnwrapNewArrayOperation.rewriteAST() to properly set the tagged flag to true when non-nls markers exist on the line of an operand (i.e. not just the operand itself) - add new test to CleanUpTest1d5 = fixes eclipse-jdt#2202
1 parent ea4dc73 commit d4c3a60

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UnwrapNewArrayOperation.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2024 Red Hat Inc. and others.
2+
* Copyright (c) 2019, 2025 Red Hat Inc. and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -35,7 +35,7 @@
3535
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
3636
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSElement;
3737
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSLine;
38-
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSUtil;
38+
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSScanner;
3939
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
4040

4141
/**
@@ -55,23 +55,30 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
5555
ASTRewrite rewrite= cuRewrite.getASTRewrite();
5656
List<Expression> expressionsInArray= node != null && node.getInitializer() != null && node.getInitializer().expressions() != null ?
5757
node.getInitializer().expressions() : Collections.EMPTY_LIST;
58-
boolean isTagged[]= new boolean[expressionsInArray.size()];
5958
ICompilationUnit cu= cuRewrite.getCu();
59+
CompilationUnit root= cuRewrite.getRoot();
6060

6161
boolean tagged= false;
62-
for (int i= 0; i < expressionsInArray.size(); ++i) {
63-
Expression operand= expressionsInArray.get(i);
64-
NLSLine nlsLine= NLSUtil.scanCurrentLine(cu, operand.getStartPosition());
65-
if (nlsLine != null) {
66-
for (NLSElement element : nlsLine.getElements()) {
67-
if (element.getPosition().getOffset() == operand.getStartPosition()) {
68-
if (element.hasTag()) {
69-
tagged= true;
70-
isTagged[i]= true;
62+
try {
63+
for (int i= 0; i < expressionsInArray.size(); ++i) {
64+
Expression operand= expressionsInArray.get(i);
65+
int lineNo= root.getLineNumber(operand.getStartPosition());
66+
for (NLSLine nlsLine : NLSScanner.scan(cu)) {
67+
if (nlsLine.getLineNumber() == lineNo - 1) {
68+
for (NLSElement element : nlsLine.getElements()) {
69+
if (element.hasTag()) {
70+
tagged= true;
71+
break;
72+
}
7173
}
7274
}
75+
if (tagged) {
76+
break;
77+
}
7378
}
7479
}
80+
} catch (Exception e) {
81+
// do nothing
7582
}
7683

7784
TextEditGroup group= createTextEditGroup(FixMessages.UnusedCodeFix_RemoveUnnecessaryArrayCreation_description, cuRewrite);

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d5.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2024 IBM Corporation and others.
2+
* Copyright (c) 2019, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -3912,6 +3912,63 @@ public void foo() {
39123912
new HashSet<>(Arrays.asList(FixMessages.UnusedCodeFix_RemoveUnnecessaryArrayCreation_description)));
39133913
}
39143914

3915+
@Test
3916+
public void testUnnecessaryArrayIssue2202() throws Exception {
3917+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
3918+
String sample= """
3919+
package test1;
3920+
3921+
import java.util.Arrays;
3922+
import java.util.List;
3923+
3924+
public class A {
3925+
3926+
public void foo1(String x, List<Object> y) {
3927+
// do nothing
3928+
}
3929+
3930+
public void foo() {
3931+
String a = "abc";
3932+
Integer b = 0;
3933+
Double c = 1.4;
3934+
foo1("abc", Arrays.asList(new Object[] {a, //$NON-NLS-1$
3935+
b, c}));
3936+
}
3937+
3938+
}
3939+
""";
3940+
ICompilationUnit cu1= pack1.createCompilationUnit("A.java", sample, false, null);
3941+
3942+
enable(CleanUpConstants.REMOVE_UNNECESSARY_ARRAY_CREATION);
3943+
3944+
sample= """
3945+
package test1;
3946+
3947+
import java.util.Arrays;
3948+
import java.util.List;
3949+
3950+
public class A {
3951+
3952+
public void foo1(String x, List<Object> y) {
3953+
// do nothing
3954+
}
3955+
3956+
public void foo() {
3957+
String a = "abc";
3958+
Integer b = 0;
3959+
Double c = 1.4;
3960+
foo1("abc", Arrays.asList(a, //$NON-NLS-1$
3961+
b, c));
3962+
}
3963+
3964+
}
3965+
""";
3966+
String expected= sample;
3967+
3968+
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected },
3969+
new HashSet<>(Arrays.asList(FixMessages.UnusedCodeFix_RemoveUnnecessaryArrayCreation_description)));
3970+
}
3971+
39153972
@Test
39163973
public void testKeepArrayWithSingleArrayElement() throws Exception {
39173974
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);

0 commit comments

Comments
 (0)