From be1b1b2e29056cb916488b96486d0a502c13944c Mon Sep 17 00:00:00 2001 From: Jay Arthanareeswaran Date: Mon, 25 Apr 2022 12:45:08 +0530 Subject: [PATCH] Bug 577508 - Fixing regression Signed-off-by: Jay Arthanareeswaran --- .../tests/compiler/parser/SelectionTest.java | 6 ++-- .../core/tests/model/ResolveTests12To15.java | 34 ++++++++++++++++++- .../codeassist/select/SelectionParser.java | 34 ++++++++++++++++--- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java index 1ec97fccfb3..5bd523a4fc2 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2022 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -1980,9 +1980,7 @@ public void test45() { " public X() {\n"+ " }\n"+ " void foo() {\n"+ - " if ((x instanceof s))\n" + - " {\n" + - " }\n" + + " s;\n" + " }\n"+ "}\n"; String expectedReplacedSource = "Object"; diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests12To15.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests12To15.java index cec5d816009..f05551545fe 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests12To15.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests12To15.java @@ -25,7 +25,7 @@ public class ResolveTests12To15 extends AbstractJavaModelTests { ICompilationUnit wc = null; static { -// TESTS_NAMES = new String[] { "testBug577508_3" }; +// TESTS_NAMES = new String[] { "testBug577508_4" }; // TESTS_NUMBERS = new int[] { 124 }; // TESTS_RANGE = new int[] { 16, -1 }; } @@ -884,4 +884,36 @@ public void testBug577508_3() throws JavaModelException { elements ); } +public void testBug577508_4() throws JavaModelException { + this.wc = getWorkingCopy("/Resolve15/src/X.java", + "public class X {\n" + + " static public void main (String[] args) {\n" + + " Object[] objects = new Object[3];\n" + + " for (Object object : objects) \n" + + " if (object instanceof String string && !(object instanceof Runnable)) \n" + + " System.out.println(); // Open Declaration fails here if you remove the braces from the for loop.\n" + + " System.out.println(); // Open Declaration always fails here.\n" + + "}\n" + + "}"); + String str = this.wc.getSource(); + String selection = "println"; + int start = str.indexOf(selection); + int length = selection.length(); + IJavaElement[] elements = this.wc.codeSelect(start, length); + assertElementsEqual( + "Unexpected elements", + "println(java.lang.String) [in PrintStream [in PrintStream.class [in java.io [in "+ getExternalPath() + "jclMin14.jar]]]]", + elements + ); + + str = this.wc.getSource(); + start = str.lastIndexOf(selection); + length = selection.length(); + elements = this.wc.codeSelect(start, length); + assertElementsEqual( + "Unexpected elements", + "println(java.lang.String) [in PrintStream [in PrintStream.class [in java.io [in "+ getExternalPath() + "jclMin14.jar]]]]", + 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 d3bac7cbaf3..938703c261e 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 @@ -28,6 +28,7 @@ import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.codeassist.impl.AssistParser; import org.eclipse.jdt.internal.compiler.CompilationResult; +import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration; import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; @@ -42,6 +43,7 @@ import org.eclipse.jdt.internal.compiler.ast.FieldReference; import org.eclipse.jdt.internal.compiler.ast.GuardedPattern; import org.eclipse.jdt.internal.compiler.ast.ImportReference; +import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression; import org.eclipse.jdt.internal.compiler.ast.LambdaExpression; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; @@ -817,10 +819,34 @@ protected void consumeInsideCastExpressionWithQualifiedGenerics() { pushOnElementStack(K_CAST_STATEMENT); } @Override -protected Expression consumePatternInsideInstanceof(Pattern pattern) { - Expression exp = super.consumePatternInsideInstanceof(pattern); - pushOnPatternStack(pattern); // Push it back again. - return exp; +protected void consumeInstanceOfExpression() { + if (indexOfAssistIdentifier() < 0) { + super.consumeInstanceOfExpression(); + int length = this.expressionLengthPtr >= 0 ? + this.expressionLengthStack[this.expressionLengthPtr] : 0; + if (length > 0) { + Expression exp = this.expressionStack[this.expressionPtr]; + LocalDeclaration local = null; + if (exp instanceof InstanceOfExpression) { + local = ((InstanceOfExpression) exp).elementVariable; + } else if (exp instanceof AND_AND_Expression) { + InstanceOfExpression insExpr = (InstanceOfExpression) ((AND_AND_Expression) exp).left; + local = insExpr.elementVariable; + } + if (local != null) { + pushOnAstStack(local); + if (!this.diet) { + this.restartRecovery = true; + this.lastIgnoredToken = -1; + } + } + } + } else { + getTypeReference(this.intStack[this.intPtr--]); + this.isOrphanCompletionNode = true; + this.restartRecovery = true; + this.lastIgnoredToken = -1; + } } @Override protected void consumeInstanceOfExpressionWithName() {