Simplify property hierarchy in reflection

Leave only 3*2 = 6 classes: KProperty0, KProperty1, KProperty2 and their
mutable analogs, depending on the number of receivers a property takes
This commit is contained in:
Alexander Udalov
2015-06-26 16:40:39 +03:00
parent c3b97e0668
commit 30794060a9
58 changed files with 374 additions and 653 deletions
@@ -2796,14 +2796,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
if (receiverParameter != null) {
Type[] parameterTypes = new Type[] {JAVA_STRING_TYPE, K_PACKAGE_TYPE, getType(Class.class)};
factoryMethod = descriptor.isVar()
? method("mutableTopLevelExtensionProperty", K_MUTABLE_TOP_LEVEL_EXTENSION_PROPERTY_TYPE, parameterTypes)
: method("topLevelExtensionProperty", K_TOP_LEVEL_EXTENSION_PROPERTY_TYPE, parameterTypes);
? method("mutableTopLevelExtensionProperty", K_MUTABLE_PROPERTY1_TYPE, parameterTypes)
: method("topLevelExtensionProperty", K_PROPERTY1_TYPE, parameterTypes);
}
else {
Type[] parameterTypes = new Type[] {JAVA_STRING_TYPE, K_PACKAGE_TYPE};
factoryMethod = descriptor.isVar()
? method("mutableTopLevelVariable", K_MUTABLE_TOP_LEVEL_VARIABLE_TYPE, parameterTypes)
: method("topLevelVariable", K_TOP_LEVEL_VARIABLE_TYPE, parameterTypes);
? method("mutableTopLevelVariable", K_MUTABLE_PROPERTY0_TYPE, parameterTypes)
: method("topLevelVariable", K_PROPERTY0_TYPE, parameterTypes);
}
return StackValue.operation(factoryMethod.getReturnType(), new Function1<InstructionAdapter, Unit>() {
@@ -2828,8 +2828,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
@NotNull final ClassDescriptor containingClass
) {
final Method factoryMethod = descriptor.isVar()
? method("mutableMemberProperty", K_MUTABLE_MEMBER_PROPERTY_TYPE, JAVA_STRING_TYPE, K_CLASS_TYPE)
: method("memberProperty", K_MEMBER_PROPERTY_TYPE, JAVA_STRING_TYPE, K_CLASS_TYPE);
? method("mutableMemberProperty", K_MUTABLE_PROPERTY1_TYPE, JAVA_STRING_TYPE, K_CLASS_TYPE)
: method("memberProperty", K_PROPERTY1_TYPE, JAVA_STRING_TYPE, K_CLASS_TYPE);
return StackValue.operation(factoryMethod.getReturnType(), new Function1<InstructionAdapter, Unit>() {
@Override
@@ -44,12 +44,10 @@ public class AsmTypes {
public static final Type K_FUNCTION = reflect("KFunction");
public static final Type K_MEMBER_PROPERTY_TYPE = reflect("KMemberProperty");
public static final Type K_MUTABLE_MEMBER_PROPERTY_TYPE = reflect("KMutableMemberProperty");
public static final Type K_TOP_LEVEL_VARIABLE_TYPE = reflect("KTopLevelVariable");
public static final Type K_MUTABLE_TOP_LEVEL_VARIABLE_TYPE = reflect("KMutableTopLevelVariable");
public static final Type K_TOP_LEVEL_EXTENSION_PROPERTY_TYPE = reflect("KTopLevelExtensionProperty");
public static final Type K_MUTABLE_TOP_LEVEL_EXTENSION_PROPERTY_TYPE = reflect("KMutableTopLevelExtensionProperty");
public static final Type K_PROPERTY0_TYPE = reflect("KProperty0");
public static final Type K_PROPERTY1_TYPE = reflect("KProperty1");
public static final Type K_MUTABLE_PROPERTY0_TYPE = reflect("KMutableProperty0");
public static final Type K_MUTABLE_PROPERTY1_TYPE = reflect("KMutableProperty1");
public static final String REFLECTION = "kotlin/jvm/internal/Reflection";
@@ -694,20 +694,15 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
return null;
}
JetType receiverType = null;
if (extensionReceiver != null) {
receiverType = extensionReceiver.getType();
}
else if (dispatchReceiver != null) {
receiverType = dispatchReceiver.getType();
}
boolean isExtension = extensionReceiver != null;
JetType receiverType = extensionReceiver != null ? extensionReceiver.getType() :
dispatchReceiver != null ? dispatchReceiver.getType() :
null;
if (descriptor instanceof FunctionDescriptor) {
return createFunctionReferenceType(expression, context, (FunctionDescriptor) descriptor, receiverType);
}
else if (descriptor instanceof PropertyDescriptor) {
return createPropertyReferenceType(expression, context, (PropertyDescriptor) descriptor, receiverType, isExtension);
return createPropertyReferenceType(expression, context, (PropertyDescriptor) descriptor, receiverType);
}
else if (descriptor instanceof VariableDescriptor) {
context.trace.report(UNSUPPORTED.on(reference, "References to variables aren't supported yet"));
@@ -751,11 +746,11 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
@NotNull JetCallableReferenceExpression expression,
@NotNull ExpressionTypingContext context,
@NotNull PropertyDescriptor descriptor,
@Nullable JetType receiverType,
boolean isExtension
@Nullable JetType receiverType
) {
JetType type = components.reflectionTypes.getKPropertyType(Annotations.EMPTY, receiverType, descriptor.getType(), isExtension,
descriptor.isVar());
JetType type = components.reflectionTypes.getKPropertyType(
Annotations.EMPTY, receiverType, descriptor.getType(), descriptor.isVar()
);
LocalVariableDescriptor localVariable =
new LocalVariableDescriptor(context.scope.getContainingDeclaration(), Annotations.EMPTY, Name.special("<anonymous>"),
@@ -10,8 +10,8 @@ fun box(): String {
val s = J::s
// Check that correct reflection objects are created
assert(i.javaClass.getSimpleName() == "KMemberPropertyImpl", "Fail i class")
assert(s.javaClass.getSimpleName() == "KMutableMemberPropertyImpl", "Fail s class")
assert(i.javaClass.getSimpleName() == "KProperty1Impl", "Fail i class")
assert(s.javaClass.getSimpleName() == "KMutableProperty1Impl", "Fail s class")
// Check that no Method objects are created for such properties
assert(i.javaGetter == null, "Fail i getter")
@@ -33,8 +33,8 @@ fun box(): String {
assert(a.s == "def", "Fail js access")
// Check that valid Kotlin reflection objects are created by those Field objects
val ki = ji.kotlin as KMemberProperty<J, Int>
val ks = js.kotlin as KMutableMemberProperty<J, String>
val ki = ji.kotlin as KProperty1<J, Int>
val ks = js.kotlin as KMutableProperty1<J, String>
assert(ki.get(a) == 42, "Fail ki get")
assert(ks.get(a) == "def", "Fail ks get")
ks.set(a, "ghi")
@@ -11,7 +11,7 @@ fun box(): String {
val j = J()
val prop = J::prop
if (prop !is KMutableMemberProperty<*, *>) return "Fail instanceof"
if (prop !is KMutableProperty1<*, *>) return "Fail instanceof"
if (prop.name != "prop") return "Fail name: ${prop.name}"
if (prop.get(j) != ":(") return "Fail get before: ${prop[j]}"
prop[j] = ":)"
@@ -27,13 +27,13 @@ fun box(): String {
val prop2 = klass.properties.firstOrNull { it.name == "prop" } ?: "Fail: no 'prop' property in properties"
if (prop != prop2) return "Fail: property references from :: and from properties differ: $prop != $prop2"
if (prop2 !is KMutableMemberProperty<*, *>) return "Fail instanceof 2"
(prop2 as KMutableMemberProperty<J, String>).set(j, "::)")
if (prop2 !is KMutableProperty1<*, *>) return "Fail instanceof 2"
(prop2 as KMutableProperty1<J, String>).set(j, "::)")
if (prop.get(j) != "::)") return "Fail get after 2: ${prop[j]}"
val ext = klass.extensionProperties.firstOrNull { it.name == "ext" } ?: "Fail: no 'ext' property in extensionProperties"
ext as KMemberExtensionProperty<J, Int, Int>
ext as KProperty2<J, Int, Int>
val fortyTwo = ext.get(j, 42)
if (fortyTwo != 42) return "Fail ext get: $fortyTwo"
@@ -1,6 +1,6 @@
//For KT-6020
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KMutableMemberProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.KMutableProperty1
class Value<T>(var value: T = null as T, var text: String? = null)
@@ -8,7 +8,7 @@ val <T> Value<T>.additionalText by DVal(Value<T>::text) //works
val <T> Value<T>.additionalValue by DVal(Value<T>::value) //not work
class DVal<T, R, P: KMemberProperty<T, R>>(val kmember: P) {
class DVal<T, R, P: KProperty1<T, R>>(val kmember: P) {
fun get(t: T, p: PropertyMetadata): R {
return kmember.get(t)
}
@@ -17,4 +17,4 @@ class DVal<T, R, P: KMemberProperty<T, R>>(val kmember: P) {
fun box(): String {
val p = Value("O", "K")
return p.additionalValue + p.additionalText
}
}
@@ -1,8 +1,8 @@
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class A {
companion object {
val ref: KMemberProperty<A, String> = A::foo
val ref: KProperty1<A, String> = A::foo
}
val foo: String = "OK"
@@ -1,11 +1,11 @@
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.accessible
class Result {
private val value = "OK"
fun ref(): KMemberProperty<Result, String> = ::value
fun ref(): KProperty1<Result, String> = ::value
}
fun box(): String {
@@ -1,11 +1,11 @@
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMutableMemberProperty
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.jvm.accessible
class A {
private var value = 0
fun ref(): KMutableMemberProperty<A, Int> = ::value
fun ref(): KMutableProperty1<A, Int> = ::value
}
fun box(): String {
@@ -4,7 +4,7 @@ object A {
val b: String = "OK"
platformStatic val c: String = "OK"
platformStatic var c: String = "Fail"
platformStatic fun test1() : String {
return b
@@ -36,6 +36,8 @@ fun box(): String {
if ((A::test4)(A) != "1OK") return "fail 4"
(A::c).set(A, "OK")
if (((A::c).get(A)) != "OK") return "fail 5"
return "OK"
@@ -1,7 +1,7 @@
import kotlin.reflect.KExtensionProperty
import kotlin.reflect.KProperty1
import kotlin.test.assertEquals
fun check(expected: String, p: KExtensionProperty<*, *>) {
fun check(expected: String, p: KProperty1<*, *>) {
var s = p.toString()
// Strip "val" or "var"
@@ -4,7 +4,7 @@ import kotlin.reflect.jvm.*
class K(private val value: String)
fun box(): String {
val p = javaClass<K>().kotlin.properties.single() as KMemberProperty<K, String>
val p = javaClass<K>().kotlin.properties.single() as KProperty1<K, String>
try {
return p.get(K("Fail: private property should not be accessible by default"))
@@ -1,11 +1,10 @@
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class A<T> {
val result = "OK"
}
fun box(): String {
val k = A::class.properties.single()
k : KMemberProperty<A<*>, *>
val k : KProperty1<A<*>, *> = A::class.properties.single()
return k.get(A<String>()) as String
}
@@ -1,5 +1,5 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMutableMemberExtensionProperty
import kotlin.reflect.KMutableProperty2
var storage = "before"
@@ -15,12 +15,12 @@ class A {
fun box(): String {
val props = javaClass<A>().kotlin.extensionProperties
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableMemberExtensionProperty<A, *, *>) { "Fail 1: $readonly" }
assert(readonly !is KMutableProperty2<A, *, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableMemberExtensionProperty<A, *, *>) { "Fail 2: $mutable" }
assert(mutable is KMutableProperty2<A, *, *>) { "Fail 2: $mutable" }
val a = A()
mutable as KMutableMemberExtensionProperty<A, String, String>
mutable as KMutableProperty2<A, String, String>
assert(mutable[a, ""] == "before") { "Fail 3: ${mutable.get(a, "")}" }
mutable[a, ""] = "OK"
return mutable.get(a, "")
@@ -1,5 +1,5 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMutableMemberProperty
import kotlin.reflect.KMutableProperty1
class A(val readonly: String) {
var mutable: String = "before"
@@ -8,12 +8,12 @@ class A(val readonly: String) {
fun box(): String {
val props = javaClass<A>().kotlin.properties
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableMemberProperty<A, *>) { "Fail 1: $readonly" }
assert(readonly !is KMutableProperty1<A, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableMemberProperty<A, *>) { "Fail 2: $mutable" }
assert(mutable is KMutableProperty1<A, *>) { "Fail 2: $mutable" }
val a = A("")
mutable as KMutableMemberProperty<A, String>
mutable as KMutableProperty1<A, String>
assert(mutable[a] == "before") { "Fail 3: ${mutable.get(a)}" }
mutable[a] = "OK"
return mutable.get(a)
@@ -1,6 +1,6 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KMemberExtensionProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.KProperty2
class A {
val foo: String = "member"
@@ -9,15 +9,15 @@ class A {
fun box(): String {
run {
val foo: KMemberProperty<A, *> = javaClass<A>().kotlin.properties.single()
val foo: KProperty1<A, *> = javaClass<A>().kotlin.properties.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.extensionProperties.single()
val foo: KProperty2<A, *, *> = javaClass<A>().kotlin.extensionProperties.single()
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
foo as KMemberExtensionProperty<A, Unit, *>
foo as KProperty2<A, Unit, *>
assert(foo.get(A(), Unit) == "extension") { "Fail value: ${foo[A(), Unit]}" }
}
@@ -1,6 +1,6 @@
// !CHECK_TYPE
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
interface Base {
val x: Any
@@ -20,17 +20,17 @@ class C : B() {
fun test() {
val base = Base::x
checkSubtype<KMemberProperty<Base, Any>>(base)
checkSubtype<KProperty1<Base, Any>>(base)
checkSubtype<Any>(base.get(A()))
checkSubtype<Number>(<!TYPE_MISMATCH!>base.get(B())<!>)
checkSubtype<Int>(<!TYPE_MISMATCH!>base.get(C())<!>)
val a = A::x
checkSubtype<KMemberProperty<A, String>>(a)
checkSubtype<KProperty1<A, String>>(a)
checkSubtype<String>(a.get(A()))
checkSubtype<Number>(<!TYPE_MISMATCH!>a.get(<!TYPE_MISMATCH!>B()<!>)<!>)
val b = B::x
checkSubtype<KMemberProperty<B, Number>>(b)
checkSubtype<KProperty1<B, Number>>(b)
checkSubtype<Int>(<!TYPE_MISMATCH!>b.get(C())<!>)
}
@@ -1,6 +1,6 @@
// !CHECK_TYPE
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
open class Base {
val foo: Int = 42
@@ -10,6 +10,6 @@ open class Derived : Base()
fun test() {
val o = Base::foo
checkSubtype<KMemberProperty<Base, Int>>(o)
checkSubtype<KProperty1<Base, Int>>(o)
checkSubtype<Int>(o.get(Derived()))
}
@@ -6,7 +6,7 @@ class A(var g: A) {
val f: Int = 0
fun test() {
val fRef: KMemberProperty<A, Int> = ::f
val gRef: KMutableMemberProperty<A, A> = ::g
val fRef: KProperty1<A, Int> = ::f
val gRef: KMutableProperty1<A, A> = ::g
}
}
@@ -4,8 +4,8 @@ import kotlin.reflect.*
class A {
fun test() {
val fooRef: KExtensionProperty<A, String> = ::foo
val barRef: KMutableExtensionProperty<A, Int> = ::bar
val fooRef: KProperty1<A, String> = ::foo
val barRef: KMutableProperty1<A, Int> = ::bar
}
}
@@ -12,18 +12,14 @@ var Int.meaning: Long
fun test() {
val f = String::countCharacters
checkSubtype<KTopLevelExtensionProperty<String, Int>>(f)
checkSubtype<KExtensionProperty<String, Int>>(f)
checkSubtype<KMutableExtensionProperty<String, Int>>(<!TYPE_MISMATCH!>f<!>)
checkSubtype<KProperty1<String, Int>>(f)
checkSubtype<KMutableProperty1<String, Int>>(<!TYPE_MISMATCH!>f<!>)
checkSubtype<Int>(f.get("abc"))
f.<!UNRESOLVED_REFERENCE!>set<!>("abc", 0)
val g = Int::meaning
checkSubtype<KTopLevelExtensionProperty<Int, Long>>(g)
checkSubtype<KExtensionProperty<Int, Long>>(g)
checkSubtype<KMutableTopLevelExtensionProperty<Int, Long>>(g)
checkSubtype<KMutableExtensionProperty<Int, Long>>(g)
checkSubtype<KMutableProperty1<Int, Long>>(g)
checkSubtype<Long>(g.get(0))
g.set(1, 0L)
}
@@ -1,6 +1,6 @@
// !CHECK_TYPE
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class A<T>(val t: T) {
val foo: T = t
@@ -8,9 +8,9 @@ class A<T>(val t: T) {
fun bar() {
val x = A<String>::foo
checkSubtype<KMemberProperty<A<String>, String>>(x)
checkSubtype<KMemberProperty<A<String>, Any?>>(x)
checkSubtype<KProperty1<A<String>, String>>(x)
checkSubtype<KProperty1<A<String>, Any?>>(x)
val y = A<*>::foo
checkSubtype<KMemberProperty<A<*>, Any?>>(y)
checkSubtype<KProperty1<A<*>, Any?>>(y)
}
@@ -17,10 +17,10 @@ public class JavaClass {
import kotlin.reflect.*
fun test() {
val pubFinRef: KMemberProperty<JavaClass, Int> = JavaClass::publicFinal
val pubMutRef: KMutableMemberProperty<JavaClass, Long> = JavaClass::publicMutable
val protFinRef: KMemberProperty<JavaClass, Double> = JavaClass::protectedFinal
val protMutRef: KMutableMemberProperty<JavaClass, Char> = JavaClass::protectedMutable
val privFinRef: KMemberProperty<JavaClass, String?> = JavaClass::<!INVISIBLE_MEMBER!>privateFinal<!>
val privMutRef: KMutableMemberProperty<JavaClass, Any?> = JavaClass::<!INVISIBLE_MEMBER!>privateMutable<!>
val pubFinRef: KProperty1<JavaClass, Int> = JavaClass::publicFinal
val pubMutRef: KMutableProperty1<JavaClass, Long> = JavaClass::publicMutable
val protFinRef: KProperty1<JavaClass, Double> = JavaClass::protectedFinal
val protMutRef: KMutableProperty1<JavaClass, Char> = JavaClass::protectedMutable
val privFinRef: KProperty1<JavaClass, String?> = JavaClass::<!INVISIBLE_MEMBER!>privateFinal<!>
val privMutRef: KMutableProperty1<JavaClass, Any?> = JavaClass::<!INVISIBLE_MEMBER!>privateMutable<!>
}
@@ -1,4 +1,3 @@
// !CHECK_TYPE
// !DIAGNOSTICS:-UNUSED_VARIABLE
// FILE: JavaClass.java
@@ -20,8 +19,8 @@ import JavaClass.*
import kotlin.reflect.*
fun test() {
val pubFinRef: KTopLevelProperty<String> = ::publicFinal
val pubMutRef: KMutableTopLevelProperty<Any?> = ::publicMutable
val pubFinRef: KProperty0<String> = ::publicFinal
val pubMutRef: KMutableProperty0<Any?> = ::publicMutable
val protFinRef: KProperty<Double> = ::protectedFinal
val protMutRef: KMutableProperty<Char> = ::protectedMutable
val privFinRef: KProperty<JavaClass?> = ::<!INVISIBLE_MEMBER!>privateFinal<!>
@@ -6,7 +6,7 @@ class A(var g: A) {
val f: Int = 0
fun test() {
checkSubtype<KMemberProperty<A, Int>>(::f)
checkSubtype<KMutableMemberProperty<A, A>>(::g)
checkSubtype<KProperty1<A, Int>>(::f)
checkSubtype<KMutableProperty1<A, A>>(::g)
}
}
}
@@ -1,10 +1,10 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class TestClass(var prop: Int)
open class OtherClass
fun OtherClass.test(prop: KMemberProperty<TestClass, Int>): Unit = throw Exception()
fun OtherClass.test(prop: KProperty1<TestClass, Int>): Unit = throw Exception()
class OtherClass2: OtherClass() {
val result = test(TestClass::<!UNRESOLVED_REFERENCE!>result<!>)
}
@@ -1,6 +1,6 @@
package
internal fun OtherClass.test(/*0*/ prop: kotlin.reflect.KMemberProperty<TestClass, kotlin.Int>): kotlin.Unit
internal fun OtherClass.test(/*0*/ prop: kotlin.reflect.KProperty1<TestClass, kotlin.Int>): kotlin.Unit
internal open class OtherClass {
public constructor OtherClass()
@@ -15,9 +15,9 @@ fun A.test() {
val y = ::bar
val z = ::self
checkSubtype<KMemberProperty<A, Unit>>(x)
checkSubtype<KMutableMemberProperty<A, String>>(y)
checkSubtype<KMutableMemberProperty<A, A>>(z)
checkSubtype<KProperty1<A, Unit>>(x)
checkSubtype<KMutableProperty1<A, String>>(y)
checkSubtype<KMutableProperty1<A, A>>(z)
y.set(z.get(A()), x.get(A()).toString())
}
@@ -10,16 +10,16 @@ class A {
fun test() {
val p = A::foo
checkSubtype<KMemberProperty<A, Int>>(p)
checkSubtype<KMutableMemberProperty<A, Int>>(<!TYPE_MISMATCH!>p<!>)
checkSubtype<KProperty1<A, Int>>(p)
checkSubtype<KMutableProperty1<A, Int>>(<!TYPE_MISMATCH!>p<!>)
checkSubtype<Int>(p.get(A()))
p.get(<!NO_VALUE_FOR_PARAMETER!>)<!>
p.<!UNRESOLVED_REFERENCE!>set<!>(A(), 239)
val q = A::bar
checkSubtype<KMemberProperty<A, String>>(q)
checkSubtype<KMutableMemberProperty<A, String>>(q)
checkSubtype<KProperty1<A, String>>(q)
checkSubtype<KMutableProperty1<A, String>>(q)
checkSubtype<String>(q.get(A()))
q.set(A(), "q")
}
@@ -1,6 +1,6 @@
// !CHECK_TYPE
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class C {
val baz: Int = 12
@@ -9,5 +9,5 @@ class C {
fun Int.baz() {}
fun test() {
C::baz checkType { _<KMemberProperty<C, Int>>() }
}
C::baz checkType { _<KProperty1<C, Int>>() }
}
@@ -7,12 +7,9 @@ val y: String get() = "y"
fun testX() {
val xx = ::x
checkSubtype<KMutableTopLevelProperty<Int>>(xx)
checkSubtype<KMutableTopLevelVariable<Int>>(xx)
checkSubtype<KTopLevelProperty<Int>>(xx)
checkSubtype<KTopLevelVariable<Int>>(xx)
checkSubtype<KMutableProperty0<Int>>(xx)
checkSubtype<KProperty0<Int>>(xx)
checkSubtype<KMutableProperty<Int>>(xx)
checkSubtype<KMutableVariable<Int>>(xx)
checkSubtype<KProperty<Int>>(xx)
checkSubtype<KCallable<Int>>(xx)
@@ -23,8 +20,8 @@ fun testX() {
fun testY() {
val yy = ::y
checkSubtype<KMutableTopLevelProperty<String>>(<!TYPE_MISMATCH!>yy<!>)
checkSubtype<KTopLevelVariable<String>>(yy)
checkSubtype<KMutableProperty0<String>>(<!TYPE_MISMATCH!>yy<!>)
checkSubtype<KProperty0<String>>(yy)
checkSubtype<KMutableProperty<String>>(<!TYPE_MISMATCH!>yy<!>)
checkSubtype<KProperty<String>>(yy)
checkSubtype<KCallable<String>>(yy)
+2 -2
View File
@@ -41,10 +41,10 @@ public interface KClass<T> : KDeclarationContainer {
/**
* Returns non-extension properties declared in this class and all of its superclasses.
*/
public val properties: Collection<KMemberProperty<T, *>>
public val properties: Collection<KProperty1<T, *>>
/**
* Returns extension properties declared in this class and all of its superclasses.
*/
public val extensionProperties: Collection<KMemberExtensionProperty<T, *, *>>
public val extensionProperties: Collection<KProperty2<T, *, *>>
}
@@ -1,47 +0,0 @@
/*
* 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
/**
* Represents an extension property.
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/extensions.html#extension-properties)
* for more information.
*
* @param E the type of the extension receiver.
* @param R the type of the property.
*/
public interface KExtensionProperty<E, out R> : KProperty<R> {
/**
* Returns the current value of the property.
*
* @param receiver the instance of the extension receiver.
*/
public fun get(receiver: E): R
}
/**
* Represents an extension property declared as a `var`.
*/
public interface KMutableExtensionProperty<E, R> : KExtensionProperty<E, R>, KMutableProperty<R> {
/**
* Modifies the value of the property.
*
* @param receiver the instance of the extension receiver.
* @param value the new value to be assigned to this property.
*/
public fun set(receiver: E, value: R)
}
@@ -1,51 +0,0 @@
/*
* 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
/**
* Represents an extension property declared in a class.
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/extensions.html#extension-properties)
* for more information.
*
* @param T the type of the instance which should be used to obtain the value of the property.
* Must be derived either from a class declaring this property, or any subclass of that class.
* @param E the type of the extension receiver.
* @param R the type of the property.
*/
public interface KMemberExtensionProperty<T : Any, E, out R> : KProperty<R> {
/**
* Returns the current value of the property.
*
* @param instance the instance to obtain the value of the property from.
* @param extensionReceiver the instance of the extension receiver.
*/
public fun get(instance: T, extensionReceiver: E): R
}
/**
* Represents a `var` extension property declared in a class.
*/
public interface KMutableMemberExtensionProperty<T : Any, E, R> : KMemberExtensionProperty<T, E, R>, KMutableProperty<R> {
/**
* Modifies the value of the property.
*
* @param instance the instance to obtain the value of the property from.
* @param extensionReceiver the instance of the extension receiver.
* @param value the new value to be assigned to this property.
*/
public fun set(instance: T, extensionReceiver: E, value: R)
}
@@ -1,46 +0,0 @@
/*
* 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
/**
* Represents a property declared in a class.
*
* @param T the type of the instance which should be used to obtain the value of the property.
* Must be derived either from a class declaring this property, or any subclass of that class.
* @param R the type of the property.
*/
public interface KMemberProperty<T : Any, out R> : KProperty<R> {
/**
* Returns the current value of the property.
*
* @param instance the instance to obtain the value of the property from.
*/
public fun get(instance: T): R
}
/**
* Represents a `var` property declared in a class.
*/
public interface KMutableMemberProperty<T : Any, R> : KMemberProperty<T, R>, KMutableProperty<R> {
/**
* Modifies the value of the property.
*
* @param instance the instance to obtain the value of the property from.
* @param value the new value to be assigned to this property.
*/
public fun set(instance: T, value: R)
}
@@ -30,3 +30,97 @@ public interface KProperty<out R> : KCallable<R>
* Represents a property declared as a `var`.
*/
public interface KMutableProperty<R> : KProperty<R>
/**
* Represents a property without any kind of receiver.
* Such property is either originally declared in a receiverless context such as a package,
* or has the receiver bound to it.
*/
public interface KProperty0<out R> : KProperty<R> {
/**
* Returns the current value of the property.
*/
public fun get(): R
}
/**
* Represents a `var`-property without any kind of receiver.
*/
public interface KMutableProperty0<R> : KProperty0<R>, KMutableProperty<R> {
/**
* Modifies the value of the property.
*
* @param value the new value to be assigned to this property.
*/
public fun set(value: R)
}
/**
* Represents a property, operations on which take one receiver as a parameter.
*
* @param T the type of the receiver which should be used to obtain the value of the property.
* @param R the type of the property.
*/
public interface KProperty1<T, out R> : KProperty<R> {
/**
* Returns the current value of the property.
*
* @param receiver the receiver which is used to obtain the value of the property.
* For example, it should be a class instance if this is a member property of that class,
* or an extension receiver if this is a top level extension property.
*/
public fun get(receiver: T): R
}
/**
* Represents a `var`-property, operations on which take one receiver as a parameter.
*/
public interface KMutableProperty1<T, R> : KProperty1<T, R>, KMutableProperty<R> {
/**
* Modifies the value of the property.
*
* @param receiver the receiver which is used to modify the value of the property.
* For example, it should be a class instance if this is a member property of that class,
* or an extension receiver if this is a top level extension property.
* @param value the new value to be assigned to this property.
*/
public fun set(receiver: T, value: R)
}
/**
* Represents a property, operations on which take two receivers as parameters,
* such as an extension property declared in a class.
*
* @param D the type of the first receiver. In case of the extension property in a class this is
* the type of the declaring class of the property, or any subclass of that class.
* @param E the type of the second receiver. In case of the extension property in a class this is
* the type of the extension receiver.
* @param R the type of the property.
*/
public interface KProperty2<D, E, out R> : KProperty<R> {
/**
* Returns the current value of the property. In case of the extension property in a class,
* the instance of the class should be passed first and the instance of the extension receiver second.
*
* @param receiver1 the instance of the first receiver.
* @param receiver2 the instance of the second receiver.
*/
public fun get(receiver1: D, receiver2: E): R
}
/**
* Represents a `var`-property, operations on which take two receivers as parameters.
*/
public interface KMutableProperty2<D, E, R> : KProperty2<D, E, R>, KMutableProperty<R> {
/**
* Modifies the value of the property.
*
* @param receiver1 the instance of the first receiver.
* @param receiver2 the instance of the second receiver.
* @param value the new value to be assigned to this property.
*/
public fun set(receiver1: D, receiver2: E, value: R)
}
@@ -1,27 +0,0 @@
/*
* 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
/**
* Represents an extension property declared in a package.
*/
public interface KTopLevelExtensionProperty<E, out R> : KExtensionProperty<E, R>, KTopLevelProperty<R>
/**
* Represents a package extension property declared as a `var`.
*/
public interface KMutableTopLevelExtensionProperty<E, R> : KTopLevelExtensionProperty<E, R>, KMutableExtensionProperty<E, R>, KMutableTopLevelProperty<R>
@@ -1,27 +0,0 @@
/*
* 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
/**
* Represents a property declared in a package.
*/
public interface KTopLevelProperty<out R> : KProperty<R>
/**
* Represents a package property declared as a `var`.
*/
public interface KMutableTopLevelProperty<R> : KTopLevelProperty<R>, KMutableProperty<R>
@@ -1,27 +0,0 @@
/*
* 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
/**
* Represents a variable declared in a package.
*/
public interface KTopLevelVariable<out R> : KVariable<R>, KTopLevelProperty<R>
/**
* Represents a package variable declared as a `var`.
*/
public interface KMutableTopLevelVariable<R> : KTopLevelVariable<R>, KMutableVariable<R>, KMutableTopLevelProperty<R>
@@ -1,41 +0,0 @@
/*
* 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
/**
* Represents a property without any kind of receiver.
* Such property is either originally declared in a receiverless context such as a package,
* or has the receiver bound to it.
*/
public interface KVariable<out R> : KProperty<R> {
/**
* Returns the current value of the variable.
*/
public fun get(): R
}
/**
* Represents a variable declared as a `var`.
*/
public interface KMutableVariable<R> : KVariable<R>, KMutableProperty<R> {
/**
* Modifies the value of the variable.
*
* @param value the new value to be assigned to this variable.
*/
public fun set(value: R)
}
@@ -50,12 +50,10 @@ public class ReflectionTypes(private val module: ModuleDescriptor) {
public fun getKFunction(n: Int): ClassDescriptor = find("KFunction$n")
public val kClass: ClassDescriptor by ClassLookup
public val kTopLevelVariable: ClassDescriptor by ClassLookup
public val kMutableTopLevelVariable: ClassDescriptor by ClassLookup
public val kMemberProperty: ClassDescriptor by ClassLookup
public val kMutableMemberProperty: ClassDescriptor by ClassLookup
public val kTopLevelExtensionProperty: ClassDescriptor by ClassLookup
public val kMutableTopLevelExtensionProperty: ClassDescriptor by ClassLookup
public val kProperty0: ClassDescriptor by ClassLookup
public val kProperty1: ClassDescriptor by ClassLookup
public val kMutableProperty0: ClassDescriptor by ClassLookup
public val kMutableProperty1: ClassDescriptor by ClassLookup
public fun getKClassType(annotations: Annotations, type: JetType): JetType {
val descriptor = kClass
@@ -84,23 +82,18 @@ public class ReflectionTypes(private val module: ModuleDescriptor) {
return JetTypeImpl(annotations, classDescriptor.getTypeConstructor(), false, arguments, classDescriptor.getMemberScope(arguments))
}
public fun getKPropertyType(
annotations: Annotations,
receiverType: JetType?,
returnType: JetType,
extensionProperty: Boolean,
mutable: Boolean
): JetType {
val classDescriptor = if (mutable) when {
extensionProperty -> kMutableTopLevelExtensionProperty
receiverType != null -> kMutableMemberProperty
else -> kMutableTopLevelVariable
}
else when {
extensionProperty -> kTopLevelExtensionProperty
receiverType != null -> kMemberProperty
else -> kTopLevelVariable
}
public fun getKPropertyType(annotations: Annotations, receiverType: JetType?, returnType: JetType, mutable: Boolean): JetType {
val classDescriptor =
when {
receiverType != null -> when {
mutable -> kMutableProperty1
else -> kProperty1
}
else -> when {
mutable -> kMutableProperty0
else -> kProperty0
}
}
if (ErrorUtils.isError(classDescriptor)) {
return classDescriptor.getDefaultType()
@@ -21,11 +21,11 @@ import kotlin.reflect.jvm.internal.KClassImpl
/**
* Returns non-extension properties declared in this class.
*/
public val <T> KClass<T>.declaredProperties: Collection<KMemberProperty<T, *>>
public val <T> KClass<T>.declaredProperties: Collection<KProperty1<T, *>>
get() = (this as KClassImpl<T>).getProperties(declared = true)
/**
* Returns extension properties declared in this class.
*/
public val <T> KClass<T>.declaredExtensionProperties: Collection<KMemberExtensionProperty<T, *, *>>
public val <T> KClass<T>.declaredExtensionProperties: Collection<KProperty2<T, *, *>>
get() = (this as KClassImpl<T>).getExtensionProperties(declared = true)
@@ -18,4 +18,4 @@ package kotlin.reflect.jvm.internal
import kotlin.reflect.KCallable
trait KCallableImpl<out R> : KCallable<R>
interface KCallableImpl<out R> : KCallable<R>
@@ -75,22 +75,22 @@ class KClassImpl<T>(override val jClass: Class<T>) : KCallableContainerImpl(), K
}
}
override val properties: Collection<KMemberProperty<T, *>>
override val properties: Collection<KProperty1<T, *>>
get() = getProperties(declared = false)
override val extensionProperties: Collection<KMemberExtensionProperty<T, *, *>>
override val extensionProperties: Collection<KProperty2<T, *, *>>
get() = getExtensionProperties(declared = false)
fun getProperties(declared: Boolean): Collection<KMemberProperty<T, *>> =
fun getProperties(declared: Boolean): Collection<KProperty1<T, *>> =
getProperties(extension = false, declared = declared) { descriptor ->
if (descriptor.isVar()) KMutableMemberPropertyImpl<T, Any?>(this, descriptor)
else KMemberPropertyImpl<T, Any?>(this, descriptor)
if (descriptor.isVar()) KMutableProperty1Impl<T, Any?>(this, descriptor)
else KProperty1Impl<T, Any?>(this, descriptor)
}
fun getExtensionProperties(declared: Boolean): Collection<KMemberExtensionProperty<T, *, *>> =
fun getExtensionProperties(declared: Boolean): Collection<KProperty2<T, *, *>> =
getProperties(extension = true, declared = declared) { descriptor ->
if (descriptor.isVar()) KMutableMemberExtensionPropertyImpl<T, Any?, Any?>(this, descriptor)
else KMemberExtensionPropertyImpl<T, Any?, Any?>(this, descriptor)
if (descriptor.isVar()) KMutableProperty2Impl<T, Any?, Any?>(this, descriptor)
else KProperty2Impl<T, Any?, Any?>(this, descriptor)
}
private fun <P : KProperty<*>> getProperties(extension: Boolean, declared: Boolean, create: (PropertyDescriptor) -> P): Collection<P> =
@@ -104,11 +104,11 @@ class KClassImpl<T>(override val jClass: Class<T>) : KCallableContainerImpl(), K
.map(create)
.toList()
fun memberProperty(name: String): KMemberProperty<T, *> =
KMemberPropertyImpl<T, Any>(this, name)
fun memberProperty(name: String): KProperty1<T, *> =
KProperty1Impl<T, Any>(this, name, null)
fun mutableMemberProperty(name: String): KMutableMemberProperty<T, *> =
KMutableMemberPropertyImpl<T, Any>(this, name)
fun mutableMemberProperty(name: String): KMutableProperty1<T, *> =
KMutableProperty1Impl<T, Any>(this, name, null)
override fun equals(other: Any?): Boolean =
other is KClassImpl<*> && jClass == other.jClass
@@ -1,58 +0,0 @@
/*
* 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 kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KMutableMemberProperty
open class KMemberPropertyImpl<T : Any, out R> : DescriptorBasedProperty, KMemberProperty<T, R>, KPropertyImpl<R> {
constructor(container: KClassImpl<T>, name: String) : super(container, name, null)
constructor(container: KClassImpl<T>, descriptor: PropertyDescriptor) : super(container, descriptor)
override val name: String get() = descriptor.getName().asString()
override fun get(instance: T): R {
try {
val getter = getter
@suppress("UNCHECKED_CAST")
return if (getter != null) getter(instance) as R else field!!.get(instance) as R
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
}
}
}
class KMutableMemberPropertyImpl<T : Any, R> : KMemberPropertyImpl<T, R>, KMutableMemberProperty<T, R>, KMutablePropertyImpl<R> {
constructor(container: KClassImpl<T>, name: String) : super(container, name)
constructor(container: KClassImpl<T>, descriptor: PropertyDescriptor) : super(container, descriptor)
override fun set(instance: T, value: R) {
try {
val setter = setter
if (setter != null) setter(instance, value) else field!!.set(instance, value)
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
}
}
}
@@ -31,17 +31,17 @@ class KPackageImpl(override val jClass: Class<*>) : KCallableContainerImpl(), KP
override val scope: JetScope get() = descriptor.memberScope
fun topLevelVariable(name: String): KTopLevelVariable<*> =
KTopLevelVariableImpl<Any?>(this, name)
fun topLevelVariable(name: String): KProperty0<*> =
KProperty0Impl<Any?>(this, name)
fun mutableTopLevelVariable(name: String): KMutableTopLevelVariable<*> =
KMutableTopLevelVariableImpl<Any?>(this, name)
fun mutableTopLevelVariable(name: String): KMutableProperty0<*> =
KMutableProperty0Impl<Any?>(this, name)
fun <T> topLevelExtensionProperty(name: String, receiver: Class<T>): KTopLevelExtensionProperty<T, *> =
KTopLevelExtensionPropertyImpl<T, Any?>(this, name, receiver)
fun <T> topLevelExtensionProperty(name: String, receiver: Class<T>): KProperty1<T, *> =
KProperty1Impl<T, Any?>(this, name, receiver)
fun <T> mutableTopLevelExtensionProperty(name: String, receiver: Class<T>): KMutableTopLevelExtensionProperty<T, *> =
KMutableTopLevelExtensionPropertyImpl<T, Any?>(this, name, receiver)
fun <T> mutableTopLevelExtensionProperty(name: String, receiver: Class<T>): KMutableProperty1<T, *> =
KMutableProperty1Impl<T, Any?>(this, name, receiver)
override fun equals(other: Any?): Boolean =
other is KPackageImpl && jClass == other.jClass
@@ -18,10 +18,10 @@ package kotlin.reflect.jvm.internal
import java.lang.reflect.Method
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMutableTopLevelVariable
import kotlin.reflect.KTopLevelVariable
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty0
open class KTopLevelVariableImpl<out R> : DescriptorBasedProperty, KTopLevelVariable<R>, KVariableImpl<R> {
open class KProperty0Impl<out R> : DescriptorBasedProperty, KProperty0<R>, KPropertyImpl<R> {
constructor(container: KPackageImpl, name: String) : super(container, name, null)
override val name: String get() = descriptor.getName().asString()
@@ -39,10 +39,10 @@ open class KTopLevelVariableImpl<out R> : DescriptorBasedProperty, KTopLevelVari
}
}
class KMutableTopLevelVariableImpl<R> : KTopLevelVariableImpl<R>, KMutableTopLevelVariable<R>, KMutableVariableImpl<R> {
class KMutableProperty0Impl<R> : KProperty0Impl<R>, KMutableProperty0<R>, KMutablePropertyImpl<R> {
constructor(container: KPackageImpl, name: String) : super(container, name)
override val setter: Method get() = super<KTopLevelVariableImpl>.setter!!
override val setter: Method get() = super<KProperty0Impl>.setter!!
override fun set(value: R) {
try {
@@ -0,0 +1,88 @@
/*
* 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.Modifier
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KProperty1
open class KProperty1Impl<T, out R> : DescriptorBasedProperty, KProperty1<T, R>, KPropertyImpl<R> {
constructor(container: KCallableContainerImpl, name: String, receiverParameterClass: Class<*>?) : super(
container, name, receiverParameterClass
)
constructor(container: KCallableContainerImpl, descriptor: PropertyDescriptor) : super(container, descriptor)
override val name: String get() = descriptor.getName().asString()
// TODO: consider optimizing this, not to do complex checks on every access
@suppress("UNCHECKED_CAST")
override fun get(receiver: T): R {
try {
val getter = getter ?:
return field!!.get(receiver) as R
if (Modifier.isStatic(getter.getModifiers())) {
// Workaround the case of platformStatic property in object, getter of which doesn't take a receiver
if (getter.getParameterTypes().isEmpty()) {
return getter.invoke(null) as R
}
return getter.invoke(null, receiver) as R
}
return getter.invoke(receiver) as R
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
}
}
}
class KMutableProperty1Impl<T, R> : KProperty1Impl<T, R>, KMutableProperty1<T, R>, KMutablePropertyImpl<R> {
constructor(container: KCallableContainerImpl, name: String, receiverParameterClass: Class<*>?) : super(
container, name, receiverParameterClass
)
constructor(container: KCallableContainerImpl, descriptor: PropertyDescriptor) : super(container, descriptor)
override fun set(receiver: T, value: R) {
try {
val setter = setter ?:
return field!!.set(receiver, value)
if (Modifier.isStatic(setter.getModifiers())) {
// Workaround the case of platformStatic property in object, setter of which doesn't take a receiver
if (setter.getParameterTypes().size() == 1) {
setter.invoke(null, value)
}
else {
setter.invoke(null, receiver, value)
}
}
else {
setter.invoke(receiver, value)
}
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
}
}
}
@@ -20,10 +20,10 @@ 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
import kotlin.reflect.KMutableProperty2
import kotlin.reflect.KProperty2
open class KMemberExtensionPropertyImpl<D : Any, E, out R> : DescriptorBasedProperty, KMemberExtensionProperty<D, E, R>, KPropertyImpl<R> {
open class KProperty2Impl<D, E, out R> : DescriptorBasedProperty, KProperty2<D, E, R>, KPropertyImpl<R> {
constructor(container: KClassImpl<D>, name: String, receiverParameterClass: Class<E>) : super(container, name, receiverParameterClass)
constructor(container: KClassImpl<D>, descriptor: PropertyDescriptor) : super(container, descriptor)
@@ -34,10 +34,10 @@ open class KMemberExtensionPropertyImpl<D : Any, E, out R> : DescriptorBasedProp
override val field: Field? get() = null
override fun get(instance: D, extensionReceiver: E): R {
override fun get(receiver1: D, receiver2: E): R {
try {
@suppress("UNCHECKED_CAST")
return getter.invoke(instance, extensionReceiver) as R
return getter.invoke(receiver1, receiver2) as R
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
@@ -46,19 +46,16 @@ open class KMemberExtensionPropertyImpl<D : Any, E, out R> : DescriptorBasedProp
}
class KMutableMemberExtensionPropertyImpl<D : Any, E, R> :
KMemberExtensionPropertyImpl<D, E, R>,
KMutableMemberExtensionProperty<D, E, R>,
KMutablePropertyImpl<R> {
class KMutableProperty2Impl<D, E, R> : KProperty2Impl<D, E, R>, KMutableProperty2<D, E, R>, KMutablePropertyImpl<R> {
constructor(container: KClassImpl<D>, name: String, receiverParameterClass: Class<E>) : super(container, name, receiverParameterClass)
constructor(container: KClassImpl<D>, descriptor: PropertyDescriptor) : super(container, descriptor)
override val setter: Method get() = super<KMemberExtensionPropertyImpl>.setter!!
override val setter: Method get() = super<KProperty2Impl>.setter!!
override fun set(instance: D, extensionReceiver: E, value: R) {
override fun set(receiver1: D, receiver2: E, value: R) {
try {
setter.invoke(instance, extensionReceiver, value)
setter.invoke(receiver1, receiver2, value)
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
@@ -19,13 +19,13 @@ package kotlin.reflect.jvm.internal
import java.lang.reflect.*
import kotlin.reflect.*
trait KPropertyImpl<out R> : KProperty<R>, KCallableImpl<R> {
interface KPropertyImpl<out R> : KProperty<R>, KCallableImpl<R> {
val field: Field?
val getter: Method?
}
trait KMutablePropertyImpl<R> : KMutableProperty<R>, KPropertyImpl<R> {
interface KMutablePropertyImpl<R> : KMutableProperty<R>, KPropertyImpl<R> {
val setter: Method?
}
@@ -1,58 +0,0 @@
/*
* 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 java.lang.reflect.Method
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMutableTopLevelExtensionProperty
import kotlin.reflect.KTopLevelExtensionProperty
open class KTopLevelExtensionPropertyImpl<T, out R> : DescriptorBasedProperty, KTopLevelExtensionProperty<T, R>, KPropertyImpl<R> {
constructor(container: KPackageImpl, name: String, receiverParameterClass: Class<T>) : super(container, name, receiverParameterClass)
override val name: String get() = descriptor.getName().asString()
override val getter: Method get() = super<DescriptorBasedProperty>.getter!!
override fun get(receiver: T): R {
try {
@suppress("UNCHECKED_CAST")
return getter.invoke(null, receiver) as R
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
}
}
}
class KMutableTopLevelExtensionPropertyImpl<T, R> :
KTopLevelExtensionPropertyImpl<T, R>,
KMutableTopLevelExtensionProperty<T, R>,
KMutablePropertyImpl<R> {
constructor(container: KPackageImpl, name: String, receiverParameterClass: Class<T>) : super(container, name, receiverParameterClass)
override val setter: Method get() = super<KTopLevelExtensionPropertyImpl>.setter!!
override fun set(receiver: T, value: R) {
try {
setter.invoke(null, receiver, value)
}
catch (e: IllegalAccessException) {
throw IllegalPropertyAccessException(e)
}
}
}
@@ -1,23 +0,0 @@
/*
* 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 kotlin.reflect.*
trait KVariableImpl<out R> : KVariable<R>, KPropertyImpl<R>
trait KMutableVariableImpl<R> : KMutableVariable<R>, KVariableImpl<R>, KMutablePropertyImpl<R>
@@ -50,32 +50,32 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
// Properties
@Override
public KMemberProperty memberProperty(String name, KClass owner) {
public KProperty1 memberProperty(String name, KClass owner) {
return ((KClassImpl) owner).memberProperty(name);
}
@Override
public KMutableMemberProperty mutableMemberProperty(String name, KClass owner) {
public KMutableProperty1 mutableMemberProperty(String name, KClass owner) {
return ((KClassImpl) owner).mutableMemberProperty(name);
}
@Override
public KTopLevelVariable topLevelVariable(String name, KPackage owner) {
public KProperty0 topLevelVariable(String name, KPackage owner) {
return ((KPackageImpl) owner).topLevelVariable(name);
}
@Override
public KMutableTopLevelVariable mutableTopLevelVariable(String name, KPackage owner) {
public KMutableProperty0 mutableTopLevelVariable(String name, KPackage owner) {
return ((KPackageImpl) owner).mutableTopLevelVariable(name);
}
@Override
public KTopLevelExtensionProperty topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
public KProperty1 topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return ((KPackageImpl) owner).topLevelExtensionProperty(name, receiver);
}
@Override
public KMutableTopLevelExtensionProperty mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
public KMutableProperty1 mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return ((KPackageImpl) owner).mutableTopLevelExtensionProperty(name, receiver);
}
}
@@ -41,6 +41,13 @@ public val KPackage.javaFacade: Class<*>
get() = (this as KPackageImpl).jClass
/**
* Returns a Java [Field] instance corresponding to the backing field of the given property,
* or `null` if the property has no backing field.
*/
public val KProperty<*>.javaField: Field?
get() = (this as KPropertyImpl<*>).field
/**
* Returns a Java [Method] instance corresponding to the getter of the given property,
* or `null` if the property has no getter, for example in case of a simple private `val` in a class.
@@ -56,45 +63,6 @@ public val KMutableProperty<*>.javaSetter: Method?
get() = (this as? KMutablePropertyImpl<*>)?.setter
/**
* Returns a Java [Field] instance corresponding to the backing field of the given top level property,
* or `null` if the property has no backing field.
*/
public val KTopLevelVariable<*>.javaField: Field?
get() = (this as KPropertyImpl<*>).field
/**
* Returns a Java [Method] instance corresponding to the getter of the given top level property.
*/
public val KTopLevelVariable<*>.javaGetter: Method
get() = (this as KTopLevelVariableImpl<*>).getter
/**
* Returns a Java [Method] instance corresponding to the setter of the given top level property.
*/
public val KMutableTopLevelVariable<*>.javaSetter: Method
get() = (this as KMutableTopLevelVariableImpl<*>).setter
/**
* Returns a Java [Method] instance corresponding to the getter of the given top level extension property.
*/
public val KTopLevelExtensionProperty<*, *>.javaGetter: Method
get() = (this as KTopLevelExtensionPropertyImpl<*, *>).getter
/**
* Returns a Java [Method] instance corresponding to the setter of the given top level extension property.
*/
public val KMutableTopLevelExtensionProperty<*, *>.javaSetter: Method
get() = (this as KMutableTopLevelExtensionPropertyImpl<*, *>).setter
/**
* Returns a Java [Field] instance corresponding to the backing field of the given member property,
* or `null` if the property has no backing field.
*/
public val KMemberProperty<*, *>.javaField: Field?
get() = (this as KPropertyImpl<*>).field
// Java reflection -> Kotlin reflection
@@ -17,8 +17,8 @@
package kotlin.reflect.jvm
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.internal.KMemberPropertyImpl
import kotlin.reflect.jvm.internal.KMutableMemberPropertyImpl
import kotlin.reflect.jvm.internal.KProperty1Impl
import kotlin.reflect.jvm.internal.KMutableProperty1Impl
/**
* Provides a way to suppress JVM access checks for a property.
@@ -34,11 +34,11 @@ import kotlin.reflect.jvm.internal.KMutableMemberPropertyImpl
public var <R> KProperty<R>.accessible: Boolean
get() {
return when (this) {
is KMutableMemberPropertyImpl<*, R> ->
is KMutableProperty1Impl<*, R> ->
field?.isAccessible() ?: true &&
getter?.isAccessible() ?: true &&
setter?.isAccessible() ?: true
is KMemberPropertyImpl<*, R> ->
is KProperty1Impl<*, R> ->
field?.isAccessible() ?: true &&
getter?.isAccessible() ?: true
else -> {
@@ -49,12 +49,12 @@ public var <R> KProperty<R>.accessible: Boolean
}
set(value) {
when (this) {
is KMutableMemberPropertyImpl<*, R> -> {
is KMutableProperty1Impl<*, R> -> {
field?.setAccessible(value)
getter?.setAccessible(value)
setter?.setAccessible(value)
}
is KMemberPropertyImpl<*, R> -> {
is KProperty1Impl<*, R> -> {
field?.setAccessible(value)
getter?.setAccessible(value)
}
@@ -67,27 +67,27 @@ public class Reflection {
// Properties
public static KMemberProperty memberProperty(String name, KClass owner) {
public static KProperty1 memberProperty(String name, KClass owner) {
return factory.memberProperty(name, owner);
}
public static KMutableMemberProperty mutableMemberProperty(String name, KClass owner) {
public static KMutableProperty1 mutableMemberProperty(String name, KClass owner) {
return factory.mutableMemberProperty(name, owner);
}
public static KTopLevelVariable topLevelVariable(String name, KPackage owner) {
public static KProperty0 topLevelVariable(String name, KPackage owner) {
return factory.topLevelVariable(name, owner);
}
public static KMutableTopLevelVariable mutableTopLevelVariable(String name, KPackage owner) {
public static KMutableProperty0 mutableTopLevelVariable(String name, KPackage owner) {
return factory.mutableTopLevelVariable(name, owner);
}
public static KTopLevelExtensionProperty topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
public static KProperty1 topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return factory.topLevelExtensionProperty(name, owner, receiver);
}
public static KMutableTopLevelExtensionProperty mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
public static KMutableProperty1 mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return factory.mutableTopLevelExtensionProperty(name, owner, receiver);
}
}
@@ -40,27 +40,27 @@ public class ReflectionFactory {
// Properties
public KMemberProperty memberProperty(String name, KClass owner) {
public KProperty1 memberProperty(String name, KClass owner) {
throw error();
}
public KMutableMemberProperty mutableMemberProperty(String name, KClass owner) {
public KMutableProperty1 mutableMemberProperty(String name, KClass owner) {
throw error();
}
public KTopLevelVariable topLevelVariable(String name, KPackage owner) {
public KProperty0 topLevelVariable(String name, KPackage owner) {
throw error();
}
public KMutableTopLevelVariable mutableTopLevelVariable(String name, KPackage owner) {
public KMutableProperty0 mutableTopLevelVariable(String name, KPackage owner) {
throw error();
}
public KTopLevelExtensionProperty topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
public KProperty1 topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
throw error();
}
public KMutableTopLevelExtensionProperty mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
public KMutableProperty1 mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
throw error();
}
@@ -1,11 +1,11 @@
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/property/.
package foo
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class A {
companion object {
val ref: KMemberProperty<A, String> = A::foo
val ref: KProperty1<A, String> = A::foo
}
val foo: String = "OK"