Generator<ActionOnFile> (@NotNull PsiFile file) { InvokeIntention anyIntentionAroundInstanceOf = new InvokeIntentionAtElement( file, new JavaIntentionPolicy(), PsiInstanceOfExpression.class, stmt -> { PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(stmt, PsiCodeBlock.class); if (codeBlock != null) { return codeBlock; } return stmt.getParent(); }); InvokeIntention replaceToPattern = new InvokeIntentionAtElement(file, new IntentionPolicy() { @Override protected boolean shouldSkipIntention(@NotNull String actionText) { return !actionText.matches("Replace '.+' with pattern variable"); } }, f -> { Collection<PsiElement> targets = new ArrayList<>(); for (PsiTypeCastExpression cast : PsiTreeUtil.findChildrenOfType(f, PsiTypeCastExpression.class)) { PsiInstanceOfExpression candidate = InstanceOfUtils.findPatternCandidate(cast); if (candidate != null) { PsiElement element = PsiUtil.skipParenthesizedExprUp(cast.getParent()); if (element instanceof PsiLocalVariable) { targets.add(((PsiLocalVariable)element).getNameIdentifier()); } } } return targets; }); return Generator.sampledFrom(anyIntentionAroundInstanceOf, replaceToPattern, new StripTestDataMarkup(file)); }