Java Code Examples for com.android.tools.lint.detector.api.Severity#WARNING

The following examples show how to use com.android.tools.lint.detector.api.Severity#WARNING . 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: LintDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
@NonNull
protected Severity getDefaultSeverity(@NonNull Issue issue) {
    // In unit tests, include issues that are ignored by default
    Severity severity = super.getDefaultSeverity(issue);
    if (severity == Severity.IGNORE) {
        if (issue.getDefaultSeverity() != Severity.IGNORE) {
            return issue.getDefaultSeverity();
        }
        return Severity.WARNING;
    }
    return severity;
}
 
Example 3
Source File: LintCliClient.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private Severity computeSeverity(@NonNull Issue issue) {
    Severity severity = super.getSeverity(issue);

    String id = issue.getId();
    Set<String> suppress = mFlags.getSuppressedIds();
    if (suppress.contains(id)) {
        return Severity.IGNORE;
    }

    Severity manual = mFlags.getSeverityOverrides().get(id);
    if (manual != null) {
        return manual;
    }

    Set<String> enabled = mFlags.getEnabledIds();
    Set<String> check = mFlags.getExactCheckedIds();
    if (enabled.contains(id) || (check != null && check.contains(id))) {
        // Overriding default
        // Detectors shouldn't be returning ignore as a default severity,
        // but in case they do, force it up to warning here to ensure that
        // it's run
        if (severity == Severity.IGNORE) {
            severity = issue.getDefaultSeverity();
            if (severity == Severity.IGNORE) {
                severity = Severity.WARNING;
            }
        }

        return severity;
    }

    if (check != null && issue != LINT_ERROR && issue != PARSER_ERROR) {
        return Severity.IGNORE;
    }

    return severity;
}
 
Example 4
Source File: XmlReporterTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void test() throws Exception {
    File file = new File(getTargetDir(), "report");
    try {
        LintCliClient client = new LintCliClient() {
            @Override
            String getRevision() {
                return "unittest"; // Hardcode version to keep unit test output stable
            }
        };
        //noinspection ResultOfMethodCallIgnored
        file.getParentFile().mkdirs();
        XmlReporter reporter = new XmlReporter(client, file);
        Project project = Project.create(client, new File("/foo/bar/Foo"),
                new File("/foo/bar/Foo"));

        Warning warning1 = new Warning(ManifestDetector.USES_SDK,
                "<uses-sdk> tag should specify a target API level (the highest verified " +
                "version; when running on later versions, compatibility behaviors may " +
                "be enabled) with android:targetSdkVersion=\"?\"",
                Severity.WARNING, project);
        warning1.line = 6;
        warning1.file = new File("/foo/bar/Foo/AndroidManifest.xml");
        warning1.errorLine = "    <uses-sdk android:minSdkVersion=\"8\" />\n    ^\n";
        warning1.path = "AndroidManifest.xml";
        warning1.location = Location.create(warning1.file,
                new DefaultPosition(6, 4, 198), new DefaultPosition(6, 42, 236));

        Warning warning2 = new Warning(HardcodedValuesDetector.ISSUE,
                "[I18N] Hardcoded string \"Fooo\", should use @string resource",
                Severity.WARNING, project);
        warning2.line = 11;
        warning2.file = new File("/foo/bar/Foo/res/layout/main.xml");
        warning2.errorLine = "        android:text=\"Fooo\" />\n" +
                      "        ~~~~~~~~~~~~~~~~~~~\n";
        warning2.path = "res/layout/main.xml";
        warning2.location = Location.create(warning2.file,
                new DefaultPosition(11, 8, 377), new DefaultPosition(11, 27, 396));

        List<Warning> warnings = new ArrayList<Warning>();
        warnings.add(warning1);
        warnings.add(warning2);

        reporter.write(0, 2, warnings);

        String report = Files.toString(file, Charsets.UTF_8);
        assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<issues format=\"4\" by=\"lint unittest\">\n" +
            "\n" +
            "    <issue\n" +
            "        id=\"UsesMinSdkAttributes\"\n" +
            "        severity=\"Warning\"\n" +
            "        message=\"&lt;uses-sdk> tag should specify a target API level (the highest verified version; when running on later versions, compatibility behaviors may be enabled) with android:targetSdkVersion=&quot;?&quot;\"\n" +
            "        category=\"Correctness\"\n" +
            "        priority=\"9\"\n" +
            "        summary=\"Minimum SDK and target SDK attributes not defined\"\n" +
            "        explanation=\"The manifest should contain a `&lt;uses-sdk>` element which defines the minimum API Level required for the application to run, as well as the target version (the highest API level you have tested the version for.)\"\n" +
            "        url=\"http://developer.android.com/guide/topics/manifest/uses-sdk-element.html\"\n" +
            "        urls=\"http://developer.android.com/guide/topics/manifest/uses-sdk-element.html\"\n" +
            "        errorLine1=\"    &lt;uses-sdk android:minSdkVersion=&quot;8&quot; />\"\n" +
            "        errorLine2=\"    ^\">\n" +
            "        <location\n" +
            "            file=\"AndroidManifest.xml\"\n" +
            "            line=\"7\"\n" +
            "            column=\"5\"/>\n" +
            "    </issue>\n" +
            "\n" +
            "    <issue\n" +
            "        id=\"HardcodedText\"\n" +
            "        severity=\"Warning\"\n" +
            "        message=\"[I18N] Hardcoded string &quot;Fooo&quot;, should use @string resource\"\n" +
            "        category=\"Internationalization\"\n" +
            "        priority=\"5\"\n" +
            "        summary=\"Hardcoded text\"\n" +
            "        explanation=\"Hardcoding text attributes directly in layout files is bad for several reasons:\n" +
            "\n" +
            "* When creating configuration variations (for example for landscape or portrait)you have to repeat the actual text (and keep it up to date when making changes)\n" +
            "\n" +
            "* The application cannot be translated to other languages by just adding new translations for existing string resources.\n" +
            "\n" +
            "In Android Studio and Eclipse there are quickfixes to automatically extract this hardcoded string into a resource lookup.\"\n" +
            "        errorLine1=\"        android:text=&quot;Fooo&quot; />\"\n" +
            "        errorLine2=\"        ~~~~~~~~~~~~~~~~~~~\">\n" +
            "        <location\n" +
            "            file=\"res/layout/main.xml\"\n" +
            "            line=\"12\"\n" +
            "            column=\"9\"/>\n" +
            "    </issue>\n" +
            "\n" +
            "</issues>\n",
            report);

        // Make sure the XML is valid
        Document document = PositionXmlParser.parse(report);
        assertNotNull(document);
        assertEquals(2, document.getElementsByTagName("issue").getLength());
    } finally {
        //noinspection ResultOfMethodCallIgnored
        file.delete();
    }
}
 
Example 5
Source File: XmlReporterTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void testNonPrintableChars() throws Exception {
    // See https://code.google.com/p/android/issues/detail?id=56205
    File file = new File(getTargetDir(), "report");
    try {
        LintCliClient client = new LintCliClient() {
            @Override
            String getRevision() {
                return "unittest"; // Hardcode version to keep unit test output stable
            }
        };
        //noinspection ResultOfMethodCallIgnored
        file.getParentFile().mkdirs();
        XmlReporter reporter = new XmlReporter(client, file);
        Project project = Project.create(client, new File("/foo/bar/Foo"),
                new File("/foo/bar/Foo"));

        Warning warning1 = new Warning(TypographyDetector.FRACTIONS,
                String.format("Use fraction character %1$c (%2$s) instead of %3$s ?",
                        '\u00BC', "&#188;", "1/4"), Severity.WARNING, project);
        warning1.line = 592;
        warning1.file = new File("/foo/bar/Foo/AndroidManifest.xml");
        warning1.errorLine =
                "        <string name=\"user_registration_name3_3\">Register 3/3</string>\n" +
                "                                             ^";
        warning1.path = "res/values-en/common_strings.xml";
        warning1.location = Location.create(warning1.file,
                new DefaultPosition(592, 46, -1), null);

        List<Warning> warnings = new ArrayList<Warning>();
        warnings.add(warning1);

        reporter.write(0, 2, warnings);

        String report = Files.toString(file, Charsets.UTF_8);
        assertEquals(""
                + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<issues format=\"4\" by=\"lint unittest\">\n"
                + "\n"
                + "    <issue\n"
                + "        id=\"TypographyFractions\"\n"
                + "        severity=\"Warning\"\n"
                + "        message=\"Use fraction character ¼ (&amp;#188;) instead of 1/4 ?\"\n"
                + "        category=\"Usability:Typography\"\n"
                + "        priority=\"5\"\n"
                + "        summary=\"Fraction string can be replaced with fraction character\"\n"
                + "        explanation=\"You can replace certain strings, such as 1/2, and 1/4, with dedicated characters for these, such as ½ (&amp;#189;) and ¼ (&amp;#188;). This can help make the text more readable.\"\n"
                + "        url=\"http://en.wikipedia.org/wiki/Number_Forms\"\n"
                + "        urls=\"http://en.wikipedia.org/wiki/Number_Forms\">\n"
                + "        <location\n"
                + "            file=\"AndroidManifest.xml\"\n"
                + "            line=\"593\"\n"
                + "            column=\"47\"/>\n"
                + "    </issue>\n"
                + "\n"
                + "</issues>\n",
                report);

        // Make sure the XML is valid
        Document document = PositionXmlParser.parse(report);
        assertNotNull(document);
        assertEquals(1, document.getElementsByTagName("issue").getLength());
        String explanation =  ((Element)document.getElementsByTagName("issue").item(0)).
                getAttribute("explanation");
        assertEquals(TypographyDetector.FRACTIONS.getExplanation(TextFormat.RAW),
                explanation);
    } finally {
        //noinspection ResultOfMethodCallIgnored
        file.delete();
    }
}
 
Example 6
Source File: TextReporterTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void test() throws Exception {
    File file = new File(getTargetDir(), "report");
    try {
        LintCliClient client = new LintCliClient() {
            @Override
            String getRevision() {
                return "unittest"; // Hardcode version to keep unit test output stable
            }
        };
        //noinspection ResultOfMethodCallIgnored
        file.getParentFile().mkdirs();
        FileWriter writer = new FileWriter(file);
        TextReporter reporter = new TextReporter(client, client.mFlags, file, writer, true);
        Project project = Project.create(client, new File("/foo/bar/Foo"),
                new File("/foo/bar/Foo"));
        client.mFlags.setShowEverything(true);

        Warning warning1 = new Warning(ManifestDetector.USES_SDK,
                "<uses-sdk> tag should specify a target API level (the highest verified " +
                        "version; when running on later versions, compatibility behaviors may " +
                        "be enabled) with android:targetSdkVersion=\"?\"",
                Severity.WARNING, project);
        warning1.line = 6;
        warning1.file = new File("/foo/bar/Foo/AndroidManifest.xml");
        warning1.errorLine = "    <uses-sdk android:minSdkVersion=\"8\" />\n    ^\n";
        warning1.path = "AndroidManifest.xml";
        warning1.location = Location.create(warning1.file,
                new DefaultPosition(6, 4, 198), new DefaultPosition(6, 42, 236));
        Location secondary = Location.create(warning1.file,
                new DefaultPosition(7, 4, 198), new DefaultPosition(7, 42, 236));
        secondary.setMessage("Secondary location");
        warning1.location.setSecondary(secondary);

        Warning warning2 = new Warning(HardcodedValuesDetector.ISSUE,
                "[I18N] Hardcoded string \"Fooo\", should use @string resource",
                Severity.WARNING, project);
        warning2.line = 11;
        warning2.file = new File("/foo/bar/Foo/res/layout/main.xml");
        warning2.errorLine = "        android:text=\"Fooo\" />\n" +
                "        ~~~~~~~~~~~~~~~~~~~\n";
        warning2.path = "res/layout/main.xml";
        warning2.location = Location.create(warning2.file,
                new DefaultPosition(11, 8, 377), new DefaultPosition(11, 27, 396));
        secondary = Location.create(warning1.file,
                new DefaultPosition(7, 4, 198), new DefaultPosition(7, 42, 236));
        secondary.setMessage("Secondary location");
        warning2.location.setSecondary(secondary);
        Location tertiary = Location.create(warning2.file,
                new DefaultPosition(5, 4, 198), new DefaultPosition(5, 42, 236));
        secondary.setSecondary(tertiary);

        List<Warning> warnings = new ArrayList<Warning>();
        warnings.add(warning1);
        warnings.add(warning2);
        Collections.sort(warnings);

        reporter.write(0, 2, warnings);

        String report = Files.toString(file, Charsets.UTF_8);
        assertEquals(""
                + "AndroidManifest.xml:7: Warning: <uses-sdk> tag should specify a target API level (the highest verified version; when running on later versions, compatibility behaviors may be enabled) with android:targetSdkVersion=\"?\" [UsesMinSdkAttributes]\n"
                + "    <uses-sdk android:minSdkVersion=\"8\" />\n"
                + "    ^\n"
                + "    AndroidManifest.xml:8: Secondary location\n"
                + "res/layout/main.xml:12: Warning: [I18N] Hardcoded string \"Fooo\", should use @string resource [HardcodedText]\n"
                + "        android:text=\"Fooo\" />\n"
                + "        ~~~~~~~~~~~~~~~~~~~\n"
                + "    AndroidManifest.xml:8: Secondary location\n"
                + "Also affects: res/layout/main.xml:6\n"
                + "0 errors, 2 warnings\n",
                report);
    } finally {
        //noinspection ResultOfMethodCallIgnored
        file.delete();
    }
}
 
Example 7
Source File: HtmlReporter.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private void writeIssueMetadata(Issue issue, Severity severity, String disabledBy)
        throws IOException {
    mWriter.write("<div class=\"metadata\">");               //$NON-NLS-1$

    if (mClient.getRegistry() instanceof BuiltinIssueRegistry) {
        boolean adtHasFix = QuickfixHandler.ADT.hasAutoFix(issue);
        boolean studioHasFix = QuickfixHandler.STUDIO.hasAutoFix(issue);
        if (adtHasFix || studioHasFix) {
            String adt = "Eclipse/ADT";
            String studio = "Android Studio/IntelliJ";
            String tools = adtHasFix && studioHasFix
                    ? (adt + " & " + studio) : studioHasFix ? studio : adt;
            mWriter.write("Note: This issue has an associated quickfix operation in " + tools);
            if (mFixUrl != null) {
                mWriter.write("&nbsp;<img alt=\"Fix\" border=\"0\" align=\"top\" src=\""); //$NON-NLS-1$
                mWriter.write(mFixUrl);
                mWriter.write("\" />\n");                            //$NON-NLS-1$
            }

            mWriter.write("<br>\n");
        }
    }

    if (disabledBy != null) {
        mWriter.write(String.format("Disabled By: %1$s<br/>\n", disabledBy));
    }

    mWriter.write("Priority: ");
    mWriter.write(String.format("%1$d / 10", issue.getPriority()));
    mWriter.write("<br/>\n");                                //$NON-NLS-1$
    mWriter.write("Category: ");
    mWriter.write(issue.getCategory().getFullName());
    mWriter.write("</div>\n");                               //$NON-NLS-1$

    mWriter.write("Severity: ");
    if (severity == Severity.ERROR || severity == Severity.FATAL) {
        mWriter.write("<span class=\"error\">");             //$NON-NLS-1$
    } else if (severity == Severity.WARNING) {
        mWriter.write("<span class=\"warning\">");           //$NON-NLS-1$
    } else {
        mWriter.write("<span>");                             //$NON-NLS-1$
    }
    appendEscapedText(severity.getDescription());
    mWriter.write("</span>");                                //$NON-NLS-1$

    mWriter.write("<div class=\"summary\">\n");              //$NON-NLS-1$
    mWriter.write("Explanation: ");
    String description = issue.getBriefDescription(HTML);
    mWriter.write(description);
    if (!description.isEmpty()
            && Character.isLetter(description.charAt(description.length() - 1))) {
        mWriter.write('.');
    }
    mWriter.write("</div>\n");                               //$NON-NLS-1$
    mWriter.write("<div class=\"explanation\">\n");          //$NON-NLS-1$
    String explanationHtml = issue.getExplanation(HTML);
    mWriter.write(explanationHtml);
    mWriter.write("\n</div>\n");                             //$NON-NLS-1$;
    List<String> moreInfo = issue.getMoreInfo();
    mWriter.write("<br/>");                                  //$NON-NLS-1$
    mWriter.write("<div class=\"moreinfo\">");               //$NON-NLS-1$
    mWriter.write("More info: ");
    int count = moreInfo.size();
    if (count > 1) {
        mWriter.write("<ul>");                               //$NON-NLS-1$
    }
    for (String uri : moreInfo) {
        if (count > 1) {
            mWriter.write("<li>");                           //$NON-NLS-1$
        }
        mWriter.write("<a href=\"");                         //$NON-NLS-1$
        mWriter.write(uri);
        mWriter.write("\">"    );                            //$NON-NLS-1$
        mWriter.write(uri);
        mWriter.write("</a>\n");                             //$NON-NLS-1$
    }
    if (count > 1) {
        mWriter.write("</ul>");                              //$NON-NLS-1$
    }
    mWriter.write("</div>");                                 //$NON-NLS-1$

    mWriter.write("<br/>");                                  //$NON-NLS-1$
    mWriter.write(String.format(
            "To suppress this error, use the issue id \"%1$s\" as explained in the " +
            "%2$sSuppressing Warnings and Errors%3$s section.",
            issue.getId(),
            "<a href=\"#SuppressInfo\">", "</a>"));          //$NON-NLS-1$ //$NON-NLS-2$
    mWriter.write("<br/>\n");
}