private static boolean isValueOfCall(PsiMethodCallExpression methodCallExpression) { final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return false; } final PsiExpression argument = arguments[0]; final PsiType type = argument.getType(); return (MethodCallUtils.isCallToMethod( methodCallExpression, CommonClassNames.JAVA_LANG_INTEGER, null, "valueOf", PsiType.INT) && PsiType.INT.equals(type)) || (MethodCallUtils.isCallToMethod( methodCallExpression, CommonClassNames.JAVA_LANG_SHORT, null, "valueOf", PsiType.SHORT) && PsiType.SHORT.equals(type)) || (MethodCallUtils.isCallToMethod( methodCallExpression, CommonClassNames.JAVA_LANG_BYTE, null, "valueOf", PsiType.BYTE) && PsiType.BYTE.equals(type)) || (MethodCallUtils.isCallToMethod( methodCallExpression, CommonClassNames.JAVA_LANG_LONG, null, "valueOf", PsiType.LONG) && PsiType.LONG.equals(type)) || (MethodCallUtils.isCallToMethod( methodCallExpression, CommonClassNames.JAVA_LANG_CHARACTER, null, "valueOf", PsiType.CHAR) && PsiType.CHAR.equals(type)) || (MethodCallUtils.isCallToMethod( methodCallExpression, CommonClassNames.JAVA_LANG_DOUBLE, null, "valueOf", PsiType.DOUBLE) && PsiType.DOUBLE.equals(type)) || (MethodCallUtils.isCallToMethod( methodCallExpression, CommonClassNames.JAVA_LANG_FLOAT, null, "valueOf", PsiType.FLOAT) && PsiType.FLOAT.equals(type)); }
private void checkExpression(@NotNull PsiExpression expression) { if (expression.getParent() instanceof PsiParenthesizedExpression) { return; } final PsiType expressionType = expression.getType(); if (expressionType == null || expressionType.equals(PsiType.VOID) || !TypeConversionUtil.isPrimitiveAndNotNull(expressionType)) { return; } final PsiPrimitiveType primitiveType = (PsiPrimitiveType) expressionType; final PsiClassType boxedType = primitiveType.getBoxedType(expression); if (boxedType == null) { return; } final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false); if (expectedType == null || ClassUtils.isPrimitive(expectedType)) { return; } if (!expectedType.isAssignableFrom(boxedType)) { // JLS 5.2 Assignment Conversion // check if a narrowing primitive conversion is applicable if (!(expectedType instanceof PsiClassType) || !PsiUtil.isConstantExpression(expression)) { return; } final PsiClassType classType = (PsiClassType) expectedType; final String className = classType.getCanonicalText(); if (!convertableBoxedClassNames.contains(className)) { return; } if (!PsiType.BYTE.equals(expressionType) && !PsiType.CHAR.equals(expressionType) && !PsiType.SHORT.equals(expressionType) && !PsiType.INT.equals(expressionType)) { return; } } if (ignoreAddedToCollection && isAddedToCollection(expression)) { return; } registerError(expression); }