com.android.tools.lint.detector.api.Issue Java Examples

The following examples show how to use com.android.tools.lint.detector.api.Issue. 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: LintCliClient.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
@Override
public Severity getSeverity(@NonNull Issue issue) {
    Severity severity = computeSeverity(issue);

    if (mFatalOnly && severity != Severity.FATAL) {
        return Severity.IGNORE;
    }

    if (mFlags.isWarningsAsErrors() && severity == Severity.WARNING) {
        severity = Severity.ERROR;
    }

    if (mFlags.isIgnoreWarnings() && severity == Severity.WARNING) {
        severity = Severity.IGNORE;
    }

    return severity;
}
 
Example #2
Source File: DefaultConfiguration.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
@NonNull
public Severity getSeverity(@NonNull Issue issue) {
    ensureInitialized();

    Severity severity = mSeverity.get(issue.getId());
    if (severity == null) {
        severity = mSeverity.get(VALUE_ALL);
    }

    if (severity != null) {
        return severity;
    }

    if (mParent != null) {
        return mParent.getSeverity(issue);
    }

    return getDefaultSeverity(issue);
}
 
Example #3
Source File: IssueRegistry.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns all available issues of a given scope (regardless of whether
 * they are actually enabled for a given configuration etc)
 *
 * @param scope the applicable scope set
 * @return a list of issues
 */
@NonNull
protected List<Issue> getIssuesForScope(@NonNull EnumSet<Scope> scope) {
    List<Issue> list = sScopeIssues.get(scope);
    if (list == null) {
        List<Issue> issues = getIssues();
        if (scope.equals(Scope.ALL)) {
            list = issues;
        } else {
            list = new ArrayList<Issue>(getIssueCapacity(scope));
            for (Issue issue : issues) {
                // Determine if the scope matches
                if (issue.getImplementation().isAdequate(scope)) {
                    list.add(issue);
                }
            }
        }
        sScopeIssues.put(scope, list);
    }

    return list;
}
 
Example #4
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 #5
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/** Checks whether the given element is the given tag, and if so, whether it satisfied
 * the minimum version that the given tag is supported in */
private void checkElement(@NonNull XmlContext context, @NonNull Element element,
        @NonNull String tag, int api, @NonNull Issue issue) {
    if (tag.equals(element.getTagName())) {
        int minSdk = getMinSdk(context);
        if (api > minSdk && api > context.getFolderVersion()
                && api > getLocalMinSdk(element)) {
            Location location = context.getLocation(element);
            String message;
            if (issue == UNSUPPORTED) {
                message = String.format(
                        "`<%1$s>` requires API level %2$d (current min is %3$d)", tag, api,
                        minSdk);
            } else {
                assert issue == UNUSED : issue;
                message = String.format(
                        "`<%1$s>` is only used in API level %2$d and higher "
                                + "(current min is %3$d)", tag, api, minSdk);
            }
            context.report(issue, element, location, message);
        }
    }
}
 
Example #6
Source File: PluralsDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected TestConfiguration getConfiguration(LintClient client, Project project) {
    return new TestConfiguration(client, project, null) {
        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            return super.isEnabled(issue) && mEnabled.contains(issue);
        }
    };
}
 
Example #7
Source File: AbstractCheckTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected List<Issue> getIssues() {
    List<Issue> issues = new ArrayList<Issue>();
    Class<? extends Detector> detectorClass = getDetectorInstance().getClass();
    // Get the list of issues from the registry and filter out others, to make sure
    // issues are properly registered
    List<Issue> candidates = new BuiltinIssueRegistry().getIssues();
    for (Issue issue : candidates) {
        if (issue.getImplementation().getDetectorClass() == detectorClass) {
            issues.add(issue);
        }
    }

    return issues;
}
 
Example #8
Source File: RtlDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected TestConfiguration getConfiguration(LintClient client, Project project) {
    return new TestConfiguration(client, project, null) {
        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            return super.isEnabled(issue) && mEnabled.contains(issue);
        }
    };
}
 
Example #9
Source File: RootPathTest.java    From lewis with Apache License 2.0 5 votes vote down vote up
@Override
protected TestConfiguration getConfiguration(LintClient client, Project project) {
    return new TestConfiguration(client, project, null) {
        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            return super.isEnabled(issue) && mEnabled.contains(issue);
        }
    };
}
 
Example #10
Source File: MissingClassDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected TestConfiguration getConfiguration(LintClient client, Project project) {
    return new TestConfiguration(client, project, null) {
        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            return super.isEnabled(issue) && mEnabled.contains(issue);
        }
    };
}
 
Example #11
Source File: GridLayoutDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void checkReportedError(@NonNull Context context, @NonNull Issue issue,
        @NonNull Severity severity, @Nullable Location location, @NonNull String message) {
    if (message.contains("with v7 GridLayout")) {
        assertNotNull(message, getOldValue(message, TEXT));
        assertNotNull(message, getNewValue(message, TEXT));
    }
}
 
Example #12
Source File: LewisIssueRegistry.java    From lewis with Apache License 2.0 5 votes vote down vote up
/**
 * Get issues
 *
 * @return the list of new issues to add.
 */
@Override
public List<Issue> getIssues() {
    return Arrays.asList(
            RootPackageDetector.ISSUE_CLASS_IN_ROOT_PACKAGE,
            LauncherActivityDetector.ISSUE_MISSING_LAUNCHER,
            LauncherActivityDetector.ISSUE_MORE_THAN_ONE_LAUNCHER,
            LauncherActivityDetector.ISSUE_LAUNCHER_ACTIVITY_IN_LIBRARY,
            IconInLibraryDetector.ISSUE_ICON_IN_LIBRARY,
            PermissionsInLibraryDetector.ISSUE_PERMISSION_USAGE_IN_LIBRARY,
            JavaVariablesDetector.ISSUE_INSTANCE_VARIABLE_NAME,
            JavaVariablesDetector.ISSUE_CLASS_CONSTANT_NAME,
            HardcodedTextDetectorModified.ISSUE,
            LayoutIdFormat.ISSUE);
}
 
Example #13
Source File: BuiltinIssueRegistryTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public void testUnique() {
    // Check that ids are unique
    Set<String> ids = new HashSet<String>();
    for (Issue issue : new BuiltinIssueRegistry().getIssues()) {
        String id = issue.getId();
        assertTrue("Duplicate id " + id, !ids.contains(id));
        ids.add(id);
    }
}
 
Example #14
Source File: ApiDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void checkReportedError(@NonNull Context context, @NonNull Issue issue,
        @NonNull Severity severity, @Nullable Location location, @NonNull String message) {
    if (issue == UNSUPPORTED || issue == INLINED) {
        int requiredVersion = ApiDetector.getRequiredVersion(issue, message, TEXT);
        assertTrue("Could not extract message tokens from " + message,
                requiredVersion >= 1 && requiredVersion <= SdkVersionInfo.HIGHEST_KNOWN_API);
    }
}
 
Example #15
Source File: MTIssueRegistryTest.java    From MeituanLintDemo with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the Issue Registry contains the correct Issues
 */
@Test
public void testGetIssues() throws Exception {
    List<Issue> actual = mtIssueRegistry.getIssues();
    assertThat(actual).contains(LogDetector.ISSUE);
    assertThat(actual).contains(HashMapForJDK7Detector.ISSUE);
}
 
Example #16
Source File: GradleDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected TestConfiguration getConfiguration(LintClient client, Project project) {
    return new TestConfiguration(client, project, null) {
        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            return super.isEnabled(issue) && (mEnabled == null || mEnabled.contains(issue));
        }
    };
}
 
Example #17
Source File: ManifestDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected TestConfiguration getConfiguration(LintClient client, Project project) {
    return new TestConfiguration(client, project, null) {
        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            return super.isEnabled(issue) && mEnabled.contains(issue);
        }
    };
}
 
Example #18
Source File: LintCliXmlParserTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void report(
        @NonNull Context context,
        @NonNull Issue issue,
        @NonNull Severity severity,
        @Nullable Location location,
        @NonNull String message,
        @NonNull TextFormat format) {
    System.out.println(location + ":" + message);
}
 
Example #19
Source File: BuiltinIssueRegistryTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public void testNoListResize() {
    BuiltinIssueRegistry registry = new BuiltinIssueRegistry();
    List<Issue> issues = registry.getIssues();
    int issueCount = issues.size();
    assertTrue(Integer.toString(issueCount),
            BuiltinIssueRegistry.INITIAL_CAPACITY >= issueCount);
}
 
Example #20
Source File: IconDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected TestConfiguration getConfiguration(LintClient client, Project project) {
    return new TestConfiguration(client, project, null) {
        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            return super.isEnabled(issue) && mEnabled.contains(issue);
        }
    };
}
 
Example #21
Source File: SupportAnnotationDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static void checkResult(@NonNull JavaContext context, @NonNull Node node,
        @NonNull ResolvedAnnotation annotation) {
    if (node.getParent() instanceof ExpressionStatement) {
        String methodName = JavaContext.getMethodName(node);
        Object suggested = annotation.getValue(ATTR_SUGGEST);

        // Failing to check permissions is a potential security issue (and had an existing
        // dedicated issue id before which people may already have configured with a
        // custom severity in their LintOptions etc) so continue to use that issue
        // (which also has category Security rather than Correctness) for these:
        Issue issue = CHECK_RESULT;
        if (methodName != null && methodName.startsWith("check")
                && methodName.contains("Permission")) {
            issue = CHECK_PERMISSION;
        }

        String message = String.format("The result of `%1$s` is not used",
                methodName);
        if (suggested != null) {
            // TODO: Resolve suggest attribute (e.g. prefix annotation class if it starts
            // with "#" etc?
            message = String.format(
                    "The result of `%1$s` is not used; did you mean to call `%2$s`?",
                    methodName, suggested.toString());
        }
        context.report(issue, node, context.getLocation(node), message);
    }
}
 
Example #22
Source File: GradleDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given an error message produced by this lint detector for the given issue type,
 * returns the new value to be put into the source code.
 * <p>
 * Intended for IDE quickfix implementations.
 *
 * @param issue the corresponding issue
 * @param errorMessage the error message associated with the error
 * @param format the format of the error message
 * @return the corresponding new value, or null if not recognized
 */
@Nullable
public static String getNewValue(@NonNull Issue issue, @NonNull String errorMessage,
        @NonNull TextFormat format) {
    errorMessage = format.toText(errorMessage);

    if (issue == DEPENDENCY) {
        // "A newer version of com.google.guava:guava than 11.0.2 is available: 17.0.0"
        if (errorMessage.startsWith("A newer ")) {
            return findSubstring(errorMessage, " is available: ", null);
        }
        if (errorMessage.startsWith("Old buildToolsVersion ")) {
            return findSubstring(errorMessage, " version is ", " ");
        }
        // "The targetSdkVersion (20) should not be higher than the compileSdkVersion (19)"
        return findSubstring(errorMessage, "compileSdkVersion (", ")");
    } else if (issue == STRING_INTEGER) {
        return findSubstring(errorMessage, " just ", ")");
    } else if (issue == DEPRECATED) {
        if (errorMessage.contains(GradleDetector.APP_PLUGIN_ID) &&
                errorMessage.contains(GradleDetector.OLD_APP_PLUGIN_ID)) {
            return GradleDetector.APP_PLUGIN_ID;
        } else if (errorMessage.contains(GradleDetector.LIB_PLUGIN_ID) &&
                errorMessage.contains(GradleDetector.OLD_LIB_PLUGIN_ID)) {
            return GradleDetector.LIB_PLUGIN_ID;
        }
        // "Deprecated: Replace 'packageNameSuffix' with 'applicationIdSuffix'"
        return findSubstring(errorMessage, " with '", "'");
    } else if (issue == COMPATIBILITY) {
        if (errorMessage.startsWith("Version 5.2.08")) {
            return findSubstring(errorMessage, "Use version ", " ");
        }
    }

    return null;
}
 
Example #23
Source File: Main.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static void describeIssue(Issue issue) {
    System.out.println(issue.getId());
    for (int i = 0; i < issue.getId().length(); i++) {
        System.out.print('-');
    }
    System.out.println();
    System.out.println(wrap("Summary: " + issue.getBriefDescription(TEXT)));
    System.out.println("Priority: " + issue.getPriority() + " / 10");
    System.out.println("Severity: " + issue.getDefaultSeverity().getDescription());
    System.out.println("Category: " + issue.getCategory().getFullName());

    if (!issue.isEnabledByDefault()) {
        System.out.println("NOTE: This issue is disabled by default!");
        System.out.println(String.format("You can enable it by adding %1$s %2$s", ARG_ENABLE,
                issue.getId()));
    }

    System.out.println();
    System.out.println(wrap(issue.getExplanation(TEXT)));
    List<String> moreInfo = issue.getMoreInfo();
    if (!moreInfo.isEmpty()) {
        System.out.println("More information: ");
        for (String uri : moreInfo) {
            System.out.println(uri);
        }
    }
}
 
Example #24
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public static int getRequiredVersion(@NonNull Issue issue, @NonNull String errorMessage,
        @NonNull TextFormat format) {
    errorMessage = format.toText(errorMessage);

    if (issue == UNSUPPORTED || issue == INLINED) {
        Pattern pattern = Pattern.compile("\\s(\\d+)\\s"); //$NON-NLS-1$
        Matcher matcher = pattern.matcher(errorMessage);
        if (matcher.find()) {
            return Integer.parseInt(matcher.group(1));
        }
    }

    return -1;
}
 
Example #25
Source File: CompositeIssueRegistry.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public List<Issue> getIssues() {
    if (myIssues == null) {
        List<Issue> issues = Lists.newArrayListWithExpectedSize(200);
        for (IssueRegistry registry : myRegistries) {
            issues.addAll(registry.getIssues());
        }
        myIssues = issues;
    }

    return myIssues;
}
 
Example #26
Source File: DefaultConfiguration.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void ignore(
        @NonNull Context context,
        @NonNull Issue issue,
        @Nullable Location location,
        @NonNull String message) {
    // This configuration only supports suppressing warnings on a per-file basis
    if (location != null) {
        ignore(issue, location.getFile());
    }
}
 
Example #27
Source File: DefaultConfiguration.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void formatError(String message, Object... args) {
    if (args != null && args.length > 0) {
        message = String.format(message, args);
    }
    message = "Failed to parse `lint.xml` configuration file: " + message;
    LintDriver driver = new LintDriver(new IssueRegistry() {
        @Override @NonNull public List<Issue> getIssues() {
            return Collections.emptyList();
        }
    }, mClient);
    mClient.report(new Context(driver, mProject, mProject, mConfigFile),
            IssueRegistry.LINT_ERROR,
            mProject.getConfiguration(driver).getSeverity(IssueRegistry.LINT_ERROR),
            Location.create(mConfigFile), message, TextFormat.RAW);
}
 
Example #28
Source File: DefaultConfiguration.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
protected Severity getDefaultSeverity(@NonNull Issue issue) {
    if (!issue.isEnabledByDefault()) {
        return Severity.IGNORE;
    }

    return issue.getDefaultSeverity();
}
 
Example #29
Source File: LintDriver.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns whether the given issue is suppressed in the given XML DOM node.
 *
 * @param issue the issue to be checked, or null to just check for "all"
 * @param node the DOM node containing the issue
 * @return true if there is a suppress annotation covering the specific
 *         issue in this class
 */
public boolean isSuppressed(@Nullable XmlContext context, @NonNull Issue issue,
        @Nullable org.w3c.dom.Node node) {
    if (node instanceof Attr) {
        node = ((Attr) node).getOwnerElement();
    }
    boolean checkComments = mClient.checkForSuppressComments()
            && context != null && context.containsCommentSuppress();
    while (node != null) {
        if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.hasAttributeNS(TOOLS_URI, ATTR_IGNORE)) {
                String ignore = element.getAttributeNS(TOOLS_URI, ATTR_IGNORE);
                if (ignore.indexOf(',') == -1) {
                    if (matches(issue, ignore)) {
                        return true;
                    }
                } else {
                    for (String id : ignore.split(",")) { //$NON-NLS-1$
                        if (matches(issue, id)) {
                            return true;
                        }
                    }
                }
            } else if (checkComments && context.isSuppressedWithComment(node, issue)) {
                return true;
            }
        }

        node = node.getParentNode();
    }

    return false;
}
 
Example #30
Source File: PermissionsDispatcherIssueRegistry.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public List<Issue> getIssues() {
    return Arrays.asList(
            CallNeedsPermissionDetector.ISSUE,
            CallOnRequestPermissionsResultDetector.ISSUE,
            NoCorrespondingNeedsPermissionDetector.ISSUE,
            NoDelegateOnResumeDetector.ISSUE);
}