TypeConversionDescriptorBase (PsiType from, PsiType to, PsiMember member, PsiExpression context, TypeMigrationLabeler labeler) { if (member != null && from instanceof PsiEllipsisType && to instanceof PsiArrayType && !(to instanceof PsiEllipsisType)) { from = ((PsiEllipsisType)from).getComponentType(); } final int dimensions = to.getArrayDimensions() - from.getArrayDimensions(); if (dimensions < 0) { final PsiExpression expression = unwrap(context, -dimensions); if (expression == null) return null; final PsiType type = expression.getType(); if (type != null && to.isAssignableFrom(type)) { return new TypeConversionDescriptorBase() { @Override public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) { PsiExpression unwrapped = unwrap(expression, -dimensions); if (unwrapped == null) { return expression; } return (PsiExpression)expression.replace(unwrapped); } }; } } if (dimensions > 0 && to instanceof PsiArrayType arrayType && shouldConvert(context)) { PsiType componentType = arrayType; for (int i = 0; i < dimensions; i++) { assert componentType instanceof PsiArrayType; componentType = ((PsiArrayType)componentType).getComponentType(); } componentType = TypeConversionUtil.erasure(componentType); if (componentType.isAssignableFrom(from)) { final boolean vararg = member instanceof PsiMethod method && method.isVarArgs(); final PsiType finalComponentType = componentType; return new TypeConversionDescriptorBase() { @Override public PsiExpression replace(PsiExpression expression, @NotNull TypeEvaluator evaluator) { String content = vararg ? getVarargText(expression) : expression.getText(); String text = "{".repeat(dimensions) + content + "}".repeat(dimensions); PsiElement parent = expression.getParent(); if (!(parent instanceof PsiVariable variable && variable.getType().equals(finalComponentType))) { text = "new " + finalComponentType.getCanonicalText() + "[]".repeat(dimensions) + text; } PsiExpression newExpression = JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(text, expression); if (vararg) { PsiElement last = expression; PsiElement anchor = last.getNextSibling(); while (anchor != null && !PsiUtil.isJavaToken(anchor, JavaTokenType.RPARENTH)) { last = anchor; anchor = anchor.getNextSibling(); } parent.deleteChildRange(expression, last); return (PsiExpression)parent.add(newExpression); } else { return (PsiExpression)expression.replace(newExpression); } } private static String getVarargText(PsiElement element) { final StringBuilder result = new StringBuilder(); while (element != null && !PsiUtil.isJavaToken(element, JavaTokenType.RPARENTH)) { result.append(element.getText()); element = element.getNextSibling(); } return result.toString(); } }; } } return null; }