@Override public void visitLiteralExpression(@NotNull PsiLiteralExpression expression) { super.visitLiteralExpression(expression); final PsiType type = expression.getType(); if (!ClassUtils.isPrimitiveNumericType(type)) { return; } if (PsiType.CHAR.equals(type)) { return; } if (isSpecialCaseLiteral(expression)) { return; } if (ExpressionUtils.isDeclaredConstant(expression)) { return; } if (ignoreInHashCode) { final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class); if (MethodUtils.isHashCode(containingMethod)) { return; } } if (ignoreInTestCode && TestUtils.isInTestCode(expression)) { return; } final PsiElement parent = expression.getParent(); if (parent instanceof PsiPrefixExpression) { registerError(parent); } else { registerError(expression); } }
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)); }
/** * Sets the basic element information from the given type. * * @param element the element to set information from the type * @param factory * @param type the type * @param modifiers modifier list * @since 2.15 */ private static void setElementInfo( AbstractElement element, PsiElementFactory factory, PsiType type, PsiModifierList modifiers) { // type names element.setTypeName(PsiAdapter.getTypeClassName(type)); element.setTypeQualifiedName(PsiAdapter.getTypeQualifiedClassName(type)); element.setType(type.getCanonicalText()); // arrays, collections and maps types if (PsiAdapter.isObjectArrayType(type)) { element.setObjectArray(true); element.setArray(true); // additional specify if the element is a string array if (PsiAdapter.isStringArrayType(type)) element.setStringArray(true); } else if (PsiAdapter.isPrimitiveArrayType(type)) { element.setPrimitiveArray(true); element.setArray(true); } if (PsiAdapter.isCollectionType(factory, type)) element.setCollection(true); if (PsiAdapter.isListType(factory, type)) element.setList(true); if (PsiAdapter.isSetType(factory, type)) element.setSet(true); if (PsiAdapter.isMapType(factory, type)) element.setMap(true); // other types if (PsiAdapter.isPrimitiveType(type)) element.setPrimitive(true); if (PsiAdapter.isObjectType(factory, type)) element.setObject(true); if (PsiAdapter.isStringType(factory, type)) element.setString(true); if (PsiAdapter.isNumericType(factory, type)) element.setNumeric(true); if (PsiAdapter.isDateType(factory, type)) element.setDate(true); if (PsiAdapter.isCalendarType(factory, type)) element.setCalendar(true); if (PsiAdapter.isBooleanType(factory, type)) element.setBoolean(true); if (PsiType.VOID.equals(type)) element.setVoid(true); if (PsiType.LONG.equals(type)) element.setLong(true); if (PsiType.FLOAT.equals(type)) element.setFloat(true); if (PsiType.DOUBLE.equals(type)) element.setDouble(true); if (PsiType.BYTE.equals(type)) element.setByte(true); if (PsiType.CHAR.equals(type)) element.setChar(true); if (PsiType.SHORT.equals(type)) element.setShort(true); element.setNestedArray(PsiAdapter.isNestedArray(type)); // modifiers if (modifiers != null) { if (modifiers.hasModifierProperty(PsiModifier.STATIC)) element.setModifierStatic(true); if (modifiers.hasModifierProperty(PsiModifier.FINAL)) element.setModifierFinal(true); if (modifiers.hasModifierProperty(PsiModifier.PUBLIC)) { element.setModifierPublic(true); } else if (modifiers.hasModifierProperty(PsiModifier.PROTECTED)) { element.setModifierProtected(true); } else if (modifiers.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { element.setModifierPackageLocal(true); } else if (modifiers.hasModifierProperty(PsiModifier.PRIVATE)) element.setModifierPrivate(true); } }
public static Value createValue(VirtualMachineProxyImpl vm, String expectedType, char value) { if (PsiType.CHAR.getPresentableText().equals(expectedType)) { return vm.mirrorOf(value); } if (PsiType.LONG.getPresentableText().equals(expectedType)) { return vm.mirrorOf((long) value); } if (PsiType.INT.getPresentableText().equals(expectedType)) { return vm.mirrorOf((int) value); } if (PsiType.SHORT.getPresentableText().equals(expectedType)) { return vm.mirrorOf((short) value); } if (PsiType.BYTE.getPresentableText().equals(expectedType)) { return vm.mirrorOf((byte) value); } return null; }
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); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiLiteralExpression)) { return false; } final PsiLiteralExpression expression = (PsiLiteralExpression) element; final PsiType type = expression.getType(); if (!PsiType.CHAR.equals(type)) { return false; } final String charLiteral = element.getText(); if (charLiteral.length() < 2) return false; // Incomplete char literal probably without closing amp final String charText = charLiteral.substring(1, charLiteral.length() - 1); if (StringUtil.unescapeStringCharacters(charText).length() != 1) { // not satisfied with character literals of more than one character return false; } return isInConcatenationContext(expression); }
@SuppressWarnings({"HardCodedStringLiteral"}) public static String getPrimitiveSignature(String typeName) { if (PsiType.BOOLEAN.getCanonicalText().equals(typeName)) { return "Z"; } else if (PsiType.BYTE.getCanonicalText().equals(typeName)) { return "B"; } else if (PsiType.CHAR.getCanonicalText().equals(typeName)) { return "C"; } else if (PsiType.SHORT.getCanonicalText().equals(typeName)) { return "S"; } else if (PsiType.INT.getCanonicalText().equals(typeName)) { return "I"; } else if (PsiType.LONG.getCanonicalText().equals(typeName)) { return "J"; } else if (PsiType.FLOAT.getCanonicalText().equals(typeName)) { return "F"; } else if (PsiType.DOUBLE.getCanonicalText().equals(typeName)) { return "D"; } else if (PsiType.VOID.getCanonicalText().equals(typeName)) { return "V"; } return null; }