Search in sources :

Example 1 with AndroidReference

use of com.android.tools.klint.client.api.AndroidReference in project kotlin by JetBrains.

the class ResourceEvaluator method getResourceConstant.

/** Returns a resource URL based on the field reference in the code */
@Nullable
public static ResourceUrl getResourceConstant(@NonNull UElement node) {
    AndroidReference androidReference = toAndroidReferenceViaResolve(node);
    if (androidReference == null) {
        return null;
    }
    String name = androidReference.getName();
    ResourceType type = androidReference.getType();
    boolean isFramework = androidReference.getPackage().equals("android");
    return ResourceUrl.create(type, name, isFramework, false);
}
Also used : AndroidReference(com.android.tools.klint.client.api.AndroidReference) ResourceType(com.android.resources.ResourceType) Nullable(com.android.annotations.Nullable)

Example 2 with AndroidReference

use of com.android.tools.klint.client.api.AndroidReference in project kotlin by JetBrains.

the class LayoutInflationDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
    assert method.getName().equals(INFLATE);
    if (call.getReceiver() == null) {
        return;
    }
    List<UExpression> arguments = call.getValueArguments();
    if (arguments.size() < 2) {
        return;
    }
    UExpression second = arguments.get(1);
    if (!UastLiteralUtils.isNullLiteral(second)) {
        return;
    }
    UExpression first = arguments.get(0);
    AndroidReference androidReference = UastLintUtils.toAndroidReferenceViaResolve(first);
    if (androidReference == null) {
        return;
    }
    String layoutName = androidReference.getName();
    if (context.getScope().contains(Scope.RESOURCE_FILE)) {
        // incrementally
        if (!context.getDriver().isSuppressed(context, ISSUE, call)) {
            if (mPendingErrors == null) {
                mPendingErrors = Lists.newArrayList();
            }
            Location location = context.getUastLocation(second);
            mPendingErrors.add(Pair.of(layoutName, location));
        }
    } else if (hasLayoutParams(context, layoutName)) {
        context.report(ISSUE, call, context.getUastLocation(second), ERROR_MESSAGE);
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) AndroidReference(com.android.tools.klint.client.api.AndroidReference) Location(com.android.tools.klint.detector.api.Location)

Aggregations

AndroidReference (com.android.tools.klint.client.api.AndroidReference)2 Nullable (com.android.annotations.Nullable)1 ResourceType (com.android.resources.ResourceType)1 Location (com.android.tools.klint.detector.api.Location)1 UExpression (org.jetbrains.uast.UExpression)1