private boolean checkConstantValueVariableUse( @Nullable PsiExpression expression, @NotNull PsiExpression constantExpression, @NotNull PsiElement body) { final PsiType constantType = constantExpression.getType(); if (PsiType.DOUBLE.equals(constantType)) { final Object result = ExpressionUtils.computeConstantExpression(constantExpression, false); if (Double.valueOf(0.0).equals(result) || Double.valueOf(-0.0).equals(result)) { return false; } } if (!(expression instanceof PsiReferenceExpression)) { return false; } final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression; final PsiElement target = referenceExpression.resolve(); if (!(target instanceof PsiVariable)) { return false; } if (target instanceof PsiField) { return false; } final PsiVariable variable = (PsiVariable) target; final VariableReadVisitor visitor = new VariableReadVisitor(variable); body.accept(visitor); if (!visitor.isRead()) { return false; } registerError(visitor.getReference(), constantExpression); return true; }
public static boolean isAssociativeOperation(PsiPolyadicExpression expression) { final IElementType tokenType = expression.getOperationTokenType(); final PsiType type = expression.getType(); final PsiPrimitiveType primitiveType; if (type instanceof PsiClassType) { primitiveType = PsiPrimitiveType.getUnboxedType(type); if (primitiveType == null) { return false; } } else if (type instanceof PsiPrimitiveType) { primitiveType = (PsiPrimitiveType) type; } else { return false; } if (JavaTokenType.PLUS == tokenType || JavaTokenType.ASTERISK == tokenType) { return !PsiType.FLOAT.equals(primitiveType) && !PsiType.DOUBLE.equals(primitiveType); } else if (JavaTokenType.EQEQ == tokenType || JavaTokenType.NE == tokenType) { return PsiType.BOOLEAN.equals(primitiveType); } else if (JavaTokenType.AND == tokenType || JavaTokenType.OR == tokenType || JavaTokenType.XOR == tokenType) { return true; } else if (JavaTokenType.OROR == tokenType || JavaTokenType.ANDAND == tokenType) { return true; } return false; }
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); } }