diff options
author | Tor Norbye <tnorbye@google.com> | 2011-12-20 10:55:18 -0800 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2011-12-20 10:55:18 -0800 |
commit | 0bb3a7e370712f26d3f4285a700ae89fd4fa6253 (patch) | |
tree | 5bc4ce735b94f54db92bd1b6091f54f14f73640c | |
parent | 3d73211da767ae07698d12db1f6d6f8a0b0ed7bd (diff) | |
download | sdk-0bb3a7e370712f26d3f4285a700ae89fd4fa6253.zip sdk-0bb3a7e370712f26d3f4285a700ae89fd4fa6253.tar.gz sdk-0bb3a7e370712f26d3f4285a700ae89fd4fa6253.tar.bz2 |
Add new lint rule looking for style definition cycles
This changeset fixes enhancement
23288: Style extending itself Lint rule
Change-Id: Id35bcce02b55735becb2260a3276128bfd71f2a6
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> + |