List<PsiLocalVariable> (@NotNull PsiCaseLabelElement label, @NotNull PsiSwitchBlock switchBlock) { PsiPrimaryPattern pattern = JavaPsiPatternUtil.getTypedPattern(label); if (pattern == null) return Collections.emptyList(); PsiType type = JavaPsiPatternUtil.getPatternType(pattern); if (type == null) return Collections.emptyList(); PsiExpression selector = switchBlock.getExpression(); if (selector == null) return Collections.emptyList(); PsiType selectorType = selector.getType(); if (selectorType == null) return Collections.emptyList(); PsiPatternVariable topLevelVariable = JavaPsiPatternUtil.getPatternVariable(pattern); String declarationStatementText = type.getPresentableText() + " "; if (topLevelVariable != null) { declarationStatementText += topLevelVariable.getName() + "="; } else { VariableNameGenerator generator = new VariableNameGenerator(switchBlock, VariableKind.LOCAL_VARIABLE); String newVarName = generator.byType(JavaPsiPatternUtil.getPatternType(label)).generate(true); declarationStatementText += newVarName + "="; } if (!type.isAssignableFrom(selectorType)) { declarationStatementText += "(" + type.getPresentableText() + ")"; } declarationStatementText += selector.getText() + ";"; PsiLocalVariable newVariable = createVariable(declarationStatementText, label); if (pattern instanceof PsiDeconstructionPattern deconstructionPattern) { PsiFile copy = (PsiFile)switchBlock.getContainingFile().copy(); PsiElement contextCopy = PsiTreeUtil.findSameElementInCopy(switchBlock.getParent(), copy); contextCopy.add(newVariable); ArrayList<PsiLocalVariable> list = new ArrayList<>(); list.add(newVariable); return collectVariables(deconstructionPattern, newVariable, contextCopy, list); } return new SmartList<>(newVariable); }