History log of /sdk/lint/libs/lint_checks/src/com/android/tools/lint/checks/UselessViewDetector.java
Revision Date Author Comments
12d4581faa6438941e65a9dc83213be34c6ca970 13-Sep-2012 Tor Norbye <tnorbye@google.com> Constants refactoring.

This changeset moves most constants into the SdkConstants
class, and gets rid of AndroidConstants and LintConstants.
It also migrates all non-ADT specific constants from
AdtConstants into SdkConstants. It furthermore moves various
other constants (such as those in XmlUtils and ValuesDescriptors)
into the constants class. It also fixes the modifier order
to be the canonical modifier order (JLS 8.x).

Finally, it removes redundancy and combines various constant
aliases such that we don't have both NAME_ATTR and ATTR_NAME
pointing to "name", etc.

Change-Id: Ifd1755016f62ce2dd80e5c76130d6de4b0e32161
7e4b8e9d595e45baa9d87cdb8282f02759e73abc 30-May-2012 Tor Norbye <tnorbye@google.com> Fix nullness annotations

Eclipse 4.2 includes analysis support for @Nullable and @NonNull
annotations. However, it requires these annotations to be *repeated*
on every single method implementing or overriding a superclass or
interface method (!).

This changeset basically applies the quickfixes to inline these
annotations. It also changes the retention of our nullness
annotations from source to class, since without this Eclipse believes
that a @NonNull annotation downstream is a redefinition of a @Nullable
annotation.

Finally, the null analysis revealed a dozen or so places where the
nullness annotation was either wrong, or some null checking on
parameters or return values needed to be done.

Change-Id: I43b4e56e2d025a8a4c92a8873f55c13cdbc4c1cb
be7c3d9ef5a346323faee9d6663bd2d19a6ce0a2 08-Feb-2012 Tor Norbye <tnorbye@google.com> Suppress UselessLeaf warnings for root layouts

Change-Id: I997ad6f1348e9884b56a60ef453cd02754c936bb
69067f399231dc28f4ff0aa02b60153ffd2d5831 06-Feb-2012 Tor Norbye <tnorbye@google.com> Add support for suppressing lint via XML attributes

This changeset adds support for suppressing in XML files:

(1) Lint will ignore errors found in attributes and elements if the
element (or any surrounding parent elements) specifies a
tools:ignore="id-list" attribute where the id-list matches the id
of the reported issue (or "all"). The "tools" prefix can be any
prefix bound to the namespace "http://schemas.android.com/tools"

(2) There's a new quickfix shown for XML lint warnings which offers to
add a lint suppress attribute for a given lint warning (setting
the id to the id of the warning, and adding the tools namespace
binding if necessary).

(3) The XML formatter now handles namespaces a bit better: after the
preferred attributes (id, name, style, layout params, etc) have
been handled, attributes are sorted by namespace prefix before
they are sorted by local name -- which effectively will sort any
new tools:ignore attributes to the end.

Change-Id: Id7474cde5665d9bd29bdd4e0d0cc89ed4d422aea
5fc3da5bd6c546bc962cc8d9348ce631d44d6d4c 20-Dec-2011 Tor Norbye <tnorbye@google.com> Fix 23204: Lint false positive 'This TableRow view is useless'?

The check which identifies "useless" views (layouts that do not have
an id, background, or children) also need to consider the style
attribute since custom styles could be setting (for example) a
background.

Change-Id: I5adad11a365082d52609a060f7573ab7e80a0351
e61a2749bf3d2509fc6b3472f07f0ecf695d2173 14-Dec-2011 Tor Norbye <tnorbye@google.com> Fix 22848: Lint: Bogus suggestion and fix for flattening layout

TabHosts etc should not be eligible as parents for an unused layout.

Change-Id: Ic934046530a5c8a4ff84a873e63230819f4fd4a5
3ce45b249f898697ae82e8c6dd045966227f3438 02-Dec-2011 Tor Norbye <tnorbye@google.com> Lint infrastructure improvements

This changeset fixes a bunch of issues in the infrastructure:

(1) It cleans up the Context class quite a bit. It had some hardcoded
XML stuff in it, which is now in a separate XmlContext class (and
there will be a JavaContext class in the Java support CL).

It also hides a bunch of public fields, cleans up some unused
stuff, and introduces a couple of wrapper methods to make detector
code cleaner; in particular, rather than calling
context.client.report(context, ...
you can now just call
context.report(...
and similarly there are wrappers for logging and checking for
disabled issues.

(2) The IParser interface is renamed to IDomParser since in the next
CL there will also be an IJavaParser. Some other related cleanup.

(3) There is now a "Location.Handle" interface. This allows detectors
to create light-weight location holders, and later on call
handle.resolve() to create a full-fledged Location. This is useful
when detectors don't yet know whether they'll need a location for
a node, but want to store it for later in case they do. As an
example, the unused resource detector creates location handles for
declaration and only resolves full locations for those that are
found to be unused.

Locations can now carry custom messages. For example, for a
duplicate id error, the secondary location now contains a
"original declaration here" message. And the CLI and HTML reports
now include alternate locations in the output.

Some other location cleanup too; using factory methods to make the
code cleaner, some default implementations that can be shared,
etc.

(4) There's a new SDK info class intended to provide SDK information
from a tool client (such as resource resolution). It currently
just contains parent-view information, used for the
ObsoleteLayoutParams detector and an upcoming CL for a
ViewTypeDetector.

(5) The Detector class now provides dummy implementations for the
inner-interfaces, so we no longer need the adapter classes. This
makes it easy to implement the XmlScanner or JavaScanner
interfaces without needing to also stub out a bunch of methods.

Change-Id: I4b3aaabe51febb25b000f9086703653bea6cf7c9
229581314076be1b6f82fe1efed2bd00da340899 07-Nov-2011 Tor Norbye <tnorbye@google.com> Lint Architecture Changes: Configurations, Categories, etc.

This changeset makes various architectural changes to lint:

(1) Add configurations, which are basically user preferences for lint
in a given project, such as providing a custom severity for an
issue, or disabling a specific warning in a specific file.

In Eclipse, there is a project configuration (stored in lint.xml
in each project), as well as a global configuration (stored using
Eclipse preference data). Project configurations inherit from the
global configuration.

The options dialog now shows up both as a project property page
(showing the issue state for the project configuration), as well
as a normal preference page (showing the global or "fallback"
configuraiton). I also changed the Options UI for issues from a
Table to a TreeViewer to add in category nodes, and changed the
checkbox UI to have a custom severity toggle instead.

The lint quickfixes also now have 3 suppression options:
* Ignore in this file
* Ignore in this project
* Disable check

(2) Change detectors to be registered by class and instantiated for
each lint run rather than having a fixed list of detectors get
reused over and over. Turns out that since some detectors store
state, this prevented lints from running concurrently since the
two runs could stomp each other's state.

To do this effectively I've also switched from a DetectorRegistry
to an IssueRegistry, which contains the global list of available
issues and each issue can point to the class detecting the issue
(and these are created on the fly based on parameters like scope.)

(3) Explicit Categories. Categories used to just be a string property
on issues; now it's an explicit class with both a name and an
explanation, with parents to allow nesting (such that for example
the Usability category has an Icons sub category), and finally the
category class provides sorting. Categories also show up in the
HTML Report now as separate sections.

(4) Other API changes:

* I changed the package containing APIs for lint clients to an
explicit "client" package
* Moved the LintConstants class up from lint-checks to lint-api
and added a LintUtils class which contains many generic methods
that were spread around in specific detectors.
* The detectors are now talking to a wrapper client rather than
directly to lint clients, such that the wrapper client can
filter out results on disabled checks etc, which means that
tools can assume they always get correct reports and don't have
to worry about improperly written detectors.
* I renamed ToolContext to LintClient.
* I got rid of the "isEnabled" state, which was a bit redundant
with the severity since severity has a Severity.IGNORE value.
* I added a LintListener interface, which notifies about progress
(and the CLI tool will print "."'s for each processed file
unless suppressed with -q).
* A new dispose method on the parser interface to for example
allow IDEs to lock/unlock read models on underlying data.

(5) I added a toolbar action for running Lint on the currently
selected project. I also added an --xml export option intended
for use with CI plugins.

Change-Id: Icadd9b2d14d075234d97b31398806111af747b7b
27ed098e750ee9463dfa7a36cef68d219de9eb50 31-Oct-2011 Tor Norbye <tnorbye@google.com> Lint infrastructure fixes

This changeset makes a number of infrastructure fixes to lint. It also
has some user visible aspects:

(1) The "run lint on export" option, which aborts Export APK if fatal
errors are found, now only checks for fatal issues, which should
be significantly faster (since it skips expensive detectors like
the unused resource detector).

(2) The command line lint tool lets you specify not just issue ids,
but categories to enable or disable as well. In addition to the
--enable and --disable flags to add or remove checks, there is
also a --check flag which runs the exact specified checks. Thus
you can for example run "lint --check Security" to run the
security related lint checks. When using --show to display the
available id's, they are organized and described under category
labels.

I also cleaned up the categories a bit; "Layout" isn't a category
anymore, and instead the layoutopt options are placed in other
categories like "Performance" or "Usability".

(3) From the command line you can now also specify multiple projects
or even search a directory for projects contained recursively
within it. This required a bunch of infrastructure changes to
handle partitioning up the arguments into related projects (since
checks have before-project and after-project hooks that need to
run properly).

(4) On the infrastructure side the "scope" concept was changed to
become a scope set, and a detector can declare that an issue
requires analysis of any of {manifest, resource file, java source
file, resource folder, ...} etc. When lint runs it determines
which detectors are applicable (for example, for a single-file
lint run it will ignore detectors which require a wider
scope). And when applicable, a detector will be called for all the
various scopes it requires. Therefore, the unused resource
detector, which used to have to manually scan the manifest file on
its own, now automatically gets called as part of the manifest
file parse, the resource file parses, and the java file scan.
Single-file linting is still only supported for XML files.

Change-Id: I49a5e2b27f8f6da22904085089aa0c4d2eee67f6
699dcc5fd8a93fcc3842954af35be7f7be793740 28-Oct-2011 Tor Norbye <tnorbye@google.com> Fix Useless layout detector, and add HTML summary

First, fix the logic in the useless layout detector to properly
consider the background attributes.

Second, add a summary section at the top of the HTML report with
hyperlinks to each issue section.

Change-Id: I7e9e2d81babf977751fc3ca64e61fcc8434a0792
8853b3b92496c5734bbcb75499030bb51a060097 23-Oct-2011 Tor Norbye <tnorbye@google.com> Lint: Unused resource detector, and framework improvements

This changeset contains a number of changes to the lint support:

(1) Preparation to support Java (.java source and .class binary
checks). The lint-api and lint-checks libraries do not yet
contain any APIs for visiting Java ASTs or visiting bytecodes, but
there is preliminary support such as interfaces Java and Class
detectors can implement, and they are provided with the source
paths and output paths.

(2) Various other framework API changes such as making ToolContext an
abstract class (to easy future API additions), moving file reading
to the tool context, making the Xml/Java/Class detectors marker
interfaces instead of abstract classes such that a detector can
have more than one behavior. And the error reporter now passes a
data field along with each issue containing data intended for use
by tools to for example make a quickfix able to identify target
data without having to parse the error message or infer it from
the error range.

(3) A new "unused resource" detector which identifies unused resources
(strings, ids, layouts, etc) in a project. The default lint-checks
implementation performs only simple string analysis, but in the
Eclipse plugin this is replaced by a specialized detector which
uses the Eclipse Java model to do more accurate AST-based
analysis. This is a technique lint should support anyway (the
ability of a tool to provide optimized versions of the builtin
checks) but longer term once we get actual Java AST support into
the API this might move.

Various misc smaller fixes I ran into along the way and changes to the
test infrastructure.

Change-Id: Ic5aab0180b53de70f1fd36540c854abb8210e751
b4622eb274f472919a7d7ae0bab045e4b9e4325f 21-Oct-2011 Tor Norbye <tnorbye@google.com> Lint improvements

First, in the translation detector, split out the report into two
separate issues:
- Failing to provide a translation for a string defined by the default
locale (Error)
- Unused translations (strings found in languages but no longer in the
default locale) (Warning)
Also add region handling such that a region can define just a subset
of the available strings and fall back to the overall language for
strings it does not define.

Second, the Quickfix in Eclipse for the px to dp conversion now shows
a dedicated dialog which has a combobox for the available densities
rather than asking the user to type in the density number in a text
field.

Third, the CLI driver (the "lint" command line tool) has a bunch of
new flags for controlling the report format as well as for dumping
out information about available issues:

Usage: lint [flags] <project directories>

Flags:
--suppress <id-list>: Suppress a list of issue id's.
--enable <id-list>: Only check the specific list of issues
--fullpath : Use full paths in the error output
--lines : Include the lines with errors in the output

--list: List the available issue id's and exit.
--show: List available issues along with full explanations
--show <ids>: Show full explanations for the given list of issue id's
Id lists should be comma separated with no spaces.

Here's an example of what an issue explanation looks like:

% lint --show DuplicateIds
DuplicateIds
------------
Summary: Checks for duplicate ids within a single layout or within an
include hierarchy

Priority: 7 / 10
Severity: Warning
Category: Layout

It's okay for two independent layouts to use the same ids. However,
within a single layout (including the case where two separate layouts
are fused together with an include tag) the ids should be unique such
that theActivity#findViewById() method can work predictably.

---

The layout editor now runs lint on every edit operation instead of
on every save.

And finally the scope has been moved from Detectors to Issues such
that when looking at issue markers we can clear those associated with
the scope the analysis is run under. We also run the detectors when
any of the associated issues is eligible.

Change-Id: I3f068de4edeb3ed166eeb7ab7f2cff84d7ae5cbd
8027fad9680e1622d7c70be330422d6b11fc6c88 18-Oct-2011 Tor Norbye <tnorbye@google.com> More lint checks: translation, i18n, proguard, gridlayout, "px"

This changeset adds more lint checks:

(1) Translation. It looks at all the string values in the application,
and if there are discrepancies (where a translatable string is not
defined in all provided languages and regions) then these are
listed.

(2) Internationalization. It looks for text: and contentDescription:
attributes and ensures that these refer to @string resources, not
hardcoded string values. This lint warning also has an associated
quickfix when shown in Eclipse which invokes the Extract String
refactoring wizard on the right selection context.

(3) Proguard. It looks for the old (broken) patterns we had in older
proguard.cfg files (-keepclasseswithmembernames instead of
-keepclasseswithmembers which implies shrinking.)

(4) GridLayout. It looks at the layout constraints provided on views
in a GridLayout and ensures that they fall within the overall
dimensions of the grid.

(5) "px" usage. It looks for dimensions using "px" as a unit and
recommends switching to dp instead. This lint warning also has a
quickfix in Eclipse which pops up a dialog asking for the screen
density and then converts the px value to the right dp value and
changes the unit.

(6) TextFields. It looks at EditTexts and makes sure they either set
inputType, hint or inputMethod. There's a quickfix in Eclipse for
setting the inputType, which adds the property and automatically
invokes content assist for showing the possible values.

This changeset also adds some quick fixes for a few existing lint
warnings:

(7) Accessibility: Insert a content description attribute, front the
editor and select the placeholder value.

(8) Useless leaf layout: Remove the leaf layout

(9) Useless middle layout: Invoke the Remove Container visual
refactoring

10) Inefficient Linear Layout Weights: Change the attribute to 0dp

Plus unit tests.

Change-Id: Iebd7b23224a898bd1851abd578460019aee44df5
a85107f7d763276a5a040cf68e2046ac54202015 06-Oct-2011 Tor Norbye <tnorbye@google.com> Static analyzer

This changeset adds a static analyzer, "lint", which looks for various
potential bugs in Android projects. It has 3 parts:

(1) A library which performs the actual static checks.
This library is standalone: it does not depend on Eclipse.
(Technically the library has two halves: an API half, for use
by third party developers to write additional detectors, and
an actual implementation of a bunch of built-in checks.)

(2) A command line driver, "lint", which runs the static checks and
emits any warnings to standard out. This can be thought of as
a replacement for the layoutopt tool.

(3) Eclipse integration. Lint errors are added to the Problems view as
well as shown as editor annotations. There's an options panel for
controlling which detectors are enabled. There's also a quickfix
for disabling errors directly within the editor and a marker
resolution for disabling them via the Problems view.

The static checks are run on an XML file right after it has been
saved. (This is optional via a toggle on the same preference page
as the detector list.)

The static checks are also run when you export an APK, and if any
fatal errors are found the export is abandoned. (This is also
optional via an option).

Finally you can run a full lint through the Android Tools menu,
and there's also an action to clear all the lint markers there.

There's also a new indicator on the layout editor which shows
whether there are lint errors on the associated file, and when
clicked brings up a dialog listing the specific errors.

This changeset also includes a number of checks:

* An accessibility detector which warns about images missing
contentDescriptions
* A drawable selector detector which warns about state lists where not
all states are reachable (e.g. it is not the case that only the last
item in the list omits a state qualifier)
* A detector finding duplicate ids, not just in the current layout but
across included layouts (transitively) as well
* All the layoutopt ones ported to Java + DOM
* Unit tests for the above.

The focus here is on getting the infrastructure in place, and it
currently focuses on XML resource files and analyzing them
efficiently. See the comment in XmlVisitor for details on that.

Change-Id: Ic5f5f37d92bfb96ff901b959aaac24db33552ff7