From 28e9fbf9b880cb82794f62497fadaa2a01bd6da8 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 10 Jul 2015 18:55:18 +0300 Subject: [PATCH] Correct synthetic extensions for methods like "getURL" --- .../synthetic/SyntheticExtensionsScope.kt | 19 +++++++++++++------ .../syntheticExtensions/AbbreviationName.kt | 13 +++++++++++++ .../syntheticExtensions/AbbreviationName.txt | 12 ++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++++ .../common/extensions/SyntheticExtensions2.kt | 8 ++++++-- 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.kt create mode 100644 compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.txt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SyntheticExtensionsScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SyntheticExtensionsScope.kt index 000a35d8337..c55ea8fa21e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SyntheticExtensionsScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SyntheticExtensionsScope.kt @@ -17,7 +17,6 @@ package org.jetbrains.kotlin.synthetic import com.intellij.util.SmartList -import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl @@ -35,7 +34,8 @@ import org.jetbrains.kotlin.types.typeUtil.isBoolean import org.jetbrains.kotlin.types.typeUtil.isUnit import org.jetbrains.kotlin.types.typeUtil.makeNotNullable import org.jetbrains.kotlin.utils.addIfNotNull -import java.util.* +import java.beans.Introspector +import java.util.ArrayList interface SyntheticExtensionPropertyDescriptor : PropertyDescriptor { val getMethod: FunctionDescriptor @@ -64,7 +64,7 @@ interface SyntheticExtensionPropertyDescriptor : PropertyDescriptor { if (methodName.isSpecial()) return null val identifier = methodName.getIdentifier() if (!identifier.startsWith(prefix)) return null - val name = identifier.removePrefix(prefix).decapitalize() + val name = Introspector.decapitalize(identifier.removePrefix(prefix)) if (!Name.isValidIdentifier(name)) return null return Name.identifier(name) } @@ -84,8 +84,16 @@ class SyntheticExtensionsScope(storageManager: StorageManager) : JetScope by Jet private fun syntheticPropertyInClassNotCached(javaClass: JavaClassDescriptor, type: JetType, name: Name): PropertyDescriptor? { if (name.isSpecial()) return null - val firstChar = name.getIdentifier()[0] - if (!firstChar.isJavaIdentifierStart() || firstChar.isUpperCase()) return null + val identifier = name.getIdentifier() + if (identifier.isEmpty()) return null + val firstChar = identifier[0] + if (!firstChar.isJavaIdentifierStart()) return null + if (identifier.length() > 1) { + if (firstChar.isUpperCase() != identifier[1].isUpperCase()) return null + } + else { + if (firstChar.isUpperCase()) return null + } val memberScope = javaClass.getMemberScope(type.getArguments()) val getMethod = possibleGetMethodNames(name) @@ -165,7 +173,6 @@ class SyntheticExtensionsScope(storageManager: StorageManager) : JetScope by Jet return list } - //TODO: methods like "getURL"? //TODO: reuse code with generation? private fun possibleGetMethodNames(propertyName: Name): Collection { diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.kt b/compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.kt new file mode 100644 index 00000000000..b1b397fe5f9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.kt @@ -0,0 +1,13 @@ +// FILE: KotlinFile.kt +fun foo(javaClass: JavaClass) { + javaClass.URL = javaClass.URL + "/" + + javaClass.url + javaClass.uRL +} + +// FILE: JavaClass.java +public class JavaClass { + public String getURL() { return true; } + public void setURL(String value) { } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.txt b/compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.txt new file mode 100644 index 00000000000..c2731564e6d --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.txt @@ -0,0 +1,12 @@ +package + +internal fun foo(/*0*/ javaClass: JavaClass): kotlin.Unit + +public open class JavaClass { + public constructor JavaClass() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open fun getURL(): kotlin.String! + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open fun setURL(/*0*/ value: kotlin.String!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 36c115ecd15..c0dda0e8434 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -14006,6 +14006,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class SyntheticExtensions extends AbstractJetDiagnosticsTest { + @TestMetadata("AbbreviationName.kt") + public void testAbbreviationName() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/syntheticExtensions/AbbreviationName.kt"); + doTest(fileName); + } + public void testAllFilesPresentInSyntheticExtensions() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/syntheticExtensions"), Pattern.compile("^(.+)\\.kt$"), true); } diff --git a/idea/idea-completion/testData/basic/common/extensions/SyntheticExtensions2.kt b/idea/idea-completion/testData/basic/common/extensions/SyntheticExtensions2.kt index a85c4f8780b..7a340e36334 100644 --- a/idea/idea-completion/testData/basic/common/extensions/SyntheticExtensions2.kt +++ b/idea/idea-completion/testData/basic/common/extensions/SyntheticExtensions2.kt @@ -1,10 +1,14 @@ -fun Thread.foo() { - +fun Thread.foo(urlConnection: java.net.URLConnection) { + with (urlConnection) { + + } } // EXIST_JAVA_ONLY: { lookupString: "priority", itemText: "priority", tailText: " for Thread", typeText: "Int" } // EXIST_JAVA_ONLY: { lookupString: "daemon", itemText: "daemon", tailText: " for Thread", typeText: "Boolean" } +// EXIST_JAVA_ONLY: { lookupString: "URL", itemText: "URL", tailText: " for URLConnection", typeText: "URL!" } // ABSENT: getPriority // ABSENT: setPriority // ABSENT: isDaemon // ABSENT: setDaemon +// ABSENT: getURL