From fcf05f89bbb881d5d91faeec9caaac97c94ebe78 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 --- .../core/tests/model/ResolveTests12To15.java | 34 ++++++++++++++++++- .../codeassist/select/SelectionParser.java | 34 ++++++++++++++++--- 2 files changed, 63 insertions(+), 5 deletions(-) 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..f7856dc806d 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() {