Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Arthanareeswaran2019-05-10 12:18:14 -0400
committerJay Arthanareeswaran2019-05-13 04:13:47 -0400
commit07c03e0c1a8370acadbedde0194541eadc4b20d4 (patch)
tree055f6d19c53e0c881cee4ee5202ffc4ba23606d2
parentca057284776b53a6c26c46cec81546df9ed4cf32 (diff)
downloadeclipse.jdt.core-07c03e0c1a8370acadbedde0194541eadc4b20d4.tar.gz
eclipse.jdt.core-07c03e0c1a8370acadbedde0194541eadc4b20d4.tar.xz
eclipse.jdt.core-07c03e0c1a8370acadbedde0194541eadc4b20d4.zip
Bug 547125 [12] NPE in
compiler.problem.ProblemReporter.duplicateCase(...) on reconcile Change-Id: Ie97b4e108f4ce1c15376cba577d55e40d148663c Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java86
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java20
2 files changed, 87 insertions, 19 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
index 66d8d68d83..31eba3a614 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
@@ -699,10 +699,10 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
};
String expectedProblemLog =
- "----------\n" +
- "1. ERROR in X.java (at line 7)\n" +
- " case SUNDAY : System.out.println(Day.SUNDAY);\n" +
- " ^^^^^^^^^^^\n" +
+ "----------\n" +
+ "1. ERROR in X.java (at line 4)\n" +
+ " case SATURDAY, SUNDAY: \n" +
+ " ^^^^^^^^^^^^^^^^^^^^^\n" +
"Duplicate case\n" +
"----------\n" +
"2. ERROR in X.java (at line 7)\n" +
@@ -748,9 +748,9 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
" ^^^\n" +
"The enum constant MONDAY needs a corresponding case label in this enum switch on Day\n" +
"----------\n" +
- "2. ERROR in X.java (at line 7)\n" +
- " case SUNDAY, SATURDAY : \n" +
- " ^^^^^^^^^^^^^^^^^^^^^\n" +
+ "2. ERROR in X.java (at line 4)\n" +
+ " case SATURDAY, SUNDAY: \n" +
+ " ^^^^^^^^^^^^^^^^^^^^^\n" +
"Duplicate case\n" +
"----------\n" +
"3. ERROR in X.java (at line 7)\n" +
@@ -1154,8 +1154,8 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
};
String expectedProblemLog =
"----------\n" +
- "1. ERROR in X.java (at line 8)\n" +
- " case 3, 4: \n" +
+ "1. ERROR in X.java (at line 6)\n" +
+ " case 1, 3: \n" +
" ^^^^^^^^^\n" +
"Duplicate case\n" +
"----------\n" +
@@ -1195,8 +1195,8 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
};
String expectedProblemLog =
"----------\n" +
- "1. ERROR in X.java (at line 8)\n" +
- " case \"b\", \"c\": \n" +
+ "1. ERROR in X.java (at line 6)\n" +
+ " case \"a\", \"b\": \n" +
" ^^^^^^^^^^^^^\n" +
"Duplicate case\n" +
"----------\n" +
@@ -2472,4 +2472,68 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
"Illegal last statement in Switch expression case body - continue, return not allowed\n" +
"----------\n");
}
+ public void testBug547125_01() {
+ this.runNegativeTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " void foo(Day day) {\n" +
+ " switch (day) {\n" +
+ " case SATURDAY, SUNDAY, SUNDAY:\n" +
+ " System.out.println(\"Weekend\");\n" +
+ " case MONDAY:\n" +
+ " System.out.println(\"Weekday\");\n" +
+ " default: \n" +
+ " }\n" +
+ " }\n" +
+ "}\n" +
+ "\n" +
+ "enum Day {\n" +
+ " MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;\n" +
+ "}",
+ },
+ "----------\n" +
+ "1. ERROR in X.java (at line 4)\n" +
+ " case SATURDAY, SUNDAY, SUNDAY:\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Duplicate case\n" +
+ "----------\n");
+ }
+ public void testBug547125_02() {
+ this.runNegativeTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " void foo(Day day) {\n" +
+ " switch (day) {\n" +
+ " case SATURDAY, SUNDAY, MONDAY:\n" +
+ " System.out.println(\"Weekend\");\n" +
+ " case MONDAY, SUNDAY:\n" +
+ " System.out.println(\"Weekday\");\n" +
+ " default: \n" +
+ " }\n" +
+ " }\n" +
+ "}\n" +
+ "\n" +
+ "enum Day {\n" +
+ " MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;\n" +
+ "}",
+ },
+ "----------\n" +
+ "1. ERROR in X.java (at line 4)\n" +
+ " case SATURDAY, SUNDAY, MONDAY:\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Duplicate case\n" +
+ "----------\n" +
+ "2. ERROR in X.java (at line 6)\n" +
+ " case MONDAY, SUNDAY:\n" +
+ " ^^^^^^^^^^^^^^^^^^^\n" +
+ "Duplicate case\n" +
+ "----------\n" +
+ "3. ERROR in X.java (at line 6)\n" +
+ " case MONDAY, SUNDAY:\n" +
+ " ^^^^^^^^^^^^^^^^^^^\n" +
+ "Duplicate case\n" +
+ "----------\n");
+ }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
index b3203a4767..f32a048ec9 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
@@ -691,8 +691,10 @@ public class SwitchStatement extends Expression {
this.constMapping = new int[this.nConstants];
}
int counter = 0;
+ int caseCounter = 0;
for (int i = 0; i < length; i++) {
Constant[] constantsList;
+ int[] caseIndex = new int[this.nConstants];
final Statement statement = this.statements[i];
if (!(statement instanceof CaseStatement)) {
statement.resolve(this.scope);
@@ -707,26 +709,26 @@ public class SwitchStatement extends Expression {
//----check for duplicate case statement------------
for (int j = 0; j < counter; j++) {
if (this.constants[j] == key) {
- reportDuplicateCase((CaseStatement) statement, this.cases[j], length);
+ reportDuplicateCase((CaseStatement) statement, this.cases[caseIndex[j]], length);
}
}
this.constants[counter] = key;
- this.constMapping[counter] = counter;
- counter++;
} else {
String key = con.stringValue();
//----check for duplicate case statement------------
for (int j = 0; j < counter; j++) {
if (this.stringConstants[j].equals(key)) {
- reportDuplicateCase((CaseStatement) statement, this.cases[j], length);
+ reportDuplicateCase((CaseStatement) statement, this.cases[caseIndex[j]], length);
}
}
this.stringConstants[counter] = key;
- this.constMapping[counter] = counter;
- counter++;
}
+ this.constMapping[counter] = counter;
+ caseIndex[counter] = caseCounter;
+ counter++;
}
}
+ caseCounter++;
}
if (length != counter) { // resize constants array
if (!isStringSwitch) {
@@ -810,10 +812,12 @@ public class SwitchStatement extends Expression {
private void reportDuplicateCase(final CaseStatement duplicate, final CaseStatement original, int length) {
if (this.duplicateCaseStatements == null) {
this.scope.problemReporter().duplicateCase(original);
- this.scope.problemReporter().duplicateCase(duplicate);
+ if (duplicate != original)
+ this.scope.problemReporter().duplicateCase(duplicate);
this.duplicateCaseStatements = new CaseStatement[length];
this.duplicateCaseStatements[this.duplicateCaseStatementsCounter++] = original;
- this.duplicateCaseStatements[this.duplicateCaseStatementsCounter++] = duplicate;
+ if (duplicate != original)
+ this.duplicateCaseStatements[this.duplicateCaseStatementsCounter++] = duplicate;
} else {
boolean found = false;
searchReportedDuplicate: for (int k = 2; k < this.duplicateCaseStatementsCounter; k++) {

Back to the top