コード例 #1
0
 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);
 }
コード例 #2
0
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor) {
   final PsiExpression expression = (PsiExpression) descriptor.getPsiElement();
   final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false);
   if (expectedType == null) {
     return;
   }
   final String expectedTypeText = expectedType.getCanonicalText();
   final String classToConstruct;
   if (s_boxingClasses.containsValue(expectedTypeText)) {
     classToConstruct = expectedTypeText;
   } else {
     final PsiType type = expression.getType();
     if (type == null) {
       return;
     }
     final String expressionTypeText = type.getCanonicalText();
     classToConstruct = s_boxingClasses.get(expressionTypeText);
   }
   if (shortcutReplace(expression, classToConstruct)) {
     return;
   }
   final PsiExpression strippedExpression = ParenthesesUtils.stripParentheses(expression);
   if (strippedExpression == null) {
     return;
   }
   @NonNls final String expressionText = strippedExpression.getText();
   @NonNls final String newExpression;
   if ("true".equals(expressionText)) {
     newExpression = "java.lang.Boolean.TRUE";
   } else if ("false".equals(expressionText)) {
     newExpression = "java.lang.Boolean.FALSE";
   } else {
     newExpression = classToConstruct + ".valueOf(" + expressionText + ')';
   }
   replaceExpression(expression, newExpression);
 }