Bug 572100 - [refactoring][search] [record] Move Type to a New File ...
throws a ClassCastException 

Change-Id: I54b0938f89c75bb2fda97549327dde3e53220612
Signed-off-by: Vikas Chandra <Vikas.Chandra@in.ibm.com>
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 9381ab6..c78cf30 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
@@ -24,7 +24,11 @@
 import org.eclipse.jdt.core.search.ReferenceMatch;
 import org.eclipse.jdt.core.search.SearchEngine;
 import org.eclipse.jdt.core.search.SearchMatch;
+import org.eclipse.jdt.core.search.SearchParticipant;
+import org.eclipse.jdt.core.search.SearchPattern;
 import org.eclipse.jdt.core.search.TypeReferenceMatch;
+import org.eclipse.jdt.internal.core.SourceType;
+import org.eclipse.jdt.internal.core.search.matching.DeclarationOfAccessedFieldsPattern;
 
 import junit.framework.Test;
 
@@ -422,6 +426,36 @@
 						+ "src/X.java void X.foo():C#1 [Inter] EXACT_MATCH");
 
 			}
+		public void testBug572100 () throws CoreException {
+			this.workingCopies = new ICompilationUnit[1];
+			this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/Bug572100/X.java",
+					"public interface X {\n"+
+				 " interface inter1  {\n"+
+				   " record record1(Class<?> type) implements inter1 {\n"+
+				  "    public record1 {\n"+
+				   "     if (!type.isPrimitive()) {\n"+
+				   "    }\n"+
+				    "  }\n"+
+				   " }\n"+
+				 " }\n"+
+				"}\n"
+				);
+			String str = this.workingCopies[0].getSource();
+			String selection = "inter1";
+			int start = str.indexOf(selection);
+			int length = selection.length();
+			IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
+			SourceType st = (SourceType)elements[0];
+			SearchPattern pattern = new DeclarationOfAccessedFieldsPattern(st);
+			new SearchEngine(this.workingCopies).search(pattern,
+			new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
+			getJavaSearchWorkingCopiesScope(),
+			this.resultCollector,
+			null);
+			assertSearchResults(
+					""
+			);
+		}
 }
 
 
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java
index 5ef2789..c2129a0 100644
--- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java
+++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.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
@@ -178,8 +178,10 @@
 				int otherMax = qNameRef.otherBindings == null ? 0 : qNameRef.otherBindings.length;
 				for (int i = 0; i < otherMax; i++)
 					reportDeclaration(qNameRef.otherBindings[i], locator, declPattern.knownFields);
-			} else if (reference instanceof SingleNameReference) {
-				reportDeclaration((FieldBinding)((SingleNameReference) reference).binding, locator, declPattern.knownFields);
+			} else if (reference instanceof SingleNameReference ) {
+				if(((SingleNameReference) reference).binding instanceof FieldBinding) {
+					reportDeclaration((FieldBinding)((SingleNameReference) reference).binding, locator, declPattern.knownFields);
+				}
 			}
 		}
 	} else if (reference instanceof ImportReference) {