From be954f2c27ceac5140b7b5c2a10a05d1d16de465 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 10 Mar 2015 22:01:03 +0300 Subject: [PATCH] Fix WrongIdFormat exception on Android XML widgets with non-@+id/ format --- .../lang/resolve/android/AndroidConst.kt | 6 ++---- .../lang/resolve/android/AndroidXmlHandler.kt | 5 ++++- .../plugin/android/AndroidRenameProcessor.kt | 4 +++- .../plugin/android/AndroidXmlVisitor.kt | 3 ++- .../wrongIdFormat/res/layout/layout.xml | 20 +++++++++++++++++++ .../findUsages/wrongIdFormat/wrongIdFormat.kt | 14 +++++++++++++ .../AndroidFindUsagesTestGenerated.java | 6 ++++++ 7 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 plugins/android-idea-plugin/testData/android/findUsages/wrongIdFormat/res/layout/layout.xml create mode 100644 plugins/android-idea-plugin/testData/android/findUsages/wrongIdFormat/wrongIdFormat.kt diff --git a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt index 0dd49786e3e..7645c6a2b9b 100644 --- a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt +++ b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt @@ -32,14 +32,12 @@ public object AndroidConst { val ID_USAGE_PREFIX = "@id/" } -class WrongIdFormat(id: String) : Exception("Id \"$id\" has wrong format") - public fun nameToIdDeclaration(name: String): String = AndroidConst.ID_DECLARATION_PREFIX + name -public fun idToName(id: String): String { +public fun idToName(id: String): String? { return if (isResourceIdDeclaration(id)) id.replace(AndroidConst.ID_DECLARATION_PREFIX, "") else if (isResourceIdUsage(id)) id.replace(AndroidConst.ID_USAGE_PREFIX, "") - else throw WrongIdFormat(id) + else null } public fun isResourceIdDeclaration(str: String?): Boolean = str?.startsWith(AndroidConst.ID_DECLARATION_PREFIX) ?: false diff --git a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidXmlHandler.kt b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidXmlHandler.kt index c6f4ae33584..766f6c3796a 100644 --- a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidXmlHandler.kt +++ b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidXmlHandler.kt @@ -37,7 +37,10 @@ class AndroidXmlHandler( val attributesMap = attributes.toMap() val idAttr = attributesMap[AndroidConst.ID_ATTRIBUTE_NO_NAMESPACE] val classNameAttr = attributesMap[AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE] ?: localName - if (isResourceDeclarationOrUsage(idAttr)) elementCallback(idToName(idAttr), classNameAttr) + if (isResourceDeclarationOrUsage(idAttr)) { + val name = idToName(idAttr) + if (name != null) elementCallback(name, classNameAttr) + } } override fun endElement(uri: String?, localName: String, qName: String) { diff --git a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt index bf3e2c0f6e3..1594259e84b 100644 --- a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt +++ b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt @@ -129,7 +129,9 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() { if (element == null) return val oldPropName = AndroidResourceUtil.getResourceNameByReferenceText(attribute.getValue()) val newPropName = idToName(newName) - renameSyntheticProperties(allRenames, newPropName, oldPropName, processor) + if (oldPropName != null && newPropName != null) { + renameSyntheticProperties(allRenames, newPropName, oldPropName, processor) + } } private fun renameSyntheticProperties( diff --git a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidXmlVisitor.kt b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidXmlVisitor.kt index 12cc9723cbb..683847fe67f 100644 --- a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidXmlVisitor.kt +++ b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidXmlVisitor.kt @@ -45,7 +45,8 @@ class AndroidXmlVisitor( val attributeValue = attribute.getValue() if (attributeValue != null) { val classNameAttr = tag?.getAttribute(AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE)?.getValue() ?: tag?.getLocalName() - elementCallback(idToName(attributeValue), classNameAttr!!, attribute) + val name = idToName(attributeValue) + if (name != null) elementCallback(name, classNameAttr!!, attribute) } } tag?.acceptChildren(this) diff --git a/plugins/android-idea-plugin/testData/android/findUsages/wrongIdFormat/res/layout/layout.xml b/plugins/android-idea-plugin/testData/android/findUsages/wrongIdFormat/res/layout/layout.xml new file mode 100644 index 00000000000..d50161eea7d --- /dev/null +++ b/plugins/android-idea-plugin/testData/android/findUsages/wrongIdFormat/res/layout/layout.xml @@ -0,0 +1,20 @@ + + +