aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2011-12-20 11:30:53 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-12-20 11:30:53 -0800
commit3a8542a6e9a3318e1a630b9733f313f0e24607f2 (patch)
tree0c568e52658891de9458b077cf719fcc849d6f9d
parent05e124852db51ab78ec592155b43ff86a91a18ab (diff)
parent0bb3a7e370712f26d3f4285a700ae89fd4fa6253 (diff)
downloadsdk-3a8542a6e9a3318e1a630b9733f313f0e24607f2.zip
sdk-3a8542a6e9a3318e1a630b9733f313f0e24607f2.tar.gz
sdk-3a8542a6e9a3318e1a630b9733f313f0e24607f2.tar.bz2
Merge "Add new lint rule looking for style definition cycles"
-rw-r--r--lint/libs/lint_checks/src/com/android/tools/lint/checks/BuiltinIssueRegistry.java1
-rw-r--r--lint/libs/lint_checks/src/com/android/tools/lint/checks/StyleCycleDetector.java87
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/StyleCycleDetectorTest.java34
-rw-r--r--lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/styles.xml15
4 files changed, 137 insertions, 0 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/BuiltinIssueRegistry.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/BuiltinIssueRegistry.java
index cc2a799..689ad89 100644
--- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/BuiltinIssueRegistry.java
+++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/BuiltinIssueRegistry.java
@@ -61,6 +61,7 @@ public class BuiltinIssueRegistry extends IssueRegistry {
issues.add(WrongIdDetector.UNKNOWN_ID);
issues.add(WrongIdDetector.UNKNOWN_ID_LAYOUT);
issues.add(StateListDetector.ISSUE);
+ issues.add(StyleCycleDetector.ISSUE);
issues.add(InefficientWeightDetector.INEFFICIENT_WEIGHT);
issues.add(InefficientWeightDetector.NESTED_WEIGHTS);
issues.add(InefficientWeightDetector.BASELINE_WEIGHTS);
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/StyleCycleDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/StyleCycleDetector.java
new file mode 100644
index 0000000..8c1d6d3
--- /dev/null
+++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/StyleCycleDetector.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tools.lint.checks;
+
+import static com.android.tools.lint.detector.api.LintConstants.ATTR_NAME;
+import static com.android.tools.lint.detector.api.LintConstants.ATTR_PARENT;
+import static com.android.tools.lint.detector.api.LintConstants.STYLE_RESOURCE_PREFIX;
+import static com.android.tools.lint.detector.api.LintConstants.TAG_STYLE;
+
+import com.android.resources.ResourceFolderType;
+import com.android.tools.lint.detector.api.Category;
+import com.android.tools.lint.detector.api.Issue;
+import com.android.tools.lint.detector.api.ResourceXmlDetector;
+import com.android.tools.lint.detector.api.Scope;
+import com.android.tools.lint.detector.api.Severity;
+import com.android.tools.lint.detector.api.Speed;
+import com.android.tools.lint.detector.api.XmlContext;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Checks for cycles in style definitions
+ */
+public class StyleCycleDetector extends ResourceXmlDetector {
+ /** The main issue discovered by this detector */
+ public static final Issue ISSUE = Issue.create(
+ "StyleCycle", //$NON-NLS-1$
+ "Looks for cycles in style definitions",
+ "There should be no cycles in style definitions as this can lead to runtime " +
+ "exceptions.",
+ Category.CORRECTNESS,
+ 8,
+ Severity.ERROR,
+ StyleCycleDetector.class,
+ Scope.RESOURCE_FILE_SCOPE);
+
+ /** Constructs a new {@link StyleCycleDetector} */
+ public StyleCycleDetector() {
+ }
+
+ @Override
+ public boolean appliesTo(ResourceFolderType folderType) {
+ return folderType == ResourceFolderType.VALUES;
+ }
+
+ @Override
+ public Speed getSpeed() {
+ return Speed.FAST;
+ }
+
+ @Override
+ public Collection<String> getApplicableElements() {
+ return Collections.singleton(TAG_STYLE);
+ }
+
+ @Override
+ public void visitElement(XmlContext context, Element element) {
+ Attr parentNode = element.getAttributeNode(ATTR_PARENT);
+ if (parentNode != null) {
+ String parent = parentNode.getValue();
+ String name = element.getAttribute(ATTR_NAME);
+ if (parent.endsWith(name) &&
+ parent.equals(STYLE_RESOURCE_PREFIX + name)) {
+ context.report(ISSUE, context.getLocation(parentNode),
+ String.format("Style %1$s should not extend itself", name), null);
+ }
+ }
+ }
+}
diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/StyleCycleDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/StyleCycleDetectorTest.java
new file mode 100644
index 0000000..39126df
--- /dev/null
+++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/StyleCycleDetectorTest.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tools.lint.checks;
+
+import com.android.tools.lint.detector.api.Detector;
+
+@SuppressWarnings("javadoc")
+public class StyleCycleDetectorTest extends AbstractCheckTest {
+ @Override
+ protected Detector getDetector() {
+ return new StyleCycleDetector();
+ }
+
+ public void test() throws Exception {
+ assertEquals(
+ "styles.xml:9: Error: Style DetailsPage_EditorialBuyButton should not extend itself",
+
+ lintProject("res/values/styles.xml"));
+ }
+}
diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/styles.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/styles.xml
new file mode 100644
index 0000000..34e2d6f
--- /dev/null
+++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/values/styles.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+<style name="DetailsPage_BuyButton" parent="@style/DetailsPage_Button">
+ <item name="android:textColor">@color/buy_button</item>
+ <item name="android:background">@drawable/details_page_buy_button</item>
+</style>
+
+<style name="DetailsPage_EditorialBuyButton" parent="@style/DetailsPage_EditorialBuyButton" />
+<!-- Should have been:
+<style name="DetailsPage_EditorialBuyButton" parent="@style/DetailsPage_BuyButton" />
+-->
+
+</resources>
+