diff options
author | Tor Norbye <tnorbye@google.com> | 2012-03-23 19:58:56 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-03-23 20:22:46 -0700 |
commit | a6bfeecf4573e5ae66185170ffdcfa51bfb974d2 (patch) | |
tree | c9e87e8c6461bceefda8a75ff39af04e887f0953 /lint/libs | |
parent | 04aebba9b3a718aedddc1ff5726201e0952cbe81 (diff) | |
download | sdk-a6bfeecf4573e5ae66185170ffdcfa51bfb974d2.zip sdk-a6bfeecf4573e5ae66185170ffdcfa51bfb974d2.tar.gz sdk-a6bfeecf4573e5ae66185170ffdcfa51bfb974d2.tar.bz2 |
27441: LINT: Incorrect WrongViewCast message
Change-Id: I9687c0c59e13c340b2d564d5aa17635153d88d07
Diffstat (limited to 'lint/libs')
5 files changed, 71 insertions, 4 deletions
diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultSdkInfo.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultSdkInfo.java index f8d6cd9..29c3da6 100644 --- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultSdkInfo.java +++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/DefaultSdkInfo.java @@ -77,6 +77,8 @@ class DefaultSdkInfo extends SdkInfo { @Override @Nullable public String getParentViewName(@NonNull String name) { + name = getRawType(name); + return PARENTS.get(name); } @@ -102,6 +104,9 @@ class DefaultSdkInfo extends SdkInfo { @Override public boolean isSubViewOf(@NonNull String parent, @NonNull String child) { + parent = getRawType(parent); + child = getRawType(child); + // Do analysis just on non-fqcn paths if (parent.indexOf('.') != -1) { parent = parent.substring(parent.lastIndexOf('.') + 1); @@ -128,13 +133,25 @@ class DefaultSdkInfo extends SdkInfo { return false; } + // Strip off type parameters, e.g. AdapterView<?> => AdapterView + private static String getRawType(String type) { + if (type != null) { + int index = type.indexOf('<'); + if (index != -1) { + type = type.substring(0, index); + } + } + + return type; + } + private static final int CLASS_COUNT = 59; @NonNull private static final Map<String, String> PARENTS = new HashMap<String, String>(CLASS_COUNT); static { - PARENTS.put(COMPOUND_BUTTON, VIEW); + PARENTS.put(COMPOUND_BUTTON, BUTTON); PARENTS.put(ABS_SPINNER, ADAPTER_VIEW); PARENTS.put(ABS_LIST_VIEW, ADAPTER_VIEW); PARENTS.put(ABS_SEEK_BAR, ADAPTER_VIEW); @@ -201,9 +218,7 @@ class DefaultSdkInfo extends SdkInfo { /* // Check that all widgets lead to the root view - boolean assertionsEnabled = false; - assert assertionsEnabled = true; // Intentional side-effect - if (assertionsEnabled) { + if (LintUtils.assertionsEnabled()) { for (String key : PARENTS.keySet()) { String parent = PARENTS.get(key); if (!parent.equals(VIEW)) { diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ViewTypeDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ViewTypeDetectorTest.java index f1f8492..2ea876f 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ViewTypeDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/ViewTypeDetectorTest.java @@ -34,4 +34,14 @@ public class ViewTypeDetectorTest extends AbstractCheckTest { "src/test/pkg/WrongCastActivity.java.txt=>src/test/pkg/WrongCastActivity.java" )); } + + public void test27441() throws Exception { + assertEquals( + "No warnings.", + + lintProject( + "res/layout/casts2.xml", + "src/test/pkg/WrongCastActivity2.java.txt=>src/test/pkg/WrongCastActivity2.java" + )); + } } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/casts2.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/casts2.xml new file mode 100644 index 0000000..249c02f --- /dev/null +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/casts2.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- unit test from issue 27441 --> +<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" > + + <RadioGroup + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="vertical" > + + <RadioButton + android:id="@+id/additional" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + + <Spinner + android:id="@+id/reminder_lead" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + </RadioGroup> + +</ScrollView> diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/WrongCastActivity2.java.txt b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/WrongCastActivity2.java.txt new file mode 100644 index 0000000..7cd422a --- /dev/null +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/WrongCastActivity2.java.txt @@ -0,0 +1,15 @@ +package test.pkg; + +import android.app.*; +import android.view.*; +import android.widget.*; + +public class WrongCastActivity2 extends Activity { + private TextView additionalButton; + + private void configureAdditionalButton(View bodyView) { + this.additionalButton = (TextView) bodyView + .findViewById(R.id.additional); + Object x = (AdapterView<?>) bodyView.findViewById(R.id.reminder_lead); + } +} diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java index 2ce41e4..afdc985 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java @@ -42,6 +42,9 @@ public class DefaultSdkInfoTest extends TestCase { DefaultSdkInfo info = new DefaultSdkInfo(); assertTrue(info.isSubViewOf("Button", "Button")); assertTrue(info.isSubViewOf("TextView", "Button")); + assertTrue(info.isSubViewOf("TextView", "RadioButton")); + assertTrue(info.isSubViewOf("AdapterView", "Spinner")); + assertTrue(info.isSubViewOf("AdapterView<?>", "Spinner")); assertFalse(info.isSubViewOf("Button", "TextView")); assertFalse(info.isSubViewOf("CheckBox", "ToggleButton")); assertFalse(info.isSubViewOf("ToggleButton", "CheckBox")); @@ -52,5 +55,6 @@ public class DefaultSdkInfoTest extends TestCase { assertFalse(info.isSubViewOf("EditText", "TextView")); assertTrue(info.isSubViewOf("View", "TextView")); assertFalse(info.isSubViewOf("TextView", "View")); + assertFalse(info.isSubViewOf("Spinner", "AdapterView<?>")); } } |