Java Code Examples for com.android.tools.lint.detector.api.Context#report()

The following examples show how to use com.android.tools.lint.detector.api.Context#report() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivityDetector.java    From android-custom-lint-rules with Apache License 2.0 6 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    // Don't report issues on libraries that may not have a launcher activity
    if (context.getProject() == context.getMainProject()
            && !context.getMainProject().isLibrary()
            && mManifestLocation != null) {
        if (!mHasActivity) {
            context.report(ISSUE, mManifestLocation,
                    "Expecting " + ANDROID_MANIFEST_XML + " to have an <" + TAG_ACTIVITY +
                            "> tag.");
        } else if (!mHasLauncherActivity) {
            // Report the issue if the manifest file has no activity with a launcher intent.
            context.report(ISSUE, mManifestLocation,
                    "Expecting " + ANDROID_MANIFEST_XML +
                            " to have an activity with a launcher intent.");
        }
    }
}
 
Example 2
Source File: GradleDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void report(@NonNull Context context, @NonNull Object cookie, @NonNull Issue issue,
        @NonNull String message) {
    if (context.isEnabled(issue)) {
        // Suppressed?
        // Temporarily unconditionally checking for suppress comments in Gradle files
        // since Studio insists on an AndroidLint id prefix
        boolean checkComments = /*context.getClient().checkForSuppressComments()
                &&*/ context.containsCommentSuppress();
        if (checkComments) {
            int startOffset = getStartOffset(context, cookie);
            if (startOffset >= 0 && context.isSuppressedWithComment(startOffset, issue)) {
                return;
            }
        }

        context.report(issue, createLocation(context, cookie), message);
    }
}
 
Example 3
Source File: ManifestDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void afterCheckFile(@NonNull Context context) {
    XmlContext xmlContext = (XmlContext) context;
    Element element = xmlContext.document.getDocumentElement();
    if (element != null) {
        checkDocumentElement(xmlContext, element);
    }

    if (mSeenUsesSdk == 0 && context.isEnabled(USES_SDK)
            // Not required in Gradle projects; typically defined in build.gradle instead
            // and inserted at build time
            && !context.getMainProject().isGradleProject()) {
        context.report(USES_SDK, Location.create(context.file),
                "Manifest should specify a minimum API level with " +
                "`<uses-sdk android:minSdkVersion=\"?\" />`; if it really supports " +
                "all versions of Android set it to 1.");
    }
}
 
Example 4
Source File: ResourcePrefixDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void beforeCheckFile(@NonNull Context context) {
    if (mPrefix != null && context instanceof XmlContext) {
        XmlContext xmlContext = (XmlContext) context;
        ResourceFolderType folderType = xmlContext.getResourceFolderType();
        if (folderType != null && folderType != ResourceFolderType.VALUES) {
            String name = LintUtils.getBaseName(context.file.getName());
            if (!name.startsWith(mPrefix)) {
                // Attempt to report the error on the root tag of the associated
                // document to make suppressing the error with a tools:suppress
                // attribute etc possible
                if (xmlContext.document != null) {
                    Element root = xmlContext.document.getDocumentElement();
                    if (root != null) {
                        xmlContext.report(ISSUE, root, xmlContext.getLocation(root),
                                getErrorMessage(name));
                        return;
                    }
                }
                context.report(ISSUE, Location.create(context.file),
                        getErrorMessage(name));
            }
        }
    }
}
 
Example 5
Source File: MergeRootFrameLayoutDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mPending != null && mWhitelistedLayouts != null) {
        // Process all the root FrameLayouts that are eligible, and generate
        // suggestions for <merge> replacements for any layouts that are included
        // from other layouts
        for (Pair<String, Handle> pair : mPending) {
            String layout = pair.getFirst();
            if (mWhitelistedLayouts.contains(layout)) {
                Handle handle = pair.getSecond();

                Object clientData = handle.getClientData();
                if (clientData instanceof Node) {
                    if (context.getDriver().isSuppressed(null, ISSUE, (Node) clientData)) {
                        return;
                    }
                }

                Location location = handle.resolve();
                context.report(ISSUE, location,
                        "This `<FrameLayout>` can be replaced with a `<merge>` tag");
            }
        }
    }
}
 
Example 6
Source File: PrivateKeyDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run(@NonNull Context context) {
    if (!context.getProject().getReportIssues()) {
        // If this is a library project not being analyzed, ignore it
        return;
    }

    File file = context.file;
    if (isPrivateKeyFile(file)) {
        String fileName = file.getParentFile().getName() + File.separator
            + file.getName();
        String message = String.format(
            "The `%1$s` file seems to be a private key file. " +
            "Please make sure not to embed this in your APK file.", fileName);
        context.report(ISSUE, Location.create(file), message);
    }
}
 
Example 7
Source File: TranslationDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void reportMap(Context context, Issue issue, Map<String, Location> map) {
    if (map != null) {
        for (Map.Entry<String, Location> entry : map.entrySet()) {
            Location location = entry.getValue();
            String name = entry.getKey();
            String message = mDescriptions.get(name);

            if (location == null) {
                location = Location.create(context.getProject().getDir());
            }

            // We were prepending locations, but we want to prefer the base folders
            location = Location.reverse(location);

            context.report(issue, location, message);
        }
    }
}
 
Example 8
Source File: MinSdkDetector.java    From linette with Apache License 2.0 5 votes vote down vote up
@Override
public void afterCheckProject(Context context) {
    super.afterCheckProject(context);
    int minSdk = context.getProject().getMinSdk();
    if (minSdk != -1 && minSdk < SUGGESTED_MIN_SDK_VERSION) {
        context.report(ISSUE, Location.create(context.file), ISSUE.getBriefDescription(TextFormat.TEXT));
    }
}
 
Example 9
Source File: WrongIdDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static void report(Context context, Issue issue, Handle handle, String message) {
    Location location = handle.resolve();
    Object clientData = handle.getClientData();
    if (clientData instanceof Node) {
        if (context.getDriver().isSuppressed(null, issue, (Node) clientData)) {
            return;
        }
    }

    context.report(issue, location, message);
}
 
Example 10
Source File: PermissionsInLibraryDetector.java    From lewis with Apache License 2.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {

    // if it is a library
    if (context.getProject() == context.getMainProject() && context.getMainProject().isLibrary()) {
        for (Location location : mPermissionTagsLocations) {
            context.report(ISSUE_PERMISSION_USAGE_IN_LIBRARY, location,
                    "Expecting " + ANDROID_MANIFEST_XML + " not to have a <" + TAG_USES_PERMISSION + "> tag invocation");
        }
    }
}
 
Example 11
Source File: PrivateResourceDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void beforeCheckFile(@NonNull Context context) {
    File file = context.file;
    boolean isXmlFile = LintUtils.isXmlFile(file);
    if (!isXmlFile && !LintUtils.isBitmapFile(file)) {
        return;
    }
    String parentName = file.getParentFile().getName();
    int dash = parentName.indexOf('-');
    if (dash != -1 || FD_RES_VALUES.equals(parentName)) {
        return;
    }
    ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
    if (folderType == null) {
        return;
    }
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
    if (types.isEmpty()) {
        return;
    }
    ResourceType type = types.get(0);
    String resourceName = getResourceFieldName(getBaseName(file.getName()));
    if (isPrivate(context, type, resourceName)) {
        String message = createOverrideErrorMessage(context, type, resourceName);
        Location location = Location.create(file);
        context.report(ISSUE, location, message);
    }
}
 
Example 12
Source File: InvalidPackageDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mCandidates == null) {
        return;
    }

    Set<String> seen = Sets.newHashSet();

    for (Candidate candidate : mCandidates) {
        String type = candidate.mClass;
        if (mJavaxLibraryClasses.contains(type)) {
            continue;
        }
        File jarFile = candidate.mJarFile;
        String referencedIn = candidate.mReferencedIn;

        Location location = Location.create(jarFile);
        String pkg = getPackageName(type);
        if (seen.contains(pkg)) {
            continue;
        }
        seen.add(pkg);

        if (pkg.equals("javax.inject")) {
            String name = jarFile.getName();
            //noinspection SpellCheckingInspection
            if (name.startsWith("dagger-") || name.startsWith("guice-")) {
                // White listed
                return;
            }
        }

        String message = String.format(
                "Invalid package reference in library; not included in Android: `%1$s`. " +
                "Referenced from `%2$s`.", pkg, ClassContext.getFqcn(referencedIn));
        context.report(ISSUE, location, message);
    }
}
 
Example 13
Source File: OnClickDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mNames != null && !mNames.isEmpty() && mHaveBytecode) {
        List<String> names = new ArrayList<String>(mNames.keySet());
        Collections.sort(names);
        LintDriver driver = context.getDriver();
        for (String name : names) {
            Handle handle = mNames.get(name);

            Object clientData = handle.getClientData();
            if (clientData instanceof Node) {
                if (driver.isSuppressed(null, ISSUE, (Node) clientData)) {
                    continue;
                }
            }

            Location location = handle.resolve();
            String message = String.format(
                "Corresponding method handler '`public void %1$s(android.view.View)`' not found",
                name);
            List<String> similar = mSimilar != null ? mSimilar.get(name) : null;
            if (similar != null) {
                Collections.sort(similar);
              message += String.format(" (did you mean `%1$s` ?)", Joiner.on(", ").join(similar));
            }
            context.report(ISSUE, location, message);
        }
    }
}
 
Example 14
Source File: LayoutInflationDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mPendingErrors != null) {
        for (Pair<String,Location> pair : mPendingErrors) {
            String inflatedLayout = pair.getFirst();
            if (mLayoutsWithRootLayoutParams == null ||
                    !mLayoutsWithRootLayoutParams.contains(inflatedLayout)) {
                // No root layout parameters on the inflated layout: no need to complain
                continue;
            }
            Location location = pair.getSecond();
            context.report(ISSUE, location, ERROR_MESSAGE);
        }
    }
}
 
Example 15
Source File: SecureRandomGeneratorDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mLocation != null && !mIgnore) {
        String message = "Potentially insecure random numbers on Android 4.3 and older. "
                + "Read " + BLOG_URL + " for more info.";
        context.report(ISSUE, mLocation, message);
    }
}
 
Example 16
Source File: RtlDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mUsesRtlAttributes && mEnabledRtlSupport == null && rtlApplies(context)) {
        List<File> manifestFile = context.getMainProject().getManifestFiles();
        if (!manifestFile.isEmpty()) {
            Location location = Location.create(manifestFile.get(0));
            context.report(ENABLED, location,
                    "The project references RTL attributes, but does not explicitly enable " +
                    "or disable RTL support with `android:supportsRtl` in the manifest");
        }
    }
}
 
Example 17
Source File: AlwaysShowActionDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mAlwaysFields != null && !mHasIfRoomRefs) {
        for (Location location : mAlwaysFields) {
            context.report(ISSUE, location,
                "Prefer \"`SHOW_AS_ACTION_IF_ROOM`\" instead of \"`SHOW_AS_ACTION_ALWAYS`\"");
        }
    }
}
 
Example 18
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mPendingFields != null) {
        for (List<Pair<String, Location>> list : mPendingFields.values()) {
            for (Pair<String, Location> pair : list) {
                String message = pair.getFirst();
                Location location = pair.getSecond();
                context.report(INLINED, location, message);
            }
        }
    }

    super.afterCheckProject(context);
}
 
Example 19
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void beforeCheckProject(@NonNull Context context) {
    mApiDatabase = ApiLookup.get(context.getClient());
    // We can't look up the minimum API required by the project here:
    // The manifest file hasn't been processed yet in the -before- project hook.
    // For now it's initialized lazily in getMinSdk(Context), but the
    // lint infrastructure should be fixed to parse manifest file up front.

    if (mApiDatabase == null && !mWarnedMissingDb) {
        mWarnedMissingDb = true;
        context.report(IssueRegistry.LINT_ERROR, Location.create(context.file),
                    "Can't find API database; API check not performed");
    }
}
 
Example 20
Source File: MissingClassDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (!context.getProject().isLibrary() && mHaveClasses
            && mReferencedClasses != null && !mReferencedClasses.isEmpty()
            && context.getDriver().getScope().contains(Scope.CLASS_FILE)) {
        List<String> classes = new ArrayList<String>(mReferencedClasses.keySet());
        Collections.sort(classes);
        for (String owner : classes) {
            Location.Handle handle = mReferencedClasses.get(owner);
            String fqcn = ClassContext.getFqcn(owner);

            String signature = ClassContext.getInternalName(fqcn);
            if (!signature.equals(owner)) {
                if (!mReferencedClasses.containsKey(signature)) {
                    continue;
                }
            } else if (signature.indexOf('$') != -1) {
                signature = signature.replace('$', '/');
                if (!mReferencedClasses.containsKey(signature)) {
                    continue;
                }
            }
            mReferencedClasses.remove(owner);

            // Ignore usages of platform libraries
            if (owner.startsWith("android/")) { //$NON-NLS-1$
                continue;
            }

            String message = String.format(
                    "Class referenced in the manifest, `%1$s`, was not found in the " +
                            "project or the libraries", fqcn);
            Location location = handle.resolve();
            File parentFile = location.getFile().getParentFile();
            if (parentFile != null) {
                String parent = parentFile.getName();
                ResourceFolderType type = ResourceFolderType.getFolderType(parent);
                if (type == LAYOUT) {
                    message = String.format(
                        "Class referenced in the layout file, `%1$s`, was not found in "
                            + "the project or the libraries", fqcn);
                } else if (type == XML) {
                    message = String.format(
                            "Class referenced in the preference header file, `%1$s`, was not "
                                    + "found in the project or the libraries", fqcn);

                } else if (type == VALUES) {
                    message = String.format(
                            "Class referenced in the analytics file, `%1$s`, was not "
                                    + "found in the project or the libraries", fqcn);
                }
            }

            context.report(MISSING, location, message);
        }
    }
}