PsiExpression (final PsiExpression initializer, final PsiType expectedType, final boolean inDeclaration) { if (initializer instanceof PsiTypeCastExpression) { PsiExpression operand = ((PsiTypeCastExpression)initializer).getOperand(); if (operand != null) { PsiType operandType = operand.getType(); if (operandType != null && TypeConversionUtil.isAssignable(expectedType, operandType)) { return operand; } } } else if (initializer instanceof PsiNewExpression newExpression) { if (newExpression.getArrayInitializer() != null) { if (inDeclaration) { return newExpression.getArrayInitializer(); } } else { PsiJavaCodeReferenceElement ref = newExpression.getClassOrAnonymousClassReference(); if (ref != null) { final PsiExpression tryToDetectDiamondNewExpr = ((PsiVariable)JavaPsiFacade.getElementFactory(initializer.getProject()) .createVariableDeclarationStatement("x", expectedType, initializer, initializer).getDeclaredElements()[0]) .getInitializer(); if (tryToDetectDiamondNewExpr instanceof PsiNewExpression && PsiDiamondTypeUtil.canCollapseToDiamond((PsiNewExpression)tryToDetectDiamondNewExpr, (PsiNewExpression)tryToDetectDiamondNewExpr, expectedType)) { final PsiElement paramList = RemoveRedundantTypeArgumentsUtil.replaceExplicitWithDiamond(ref.getParameterList()); return PsiTreeUtil.getParentOfType(paramList, PsiNewExpression.class); } } } } return initializer; }