Bug 574870 - [16] Declaration of pattern variable in instanceof
expression is not found 

The pattern variable declaration (introduced in J16) in instanceof
expression was never searched for. With this change, the JDT search
looks for declaration in the pattern variable declaration.

Change-Id: I87b73b82da7dc17e87de3fb18ae592f8f4c9053b
Signed-off-by: Vikas Chandra <Vikas.Chandra@in.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/183101
Reviewed-by: Manoj Palat <manpalat@in.ibm.com>
Tested-by: JDT Bot <jdt-bot@eclipse.org>
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs16Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs16Tests.java
index f0f85e3..2c43f22 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs16Tests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs16Tests.java
@@ -18,6 +18,7 @@
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.ILocalVariable;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.core.WorkingCopyOwner;
 import org.eclipse.jdt.core.search.IJavaSearchScope;
@@ -489,6 +490,136 @@
 					"src/b573388/C.java b573388.C [C] EXACT_MATCH"
 			);
 		}
+		public void testBug574870_1() throws CoreException {
+			this.workingCopies = new ICompilationUnit[1];
+			this.workingCopies[0] = getWorkingCopy(
+					"/JavaSearchBugs/src/X.java",
+					"public class X {\n" +
+							"private void method(Object o) {\n" +
+							"if ((o instanceof String xvar )) \n" +
+							"{\n" +
+							" System.out.println(/*here*/xvar);\n" +
+							"}\n" +
+
+							"}\n" +
+
+					"}");
+
+			// working copies
+			try {
+
+				String str = this.workingCopies[0].getSource();
+				String selection = "/*here*/xvar";
+				int start = str.indexOf(selection);
+				int length = selection.length();
+
+				IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
+				ILocalVariable local = (ILocalVariable) elements[0];
+				search(local, DECLARATIONS, EXACT_RULE);
+				assertSearchResults("src/X.java void X.method(Object).xvar [xvar] EXACT_MATCH");
+
+			} finally {
+
+			}
+		}
+		public void testBug574870_2() throws CoreException {
+			this.workingCopies = new ICompilationUnit[1];
+			this.workingCopies[0] = getWorkingCopy(
+					"/JavaSearchBugs/src/X.java",
+					"public class X {\n" +
+							"private void method(Object o) {\n" +
+							"if ((o instanceof String /*here*/xvar )) \n" +
+							"{\n" +
+							" System.out.println(xvar+xvar);\n" +
+							"}\n" +
+
+							"}\n" +
+
+					"}");
+
+			// working copies
+			try {
+
+				String str = this.workingCopies[0].getSource();
+				String selection = "/*here*/xvar";
+				int start = str.indexOf(selection);
+				int length = selection.length();
+
+				IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
+				ILocalVariable local = (ILocalVariable) elements[0];
+				search(local, REFERENCES, EXACT_RULE);
+				assertSearchResults("src/X.java void X.method(Object) [xvar] EXACT_MATCH\n"
+						+ "src/X.java void X.method(Object) [xvar] EXACT_MATCH");
+
+			} finally {
+
+			}
+		}
+		public void testBug574870_3() throws CoreException {
+			this.workingCopies = new ICompilationUnit[1];
+			this.workingCopies[0] = getWorkingCopy(
+					"/JavaSearchBugs/src/X.java",
+					"public class X {\n" +
+							"private void method(Object o) {\n" +
+							"if ((o instanceof String /*here*/xvar )) \n" +
+							"{\n" +
+							" System.out.println(xvar+xvar);\n" +
+							"}\n" +
+
+							"}\n" +
+
+					"}");
+
+			// working copies
+			try {
+
+				String str = this.workingCopies[0].getSource();
+				String selection = "/*here*/xvar";
+				int start = str.indexOf(selection);
+				int length = selection.length();
+
+				IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
+				ILocalVariable local = (ILocalVariable) elements[0];
+				search(local, ALL_OCCURRENCES, EXACT_RULE);
+				assertSearchResults("src/X.java void X.method(Object).xvar [xvar] EXACT_MATCH\n"
+						+ "src/X.java void X.method(Object) [xvar] EXACT_MATCH\n"
+						+ "src/X.java void X.method(Object) [xvar] EXACT_MATCH");
+
+			} finally {
+
+			}
+		}
+		public void testBug574870_4() throws CoreException {
+			this.workingCopies = new ICompilationUnit[1];
+			this.workingCopies[0] = getWorkingCopy(
+					"/JavaSearchBugs/src/X.java",
+					"public class X {\n" +
+							"private void method(Object o) {\n" +
+							"if (o instanceof String xvar ) \n" +
+							"{\n" +
+							" System.out.println(/*here*/xvar);\n" +
+							"}\n" +
+
+							"}\n" +
+
+					"}");
+
+			// working copies
+			try {
+
+				String str = this.workingCopies[0].getSource();
+				String selection = "/*here*/xvar";
+				int start = str.indexOf(selection);
+				int length = selection.length();
+				IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
+				ILocalVariable local = (ILocalVariable) elements[0];
+				search(local, DECLARATIONS, EXACT_RULE);
+				assertSearchResults("src/X.java void X.method(Object).xvar [xvar] EXACT_MATCH");
+
+			} finally {
+
+			}
+		}
 }
 
 
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocatorParser.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocatorParser.java
index 9e5e3be..ac7e556 100644
--- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocatorParser.java
+++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocatorParser.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2020 IBM Corporation and others.
+ * Copyright (c) 2000, 2021 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -392,6 +392,7 @@
 		InstanceOfExpression expression = (InstanceOfExpression) this.expressionStack[this.expressionPtr];
 		this.patternLocator.match(expression.type, this.nodeSet);
 	}
+	matchPatternVariable();
 }
 @Override
 protected void consumeInstanceOfExpressionWithName() {
@@ -400,6 +401,18 @@
 		InstanceOfExpression expression = (InstanceOfExpression) this.expressionStack[this.expressionPtr];
 		this.patternLocator.match(expression.type, this.nodeSet);
 	}
+	matchPatternVariable();
+}
+
+private void matchPatternVariable() {
+	if (this.patternFineGrain == 0) {
+		InstanceOfExpression expression = (InstanceOfExpression) this.expressionStack[this.expressionPtr];
+		LocalDeclaration elementVariable = expression.elementVariable;
+		if (elementVariable != null) {
+			// if pattern variable present, match that
+			this.patternLocator.match(elementVariable, this.nodeSet);
+		}
+	}
 }
 @Override
 protected void consumeInterfaceType() {