Support member extension properties, implement KClass.getExtensionProperties()
#KT-6570 Fixed
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
public class J extends K {
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.*
|
||||
|
||||
public open class K {
|
||||
var prop: String = ":("
|
||||
|
||||
val Int.ext: Int get() = this
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val j = J()
|
||||
|
||||
val prop = J::prop
|
||||
if (prop !is KMutableMemberProperty<*, *>) return "Fail instanceof"
|
||||
if (prop.name != "prop") return "Fail name: ${prop.name}"
|
||||
if (prop.get(j) != ":(") return "Fail get before: ${prop[j]}"
|
||||
prop[j] = ":)"
|
||||
if (prop.get(j) != ":)") return "Fail get after: ${prop[j]}"
|
||||
|
||||
|
||||
if (prop == K::prop) return "Fail J::prop == K::prop (these are different properties)"
|
||||
|
||||
|
||||
val klass = javaClass<J>().kotlin
|
||||
val prop2 = klass.getProperties().firstOrNull { it.name == "prop" }
|
||||
?: "Fail: no 'prop' property in getProperties()"
|
||||
if (prop != prop2) return "Fail: property references from :: and from getProperties() differ"
|
||||
if (prop2 !is KMutableMemberProperty<*, *>) return "Fail instanceof 2"
|
||||
(prop2 as KMutableMemberProperty<J, String>).set(j, "::)")
|
||||
if (prop.get(j) != "::)") return "Fail get after 2: ${prop[j]}"
|
||||
|
||||
|
||||
val ext = klass.getExtensionProperties().firstOrNull { it.name == "ext" }
|
||||
?: "Fail: no 'ext' property in getProperties()"
|
||||
ext as KMemberExtensionProperty<J, Int, Int>
|
||||
val fortyTwo = ext.get(j, 42)
|
||||
if (fortyTwo != 42) return "Fail ext get: $fortyTwo"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import kotlin.reflect.jvm.kotlin
|
||||
|
||||
class A {
|
||||
var String.id: String
|
||||
get() = this
|
||||
set(value) {}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val p = javaClass<A>().kotlin.getExtensionProperties().single()
|
||||
return if ("$p" == "var A.(kotlin.String.)id") "OK" else "Fail $p"
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
import kotlin.reflect.jvm.kotlin
|
||||
import kotlin.reflect.KMutableMemberExtensionProperty
|
||||
|
||||
var storage = "before"
|
||||
|
||||
class A {
|
||||
val String.readonly: String
|
||||
get() = this
|
||||
|
||||
var String.mutable: String
|
||||
get() = storage
|
||||
set(value) { storage = value }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val props = javaClass<A>().kotlin.getExtensionProperties()
|
||||
val readonly = props.single { it.name == "readonly" }
|
||||
assert(readonly !is KMutableMemberExtensionProperty<A, *, *>) { "Fail 1: $readonly" }
|
||||
val mutable = props.single { it.name == "mutable" }
|
||||
assert(mutable is KMutableMemberExtensionProperty<A, *, *>) { "Fail 2: $mutable" }
|
||||
|
||||
val a = A()
|
||||
mutable as KMutableMemberExtensionProperty<A, String, String>
|
||||
assert(mutable[a, ""] == "before") { "Fail 3: ${mutable.get(a, "")}" }
|
||||
mutable[a, ""] = "OK"
|
||||
return mutable.get(a, "")
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import kotlin.reflect.jvm.kotlin
|
||||
import kotlin.reflect.KMemberProperty
|
||||
import kotlin.reflect.KMemberExtensionProperty
|
||||
|
||||
class A {
|
||||
val foo: String = "member"
|
||||
val Unit.foo: String get() = "extension"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
run {
|
||||
val foo: KMemberProperty<A, *> = javaClass<A>().kotlin.getProperties().single()
|
||||
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
|
||||
assert(foo.get(A()) == "member") { "Fail value: ${foo[A()]}" }
|
||||
}
|
||||
|
||||
run {
|
||||
val foo: KMemberExtensionProperty<A, *, *> = javaClass<A>().kotlin.getExtensionProperties().single()
|
||||
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
|
||||
foo as KMemberExtensionProperty<A, Unit, *>
|
||||
assert(foo.get(A(), Unit) == "extension") { "Fail value: ${foo[A(), Unit]}" }
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+19
@@ -35,6 +35,7 @@ import java.util.regex.Pattern;
|
||||
BlackBoxWithJavaCodegenTestGenerated.NotNullAssertions.class,
|
||||
BlackBoxWithJavaCodegenTestGenerated.PlatformStatic.class,
|
||||
BlackBoxWithJavaCodegenTestGenerated.Properties.class,
|
||||
BlackBoxWithJavaCodegenTestGenerated.Reflection.class,
|
||||
})
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
@@ -162,4 +163,22 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/boxWithJava/reflection")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@InnerTestClasses({
|
||||
})
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Reflection extends AbstractBlackBoxCodegenTest {
|
||||
public void testAllFilesPresentInReflection() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithJava/reflection"), Pattern.compile("^([^\\.]+)$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinPropertyInheritedInJava")
|
||||
public void testKotlinPropertyInheritedInJava() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/reflection/kotlinPropertyInheritedInJava/");
|
||||
doTestWithJava(fileName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+18
@@ -2763,6 +2763,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberExtensionToString.kt")
|
||||
public void testMemberExtensionToString() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/memberExtensionToString.kt");
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("packageForJavaStaticToString.kt")
|
||||
public void testPackageForJavaStaticToString() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/methodsFromAny/packageForJavaStaticToString.kt");
|
||||
@@ -2802,12 +2808,24 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("getExtensionPropertiesMutableVsReadonly.kt")
|
||||
public void testGetExtensionPropertiesMutableVsReadonly() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/properties/getExtensionPropertiesMutableVsReadonly.kt");
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("getPropertiesMutableVsReadonly.kt")
|
||||
public void testGetPropertiesMutableVsReadonly() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/properties/getPropertiesMutableVsReadonly.kt");
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberAndMemberExtensionWithSameName.kt")
|
||||
public void testMemberAndMemberExtensionWithSameName() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/properties/memberAndMemberExtensionWithSameName.kt");
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simpleGetProperties.kt")
|
||||
public void testSimpleGetProperties() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/properties/simpleGetProperties.kt");
|
||||
|
||||
@@ -19,10 +19,7 @@ package kotlin.reflect.jvm.internal
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||
import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KMemberProperty
|
||||
import kotlin.reflect.KMutableMemberProperty
|
||||
import kotlin.reflect.KotlinReflectionInternalError
|
||||
import kotlin.reflect.*
|
||||
|
||||
class KClassImpl<T>(override val jClass: Class<T>) : KCallableContainerImpl(), KClass<T> {
|
||||
// Don't use kotlin.properties.Delegates here because it's a Kotlin class which will invoke KClassImpl() in <clinit>,
|
||||
@@ -54,6 +51,19 @@ class KClassImpl<T>(override val jClass: Class<T>) : KCallableContainerImpl(), K
|
||||
.toList()
|
||||
}
|
||||
|
||||
override fun getExtensionProperties(): Collection<KMemberExtensionProperty<T, *, *>> {
|
||||
return scope.getAllDescriptors().stream()
|
||||
.filterIsInstance<PropertyDescriptor>()
|
||||
.filter { descriptor ->
|
||||
descriptor.getExtensionReceiverParameter() != null
|
||||
}
|
||||
.map { descriptor ->
|
||||
if (descriptor.isVar()) KMutableMemberExtensionPropertyImpl<T, Any?, Any?>(this) { descriptor }
|
||||
else KMemberExtensionPropertyImpl<T, Any?, Any?>(this) { descriptor }
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
|
||||
fun memberProperty(name: String): KMemberProperty<T, *> {
|
||||
return KMemberPropertyImpl<T, Any>(this, findPropertyDescriptor(name))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* 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 kotlin.reflect.jvm.internal
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import java.lang.reflect.Field
|
||||
import java.lang.reflect.Method
|
||||
import kotlin.reflect.IllegalPropertyAccessException
|
||||
import kotlin.reflect.KMemberExtensionProperty
|
||||
import kotlin.reflect.KMutableMemberExtensionProperty
|
||||
|
||||
open class KMemberExtensionPropertyImpl<D : Any, E, out R>(
|
||||
override val container: KClassImpl<D>,
|
||||
computeDescriptor: () -> PropertyDescriptor
|
||||
) : DescriptorBasedProperty(computeDescriptor), KMemberExtensionProperty<D, E, R>, KPropertyImpl<R> {
|
||||
override val name: String get() = descriptor.getName().asString()
|
||||
|
||||
override val getter: Method get() = super<DescriptorBasedProperty>.getter!!
|
||||
|
||||
override val field: Field? get() = null
|
||||
|
||||
override fun get(dispatchReceiver: D, extensionReceiver: E): R {
|
||||
try {
|
||||
[suppress("UNCHECKED_CAST")]
|
||||
return getter.invoke(dispatchReceiver, extensionReceiver) as R
|
||||
}
|
||||
catch (e: IllegalAccessException) {
|
||||
throw IllegalPropertyAccessException(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is KMemberExtensionPropertyImpl<*, *, *> && descriptor == other.descriptor
|
||||
|
||||
override fun hashCode(): Int =
|
||||
descriptor.hashCode()
|
||||
|
||||
override fun toString(): String =
|
||||
ReflectionObjectRenderer.renderProperty(descriptor)
|
||||
}
|
||||
|
||||
|
||||
class KMutableMemberExtensionPropertyImpl<D : Any, E, R>(
|
||||
container: KClassImpl<D>,
|
||||
computeDescriptor: () -> PropertyDescriptor
|
||||
) : KMemberExtensionPropertyImpl<D, E, R>(container, computeDescriptor), KMutableMemberExtensionProperty<D, E, R>, KMutablePropertyImpl<R> {
|
||||
override val setter: Method get() = super<KMemberExtensionPropertyImpl>.setter!!
|
||||
|
||||
override fun set(dispatchReceiver: D, extensionReceiver: E, value: R) {
|
||||
try {
|
||||
setter.invoke(dispatchReceiver, extensionReceiver, value)
|
||||
}
|
||||
catch (e: IllegalAccessException) {
|
||||
throw IllegalPropertyAccessException(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,4 +18,6 @@ package kotlin.reflect
|
||||
|
||||
public trait KClass<T> {
|
||||
public fun getProperties(): Collection<KMemberProperty<T, *>>
|
||||
|
||||
public fun getExtensionProperties(): Collection<KMemberExtensionProperty<T, *, *>>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* 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 kotlin.reflect
|
||||
|
||||
public trait KMemberExtensionProperty<D : Any, E, out R> : KProperty<R> {
|
||||
public fun get(dispatchReceiver: D, extensionReceiver: E): R
|
||||
}
|
||||
|
||||
public trait KMutableMemberExtensionProperty<D : Any, E, R> : KMemberExtensionProperty<D, E, R>, KMutableProperty<R> {
|
||||
public fun set(dispatchReceiver: D, extensionReceiver: E, value: R)
|
||||
}
|
||||
Reference in New Issue
Block a user