Search in sources :

Example 6 with UCallExpression

use of org.jetbrains.uast.UCallExpression 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

UCallExpression (org.jetbrains.uast.UCallExpression)6 UExpression (org.jetbrains.uast.UExpression)5 Nullable (com.android.annotations.Nullable)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)3 PsiClass (com.intellij.psi.PsiClass)3 PsiElement (com.intellij.psi.PsiElement)3 PsiMethod (com.intellij.psi.PsiMethod)3 PsiVariable (com.intellij.psi.PsiVariable)3 UQualifiedReferenceExpression (org.jetbrains.uast.UQualifiedReferenceExpression)3 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)3 Location (com.android.tools.klint.detector.api.Location)2 PsiClassType (com.intellij.psi.PsiClassType)2 PsiType (com.intellij.psi.PsiType)2 UElement (org.jetbrains.uast.UElement)2 UIfExpression (org.jetbrains.uast.UIfExpression)2 ULiteralExpression (org.jetbrains.uast.ULiteralExpression)2 UParenthesizedExpression (org.jetbrains.uast.UParenthesizedExpression)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1