void (@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); final PsiMethod constructor = expression.resolveConstructor(); if (constructor == null) { return; } final PsiClass aClass = constructor.getContainingClass(); if (aClass == null) { return; } final PsiParameterList parameterList = constructor.getParameterList(); final int count = parameterList.getParametersCount(); if (count == 0) { return; } final PsiParameter[] parameters = parameterList.getParameters(); final String qName = aClass.getQualifiedName(); if (CommonClassNames.JAVA_LANG_STRING.equals(qName)) { if (!parameters[0].getType().equalsToText("byte[]") || hasCharsetType(parameters[count - 1])) { return; } } else if ("java.io.InputStreamReader".equals(qName) || "java.io.OutputStreamWriter".equals(qName) || "java.io.PrintStream".equals(qName)) { if (hasCharsetType(parameters[count - 1])) { return; } } else if ("java.io.PrintWriter".equals(qName)) { if (count > 1 && hasCharsetType(parameters[count - 1]) || parameters[0].getType().equalsToText("java.io.Writer")) { return; } if (count == 1) { PsiExpressionList args = expression.getArgumentList(); if (args != null && PsiUtil.skipParenthesizedExprDown(ArrayUtil.getFirstElement(args.getExpressions())) instanceof PsiReferenceExpression ref && ref.resolve() instanceof PsiField field) { if (field.getName().equals("out") || field.getName().equals("err")) { PsiClass containingClass = field.getContainingClass(); if (containingClass != null && CommonClassNames.JAVA_LANG_SYSTEM.equals(containingClass.getQualifiedName())) { // new PrintWriter(System.out): likely system default encoding is expected return; } } } } } else if ("java.util.Formatter".equals(qName)) { if (count > 1 && hasCharsetType(parameters[1])) { return; } final PsiType firstType = parameters[0].getType(); if (!firstType.equalsToText(CommonClassNames.JAVA_LANG_STRING) && !firstType.equalsToText("java.io.File") && !firstType.equalsToText("java.io.OutputStream")) { return; } } else if ("java.util.Scanner".equals(qName)) { if (count > 1 && hasCharsetType(parameters[1])) { return; } final PsiType firstType = parameters[0].getType(); if (!firstType.equalsToText("java.io.InputStream") && !firstType.equalsToText("java.io.File") && !firstType.equalsToText("java.nio.file.Path") && !firstType.equalsToText("java.nio.channels.ReadableByteChannel")) { return; } } else if ("java.io.FileReader".equals(qName) || "java.io.FileWriter".equals(qName)) { if (count > 1 && hasCharsetType(parameters[1])) { return; } } else { return; } registerNewExpressionError(expression, expression); }