List<List<PsiReferenceExpression>> (@NotNull PsiInstanceOfExpression instanceOf, @NotNull PsiPatternVariable variable, boolean shouldFindAll) { PsiType type = variable.getType(); if (!(type instanceof PsiClassType classType)) return Collections.emptyList(); PsiClass resolved = classType.resolve(); if (resolved == null || !resolved.isRecord()) return Collections.emptyList(); PsiRecordComponent[] components = resolved.getRecordComponents(); if (components.length == 0) return Collections.emptyList(); Set<PsiRecordComponent> used = new HashSet<>(); List<PsiReferenceExpression> references = VariableAccessUtils.getVariableReferences(variable, variable.getDeclarationScope()); List<List<PsiReferenceExpression>> result = new ArrayList<>(); for (int i = 0; i < components.length; i++) { result.add(new ArrayList<>()); } for (PsiReferenceExpression reference : references) { if (CastCanBeReplacedWithVariableInspection.isChangedBetween(variable, variable.getDeclarationScope(), instanceOf, reference)) { continue; } PsiRecordComponent component = getComponent(reference); if (component == null) continue; if (!used.add(component) && !shouldFindAll) continue; PsiElementFactory factory = PsiElementFactory.getInstance(reference.getProject()); PsiExpression call = factory.createExpressionFromText(reference.getText() + "." + component.getName() + "()", reference); if (SideEffectChecker.mayHaveSideEffects(call)) return Collections.emptyList(); int index = ArrayUtil.indexOf(components, component); result.get(index).add((PsiReferenceExpression)PsiUtil.skipParenthesizedExprUp(reference.getParent())); if (!shouldFindAll && used.size() == components.length) { return result; } } return used.size() == components.length ? result : Collections.emptyList(); }