diff options
author | Tor Norbye <tnorbye@google.com> | 2012-03-06 17:50:42 -0800 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-03-06 17:50:42 -0800 |
commit | 9a654c3e45ec87e29d0644ab6fb3c830fa73e5ba (patch) | |
tree | b144195ee457671ec3b9d077b11bc9a4d530c462 /lint/cli | |
parent | cd3617b0c4d32a23f0dbe71cfaed40fdba5f9865 (diff) | |
download | sdk-9a654c3e45ec87e29d0644ab6fb3c830fa73e5ba.zip sdk-9a654c3e45ec87e29d0644ab6fb3c830fa73e5ba.tar.gz sdk-9a654c3e45ec87e29d0644ab6fb3c830fa73e5ba.tar.bz2 |
Misc Lint Fixes
This changeset contains fixes for several unrelated reported lint
bugs:
26505: Default disabled rules that are explicitly enabled do not
activate
26467: Lint says ByteBuffer.array is API 9+ but it's really API 1+
26501: Warning "This tag and its children can be replaced by one
<TextView/> and a compound drawable" is not always correct
(Partially fixed)
It also fixes the following bugs:
- The quickfix for the typography detector did not work for the
fraction warning (replacing 1/2 with the half unicode symbol etc)
- A couple of XML detectors did not check for SuppressLint on the
associated root node
- Add a --exitcode flag, and only set the exit code to non-zero if
that flag is specified. Also fix the code such that non-fatal errors
also contribute to the exit code.
- Make the HTML reporter classes public to help Maven integration.
Change-Id: I60f5fdcb2a465d51fa58bb918a195b373096d54b
Diffstat (limited to 'lint/cli')
6 files changed, 105 insertions, 38 deletions
diff --git a/lint/cli/src/com/android/tools/lint/HtmlReporter.java b/lint/cli/src/com/android/tools/lint/HtmlReporter.java index 9cc9c6f..8b3e99e 100644 --- a/lint/cli/src/com/android/tools/lint/HtmlReporter.java +++ b/lint/cli/src/com/android/tools/lint/HtmlReporter.java @@ -28,6 +28,7 @@ import com.android.tools.lint.detector.api.Location; import com.android.tools.lint.detector.api.Position; import com.android.tools.lint.detector.api.Project; import com.android.tools.lint.detector.api.Severity; +import com.google.common.annotations.Beta; import com.google.common.base.Charsets; import com.google.common.collect.Maps; import com.google.common.io.ByteStreams; @@ -51,8 +52,12 @@ import java.util.Set; /** * A reporter which emits lint results into an HTML report. + * <p> + * <b>NOTE: This is not a public or final API; if you rely on this be prepared + * to adjust your code for the next tools release.</b> */ -class HtmlReporter extends Reporter { +@Beta +public class HtmlReporter extends Reporter { private static boolean USE_HOLO_STYLE = true; private static final String CSS = USE_HOLO_STYLE ? "hololike.css" : "default.css"; //$NON-NLS-1$ //$NON-NLS-2$ @@ -72,13 +77,20 @@ class HtmlReporter extends Reporter { private String mStripPrefix; private String mFixUrl; - HtmlReporter(Main client, File output) throws IOException { + /** + * Creates a new {@link HtmlReporter} + * + * @param client the associated client + * @param output the output file + * @throws IOException if an error occurs + */ + public HtmlReporter(Main client, File output) throws IOException { super(client, output); mWriter = new BufferedWriter(new FileWriter(output)); } @Override - void write(int errorCount, int warningCount, List<Warning> issues) throws IOException { + public void write(int errorCount, int warningCount, List<Warning> issues) throws IOException { Map<Issue, String> missing = computeMissingIssues(issues); mWriter.write( @@ -406,7 +418,7 @@ class HtmlReporter extends Reporter { continue; } - if (!issue.isEnabledByDefault()) { + if (!issue.isEnabledByDefault() && !mClient.isAllEnabled()) { map.put(issue, "Default"); continue; } diff --git a/lint/cli/src/com/android/tools/lint/Main.java b/lint/cli/src/com/android/tools/lint/Main.java index 1f2191b..6a40cb3 100644 --- a/lint/cli/src/com/android/tools/lint/Main.java +++ b/lint/cli/src/com/android/tools/lint/Main.java @@ -78,6 +78,7 @@ public class Main extends LintClient { private static final String ARG_CONFIG = "--config"; //$NON-NLS-1$ private static final String ARG_URL = "--url"; //$NON-NLS-1$ private static final String ARG_VERSION = "--version"; //$NON-NLS-1$ + private static final String ARG_EXITCODE = "--exitcode"; //$NON-NLS-1$ private static final String ARG_NOWARN2 = "--nowarn"; //$NON-NLS-1$ // GCC style flag names for options @@ -100,7 +101,8 @@ public class Main extends LintClient { private Set<String> mEnabled = new HashSet<String>(); /** If non-null, only run the specified checks (possibly modified by enable/disables) */ private Set<String> mCheck = null; - private boolean mFatal; + private boolean mHasErrors; + private boolean mSetExitCode; private boolean mFullPath; private int mErrorCount; private int mWarningCount; @@ -228,6 +230,8 @@ public class Main extends LintClient { mQuiet = true; } else if (arg.equals(ARG_NOLINES)) { mShowLines = false; + } else if (arg.equals(ARG_EXITCODE)) { + mSetExitCode = true; } else if (arg.equals(ARG_VERSION)) { printVersion(); System.exit(0); @@ -489,7 +493,7 @@ public class Main extends LintClient { System.exit(ERRNO_INVALIDARGS); } - System.exit(mFatal ? ERRNO_ERRORS : 0); + System.exit(mSetExitCode ? (mHasErrors ? ERRNO_ERRORS : 0) : 0); } /** @@ -805,8 +809,10 @@ public class Main extends LintClient { ARG_HELP + " <topic>", "Help on the given topic, such as \"suppress\".", ARG_LISTIDS, "List the available issue id's and exit.", ARG_VERSION, "Output version information and exit.", + ARG_EXITCODE, "Set the exit code to " + ERRNO_ERRORS + " if errors are found.", ARG_SHOW, "List available issues along with full explanations.", ARG_SHOW + " <ids>", "Show full explanations for the given list of issue id's.", + "", "\nEnabled Checks:", ARG_DISABLE + " <list>", "Disable the list of categories or " + "specific issue id's. The list should be a comma-separated list of issue " + @@ -823,7 +829,9 @@ public class Main extends LintClient { ARG_CONFIG + " <filename>", "Use the given configuration file to " + "determine whether issues are enabled or disabled. If a project contains " + "a lint.xml file, then this config file will be used as a fallback.", - "", "\nOutput Options:", + + + "", "\nOutput Options:", ARG_QUIET, "Don't show progress.", ARG_FULLPATH, "Use full paths in the error output.", ARG_SHOWALL, "Do not truncate long messages, lists of alternate locations, etc.", @@ -840,15 +848,14 @@ public class Main extends LintClient { "to files, use " + ARG_URL + " " + VALUE_NONE, ARG_SIMPLEHTML + " <filename>", "Create a simple HTML report", ARG_XML + " <filename>", "Create an XML report instead.", - }); - out.println("\nExit Status:"); - printUsage(out, new String[] { - Integer.toString(ERRNO_ERRORS), "Lint errors detected.", - Integer.toString(ERRNO_USAGE), "Lint usage.", - Integer.toString(ERRNO_EXISTS), "Cannot clobber existing file.", - Integer.toString(ERRNO_HELP), "Lint help.", - Integer.toString(ERRNO_INVALIDARGS), "Invalid command-line argument.", + "", "\nExit Status:", + "0", "Success.", + Integer.toString(ERRNO_ERRORS), "Lint errors detected.", + Integer.toString(ERRNO_USAGE), "Lint usage.", + Integer.toString(ERRNO_EXISTS), "Cannot clobber existing file.", + Integer.toString(ERRNO_HELP), "Lint help.", + Integer.toString(ERRNO_INVALIDARGS), "Invalid command-line argument.", }); } @@ -936,12 +943,12 @@ public class Main extends LintClient { } if (severity == Severity.FATAL) { - mFatal = true; // From here on, treat the fatal error as an error such that we don't display // both "Fatal:" and "Error:" etc in the error output. severity = Severity.ERROR; } if (severity == Severity.ERROR) { + mHasErrors = true; mErrorCount++; } else { mWarningCount++; @@ -1096,11 +1103,14 @@ public class Main extends LintClient { // 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()) { - return Severity.WARNING; - } else { - return severity; + if (severity == Severity.IGNORE) { + severity = issue.getDefaultSeverity(); + if (severity == Severity.IGNORE) { + severity = Severity.WARNING; + } } + + return severity; } if (mCheck != null && issue != LINT_ERROR && issue != PARSER_ERROR) { @@ -1162,6 +1172,11 @@ public class Main extends LintClient { return path; } + /** Returns whether all warnings are enabled, including those disabled by default */ + boolean isAllEnabled() { + return mWarnAll; + } + /** Returns the issue registry used by this client */ IssueRegistry getRegistry() { return mRegistry; diff --git a/lint/cli/src/com/android/tools/lint/MultiProjectHtmlReporter.java b/lint/cli/src/com/android/tools/lint/MultiProjectHtmlReporter.java index db7b4fb..d039edc 100644 --- a/lint/cli/src/com/android/tools/lint/MultiProjectHtmlReporter.java +++ b/lint/cli/src/com/android/tools/lint/MultiProjectHtmlReporter.java @@ -37,17 +37,17 @@ import java.util.Set; * "Multiplexing" reporter which allows output to be split up into a separate * report for each separate project. It also adds an overview index. */ -class MultiProjectHtmlReporter extends HtmlReporter { +public class MultiProjectHtmlReporter extends HtmlReporter { private static final String INDEX_NAME = "index.html"; //$NON-NLS-1$ private final File mDir; - MultiProjectHtmlReporter(Main client, File dir) throws IOException { + public MultiProjectHtmlReporter(Main client, File dir) throws IOException { super(client, new File(dir, INDEX_NAME)); mDir = dir; } @Override - void write(int errorCount, int warningCount, List<Warning> allIssues) throws IOException { + public void write(int errorCount, int warningCount, List<Warning> allIssues) throws IOException { Map<Project, List<Warning>> projectToWarnings = new HashMap<Project, List<Warning>>(); for (Warning warning : allIssues) { List<Warning> list = projectToWarnings.get(warning.project); diff --git a/lint/cli/src/com/android/tools/lint/Reporter.java b/lint/cli/src/com/android/tools/lint/Reporter.java index bee0eb4..2c86635 100644 --- a/lint/cli/src/com/android/tools/lint/Reporter.java +++ b/lint/cli/src/com/android/tools/lint/Reporter.java @@ -20,6 +20,7 @@ import static com.android.tools.lint.detector.api.LintConstants.DOT_9PNG; import static com.android.tools.lint.detector.api.LintConstants.DOT_PNG; import static com.android.tools.lint.detector.api.LintUtils.endsWith; +import com.google.common.annotations.Beta; import com.google.common.io.ByteStreams; import com.google.common.io.Closeables; import com.google.common.io.Files; @@ -35,8 +36,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -/** A reporter is an output generator for lint warnings */ -abstract class Reporter { +/** A reporter is an output generator for lint warnings + * <p> + * <b>NOTE: This is not a public or final API; if you rely on this be prepared + * to adjust your code for the next tools release.</b> + */ +@Beta +public abstract class Reporter { protected final Main mClient; protected final File mOutput; protected String mTitle = "Lint Report"; @@ -47,7 +53,16 @@ abstract class Reporter { protected Map<File, String> mResourceUrl = new HashMap<File, String>(); protected Map<String, File> mNameToFile = new HashMap<String, File>(); - abstract void write(int errorCount, int warningCount, List<Warning> issues) throws IOException; + /** + * Write the given warnings into the report + * + * @param errorCount the number of errors + * @param warningCount the number of warnings + * @param issues the issues to be reported + * @throws IOException if an error occurs + */ + public abstract void write(int errorCount, int warningCount, List<Warning> issues) + throws IOException; protected Reporter(Main client, File output) { mClient = client; @@ -59,12 +74,12 @@ abstract class Reporter { * * @param title the title of the report */ - void setTitle(String title) { + public void setTitle(String title) { mTitle = title; } /** @return the title of the report */ - String getTitle() { + public String getTitle() { return mTitle; } @@ -75,7 +90,7 @@ abstract class Reporter { * @param bundleResources if true, copy images into a directory relative to * the report */ - void setBundleResources(boolean bundleResources) { + public void setBundleResources(boolean bundleResources) { mBundleResources = bundleResources; mSimpleFormat = false; } @@ -86,7 +101,7 @@ abstract class Reporter { * * @param simpleFormat whether the formatting should be simple */ - void setSimpleFormat(boolean simpleFormat) { + public void setSimpleFormat(boolean simpleFormat) { mSimpleFormat = simpleFormat; } @@ -96,7 +111,7 @@ abstract class Reporter { * * @return whether the report should use simple formatting */ - boolean isSimpleFormat() { + public boolean isSimpleFormat() { return mSimpleFormat; } diff --git a/lint/cli/src/com/android/tools/lint/TextReporter.java b/lint/cli/src/com/android/tools/lint/TextReporter.java index 47202d1..63ee857 100644 --- a/lint/cli/src/com/android/tools/lint/TextReporter.java +++ b/lint/cli/src/com/android/tools/lint/TextReporter.java @@ -18,22 +18,35 @@ package com.android.tools.lint; import com.android.tools.lint.detector.api.Location; import com.android.tools.lint.detector.api.Position; +import com.google.common.annotations.Beta; import java.io.IOException; import java.io.Writer; import java.util.List; -/** A reporter which emits lint warnings as plain text strings */ -class TextReporter extends Reporter { +/** + * A reporter which emits lint warnings as plain text strings + * <p> + * <b>NOTE: This is not a public or final API; if you rely on this be prepared + * to adjust your code for the next tools release.</b> + */ +@Beta +public class TextReporter extends Reporter { private final Writer mWriter; - TextReporter(Main client, Writer writer) { + /** + * Constructs a new {@link TextReporter} + * + * @param client the client + * @param writer the writer to write into + */ + public TextReporter(Main client, Writer writer) { super(client, null); mWriter = writer; } @Override - void write(int errorCount, int warningCount, List<Warning> issues) throws IOException { + public void write(int errorCount, int warningCount, List<Warning> issues) throws IOException { boolean abbreviate = mClient.getDriver().isAbbreviating(); StringBuilder output = new StringBuilder(issues.size() * 200); diff --git a/lint/cli/src/com/android/tools/lint/XmlReporter.java b/lint/cli/src/com/android/tools/lint/XmlReporter.java index 4de1dd7..03fc937 100644 --- a/lint/cli/src/com/android/tools/lint/XmlReporter.java +++ b/lint/cli/src/com/android/tools/lint/XmlReporter.java @@ -18,6 +18,7 @@ package com.android.tools.lint; import com.android.tools.lint.detector.api.Location; import com.android.tools.lint.detector.api.Position; +import com.google.common.annotations.Beta; import com.google.common.base.Charsets; import com.google.common.io.Files; @@ -29,17 +30,28 @@ import java.util.List; /** * A reporter which emits lint results into an XML report. + * <p> + * <b>NOTE: This is not a public or final API; if you rely on this be prepared + * to adjust your code for the next tools release.</b> */ -class XmlReporter extends Reporter { +@Beta +public class XmlReporter extends Reporter { private final Writer mWriter; - XmlReporter(Main client, File output) throws IOException { + /** + * Constructs a new {@link XmlReporter} + * + * @param client the client + * @param output the output file + * @throws IOException if an error occurs + */ + public XmlReporter(Main client, File output) throws IOException { super(client, output); mWriter = new BufferedWriter(Files.newWriter(output, Charsets.UTF_8)); } @Override - void write(int errorCount, int warningCount, List<Warning> issues) throws IOException { + public void write(int errorCount, int warningCount, List<Warning> issues) throws IOException { mWriter.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //$NON-NLS-1$ "<issues>\n"); //$NON-NLS-1$ |