Search in sources :

Example 1 with UBinaryExpression

use of org.jetbrains.uast.UBinaryExpression in project kotlin by JetBrains.

the class SetTextDetector method checkNode.

private static void checkNode(@NonNull JavaContext context, @Nullable UElement node) {
    if (node instanceof ULiteralExpression) {
        Object value = ((ULiteralExpression) node).getValue();
        if (value instanceof String && value.toString().matches(WORD_PATTERN)) {
            context.report(SET_TEXT_I18N, node, context.getUastLocation(node), "String literal in `setText` can not be translated. Use Android " + "resources instead.");
        }
    } else if (node instanceof UCallExpression) {
        PsiMethod calledMethod = ((UCallExpression) node).resolve();
        if (calledMethod != null && TO_STRING_NAME.equals(calledMethod.getName())) {
            PsiClass containingClass = UastUtils.getContainingClass(calledMethod);
            if (containingClass == null) {
                return;
            }
            PsiClass superClass = containingClass.getSuperClass();
            if (superClass != null && NUMBER_CLS.equals(superClass.getQualifiedName())) {
                context.report(SET_TEXT_I18N, node, context.getUastLocation(node), "Number formatting does not take into account locale settings. " + "Consider using `String.format` instead.");
            }
        }
    } else if (node instanceof UQualifiedReferenceExpression) {
        UQualifiedReferenceExpression expression = (UQualifiedReferenceExpression) node;
        checkNode(context, expression.getReceiver());
        checkNode(context, expression.getSelector());
    } else if (node instanceof UBinaryExpression) {
        UBinaryExpression expression = (UBinaryExpression) node;
        if (expression.getOperator() == UastBinaryOperator.PLUS) {
            context.report(SET_TEXT_I18N, node, context.getUastLocation(node), "Do not concatenate text displayed with `setText`. " + "Use resource string with placeholders.");
        }
        checkNode(context, expression.getLeftOperand());
        checkNode(context, expression.getRightOperand());
    }
}
Also used : ULiteralExpression(org.jetbrains.uast.ULiteralExpression) PsiMethod(com.intellij.psi.PsiMethod) UQualifiedReferenceExpression(org.jetbrains.uast.UQualifiedReferenceExpression) PsiClass(com.intellij.psi.PsiClass) UBinaryExpression(org.jetbrains.uast.UBinaryExpression) UCallExpression(org.jetbrains.uast.UCallExpression)

Aggregations

PsiClass (com.intellij.psi.PsiClass)1 PsiMethod (com.intellij.psi.PsiMethod)1 UBinaryExpression (org.jetbrains.uast.UBinaryExpression)1 UCallExpression (org.jetbrains.uast.UCallExpression)1 ULiteralExpression (org.jetbrains.uast.ULiteralExpression)1 UQualifiedReferenceExpression (org.jetbrains.uast.UQualifiedReferenceExpression)1