diff options
author | Stephan Herrmann | 2016-05-20 19:49:45 +0000 |
---|---|---|
committer | Stephan Herrmann | 2022-03-29 21:28:30 +0000 |
commit | 1266aa6753ee4564fa7ca0c3e4b8fa11f9aecfed (patch) | |
tree | 63a3c5c346663fd0d8bce60da4692ca7806c4285 | |
parent | 2d32872411acb205fea39a85366d89fab0e54a71 (diff) | |
download | eclipse.jdt.core-1266aa6753ee4564fa7ca0c3e4b8fa11f9aecfed.tar.gz eclipse.jdt.core-1266aa6753ee4564fa7ca0c3e4b8fa11f9aecfed.tar.xz eclipse.jdt.core-1266aa6753ee4564fa7ca0c3e4b8fa11f9aecfed.zip |
Bug 494204: Various exceptions caused by incomplete switch statementI20220331-1800I20220329-1800
- tests
Change-Id: Ied930a0465fe9df4563c87c12b1e7d8692be1958
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/73328
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Reviewed-by: Stephan Herrmann <stephan.herrmann@berlin.de>
2 files changed, 53 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests_1_5.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests_1_5.java index b2b8f9956b..f814807900 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests_1_5.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests_1_5.java @@ -3002,4 +3002,53 @@ public void test0125() throws CoreException { deleteProject("P0125"); } } + +public void testBrokenSwitch0() throws JavaModelException { + ICompilationUnit cu = getWorkingCopy("/Resolve/src/Test.java", + "interface ILog {\n" + + " void log(String status);\n" + + "}\n" + + "public class Test {\n" + + " private static ILog test() {\n" + + " return new ILog() {\n" + + " @Override\n" + + " public void log(String status) {\n" + + " switch (status.length()) { // here\n" + + " case\n" + + " }\n" + + " }\n" + + " };\n" + + " }\n" + + "}\n"); + IJavaElement[] elements = codeSelect(cu, "length()", "length"); + assertElementsEqual( + "Unexpected elements", + "length() [in String [in String.class [in java.lang [in "+ getExternalJCLPathString("1.5") + "]]]]", + elements); +} + +public void testBrokenSwitch1() throws JavaModelException { + ICompilationUnit cu = getWorkingCopy("/Resolve/src/Test.java", + "interface ILog {\n" + + " void log(String status);\n" + + "}\n" + + "public class Test {\n" + + " private static ILog test() {\n" + + " return new ILog() {\n" + + " @Override\n" + + " public void log(String status) {\n" + + " Boolean severity = Boolean.FALSE;" + + " switch (status.length()) { // here\n" + + " case\n" + + " }\n" + + " }\n" + + " };\n" + + " }\n" + + "}\n"); + IJavaElement[] elements = codeSelect(cu, "length()", "length"); + assertElementsEqual( + "Unexpected elements", + "length() [in String [in String.class [in java.lang [in "+ getExternalJCLPathString("1.5") + "]]]]", + elements); +} } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java index 67dc54444f..15e854f4dd 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java @@ -184,14 +184,14 @@ private void buildMoreCompletionContext(Expression expression) { int info = topKnownElementInfo(SELECTION_OR_ASSIST_PARSER); nextElement : switch (kind) { case K_BETWEEN_CASE_AND_COLONORARROW : - if(this.expressionPtr > 0) { + if(this.expressionPtr >= info && info > -1) { SwitchStatement switchStatement = new SwitchStatement(); - switchStatement.expression = this.expressionStack[this.expressionPtr - 1]; + switchStatement.expression = this.expressionStack[info]; // info is pointer to top expr when encountering 'case' if(this.astLengthPtr > -1 && this.astPtr > -1) { int length = this.astLengthStack[this.astLengthPtr]; int newAstPtr = this.astPtr - length; ASTNode firstNode = this.astStack[newAstPtr + 1]; - if(length != 0 && firstNode.sourceStart > switchStatement.expression.sourceEnd) { + if(length != 0 && firstNode instanceof Statement && firstNode.sourceStart > switchStatement.expression.sourceEnd) { switchStatement.statements = new Statement[length + 1]; System.arraycopy( this.astStack, @@ -1327,7 +1327,7 @@ protected void consumeToken(int token) { if (isInsideMethod() || isInsideFieldInitialization()) { switch (token) { case TokenNamecase : - pushOnElementStack(K_BETWEEN_CASE_AND_COLONORARROW); + pushOnElementStack(K_BETWEEN_CASE_AND_COLONORARROW, this.expressionPtr); break; case TokenNameCOMMA : switch (topKnownElementKind(SELECTION_OR_ASSIST_PARSER)) { |