aboutsummaryrefslogtreecommitdiffstats
path: root/lint
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-09-25 11:59:02 -0700
committerTor Norbye <tnorbye@google.com>2012-09-25 12:03:16 -0700
commit16b645da280619f57e6f93d41c0c5fd478c289de (patch)
tree406c715fd6d5218631de3c6bc621c71e9910c925 /lint
parente41d71e6d66d7fc3ac8a502dabf3f6be93b9403d (diff)
downloadsdk-16b645da280619f57e6f93d41c0c5fd478c289de.zip
sdk-16b645da280619f57e6f93d41c0c5fd478c289de.tar.gz
sdk-16b645da280619f57e6f93d41c0c5fd478c289de.tar.bz2
37702: InconsistentArrays fails to identify inconsistent arrays
Change-Id: I47d3757cac4b9204bdb15a84a8679921adc049a3
Diffstat (limited to 'lint')
-rw-r--r--lint/libs/lint_checks/src/com/android/tools/lint/checks/ArraySizeDetector.java67
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ArraySizeDetectorTest.java13
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values-it/stringarrays.xml12
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/stringarrays.xml13
4 files changed, 75 insertions, 30 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/ArraySizeDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/ArraySizeDetector.java
index 771c247..8898507 100644
--- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/ArraySizeDetector.java
+++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/ArraySizeDetector.java
@@ -35,6 +35,8 @@ import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.Severity;
import com.android.tools.lint.detector.api.XmlContext;
import com.android.utils.Pair;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
@@ -78,7 +80,9 @@ public class ArraySizeDetector extends ResourceXmlDetector {
ArraySizeDetector.class,
Scope.ALL_RESOURCES_SCOPE);
- private Map<File, Pair<String, Integer>> mFileToArrayCount;
+ //private Map<File, List<Pair<String, Integer>>> mFileToArrayCount;
+ //private Map<File, List<Pair<String, Integer>>> mFileToArrayCount;
+ private Multimap<File, Pair<String, Integer>> mFileToArrayCount;
/** Locations for each array name. Populated during phase 2, if necessary */
private Map<String, Location> mLocations;
@@ -107,7 +111,7 @@ public class ArraySizeDetector extends ResourceXmlDetector {
@Override
public void beforeCheckProject(@NonNull Context context) {
if (context.getPhase() == 1) {
- mFileToArrayCount = new HashMap<File, Pair<String,Integer>>(30);
+ mFileToArrayCount = ArrayListMultimap.create(30, 5);
}
}
@@ -125,35 +129,38 @@ public class ArraySizeDetector extends ResourceXmlDetector {
Collections.sort(keys);
for (File file : keys) {
- Pair<String, Integer> pair = mFileToArrayCount.get(file);
- String name = pair.getFirst();
- if (alreadyReported.contains(name)) {
- continue;
- }
- Integer count = pair.getSecond();
-
- Integer current = countMap.get(name);
- if (current == null) {
- countMap.put(name, count);
- fileMap.put(name, file);
- } else if (!count.equals(current)) {
- alreadyReported.add(name);
-
- if (mLocations == null) {
- mLocations = new HashMap<String, Location>();
- mDescriptions = new HashMap<String, String>();
+ Collection<Pair<String, Integer>> pairs = mFileToArrayCount.get(file);
+ for (Pair<String, Integer> pair : pairs) {
+ String name = pair.getFirst();
+
+ if (alreadyReported.contains(name)) {
+ continue;
+ }
+ Integer count = pair.getSecond();
+
+ Integer current = countMap.get(name);
+ if (current == null) {
+ countMap.put(name, count);
+ fileMap.put(name, file);
+ } else if (!count.equals(current)) {
+ alreadyReported.add(name);
+
+ if (mLocations == null) {
+ mLocations = new HashMap<String, Location>();
+ mDescriptions = new HashMap<String, String>();
+ }
+ mLocations.put(name, null);
+
+ String thisName = file.getParentFile().getName() + File.separator
+ + file.getName();
+ File otherFile = fileMap.get(name);
+ String otherName = otherFile.getParentFile().getName() + File.separator
+ + otherFile.getName();
+ String message = String.format(
+ "Array %1$s has an inconsistent number of items (%2$d in %3$s, %4$d in %5$s)",
+ name, count, thisName, current, otherName);
+ mDescriptions.put(name, message);
}
- mLocations.put(name, null);
-
- String thisName = file.getParentFile().getName() + File.separator
- + file.getName();
- File otherFile = fileMap.get(name);
- String otherName = otherFile.getParentFile().getName() + File.separator
- + otherFile.getName();
- String message = String.format(
- "Array %1$s has an inconsistent number of items (%2$d in %3$s, %4$d in %5$s)",
- name, count, thisName, current, otherName);
- mDescriptions.put(name, message);
}
}
diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ArraySizeDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ArraySizeDetectorTest.java
index 629f43c..127e7f8 100644
--- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ArraySizeDetectorTest.java
+++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ArraySizeDetectorTest.java
@@ -47,6 +47,19 @@ public class ArraySizeDetectorTest extends AbstractCheckTest {
"res/values-es/strings.xml"));
}
+ public void testMultipleArrays() throws Exception {
+ assertEquals(
+ "res/values/stringarrays.xml:3: Warning: Array map_density_desc has an inconsistent number of items (5 in values/stringarrays.xml, 1 in values-it/stringarrays.xml) [InconsistentArrays]\n" +
+ " <string-array name=\"map_density_desc\">\n" +
+ " ^\n" +
+ " res/values-it/stringarrays.xml:6: Declaration with array size (1)\n" +
+ "0 errors, 1 warnings\n",
+
+ lintProject(
+ "res/values-it/stringarrays.xml",
+ "res/values/stringarrays.xml"));
+ }
+
public void testArraySizesSuppressed() throws Exception {
assertEquals(
"No warnings.",
diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values-it/stringarrays.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values-it/stringarrays.xml
new file mode 100644
index 0000000..7bd83e9
--- /dev/null
+++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values-it/stringarrays.xml
@@ -0,0 +1,12 @@
+<?xml version='1.0' encoding='utf-8'?>
+<resources>
+ <string-array name="track_type_desc">
+ <item>Pendenza</item>
+ </string-array>
+ <string-array name="map_density_desc">
+ <item>Automatico (mappa leggibile su display HD)</item>
+ </string-array>
+ <string-array name="cache_size_desc">
+ <item>Piccolo (100)</item>
+ </string-array>
+</resources> \ No newline at end of file
diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/stringarrays.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/stringarrays.xml
new file mode 100644
index 0000000..6405580
--- /dev/null
+++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/stringarrays.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string-array name="map_density_desc">
+ <item>Automatic (readable map on HD displays)</item>
+ <item>1 map pixel = 1 screen pixel</item>
+ <item>1 map pixel = 1.25 screen pixels</item>
+ <item>1 map pixel = 1.5 screen pixels</item>
+ <item>1 map pixel = 2 screen pixels</item>
+ </string-array>
+ <string-array name="spatial_resolution_desc">
+ <item>5m/yd (fine, default)</item>
+ </string-array>
+</resources> \ No newline at end of file