Support references to top level and member properties in JVM codegen
#KT-1183 In Progress
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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
|
||||
|
||||
abstract class KCallableImpl<out R>(
|
||||
public override val name: String
|
||||
) : KCallable<R>
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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
|
||||
|
||||
open class KMemberPropertyImpl<T, out R>(
|
||||
name: String,
|
||||
protected val owner: KClassImpl<T>
|
||||
) : KMemberProperty<T, R>, KPropertyImpl<R>(name) {
|
||||
// TODO: extract, make lazy (weak?), use our descriptors knowledge, support Java fields
|
||||
protected val getter: Method = owner.jClass.getMethod(getterName(name))
|
||||
|
||||
override fun get(receiver: T): R {
|
||||
return getter(receiver) as R
|
||||
}
|
||||
}
|
||||
|
||||
class KMutableMemberPropertyImpl<T, R>(
|
||||
name: String,
|
||||
owner: KClassImpl<T>
|
||||
) : KMutableMemberProperty<T, R>, KMemberPropertyImpl<T, R>(name, owner) {
|
||||
private val setter = owner.jClass.getMethod(setterName(name), getter.getReturnType()!!)
|
||||
|
||||
override fun set(receiver: T, value: R) {
|
||||
setter.invoke(receiver, value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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
|
||||
|
||||
abstract class KPropertyImpl<out R>(
|
||||
name: String
|
||||
) : KProperty<R>, KCallableImpl<R>(name)
|
||||
|
||||
|
||||
abstract class KMutablePropertyImpl<R>(
|
||||
name: String
|
||||
) : KMutableProperty<R>, KPropertyImpl<R>(name)
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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
|
||||
|
||||
open class KTopLevelPropertyImpl<out R>(
|
||||
name: String,
|
||||
protected val owner: KPackageImpl
|
||||
) : KTopLevelProperty<R>, KVariableImpl<R>(name) {
|
||||
// TODO: extract, make lazy (weak?), use our descriptors knowledge, support Java fields
|
||||
protected val getter: Method = owner.jClass.getMethod(getterName(name))
|
||||
|
||||
override fun get(): R {
|
||||
return getter(null) as R
|
||||
}
|
||||
}
|
||||
|
||||
class KMutableTopLevelPropertyImpl<R>(
|
||||
name: String,
|
||||
owner: KPackageImpl
|
||||
) : KMutableTopLevelProperty<R>, KTopLevelPropertyImpl<R>(name, owner) {
|
||||
private val setter = owner.jClass.getMethod(setterName(name), getter.getReturnType()!!)
|
||||
|
||||
override fun set(value: R) {
|
||||
setter.invoke(null, value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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
|
||||
|
||||
abstract class KVariableImpl<out R>(
|
||||
name: String
|
||||
) : KVariable<R>, KPropertyImpl<R>(name)
|
||||
|
||||
|
||||
abstract class KMutableVariableImpl<R>(
|
||||
name: String
|
||||
) : KMutableVariable<R>, KVariableImpl<R>(name)
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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
|
||||
|
||||
// TODO: use stdlib?
|
||||
suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
|
||||
private fun String.capitalizeWithJavaBeanConvention(): String {
|
||||
// The code is a bit crooked because otherwise there are overload resolution ambiguities caused by the fact
|
||||
// that we compile it with the built-ins both in source and as a compiled library
|
||||
val l = length
|
||||
if (l > 1 && Character.isUpperCase(get(1))) return this
|
||||
val first = get(0)
|
||||
this as java.lang.String
|
||||
return "" + Character.toUpperCase(first) + substring(1, l)
|
||||
}
|
||||
|
||||
private fun getterName(propertyName: String): String = "get" + propertyName.capitalizeWithJavaBeanConvention()
|
||||
private fun setterName(propertyName: String): String = "set" + propertyName.capitalizeWithJavaBeanConvention()
|
||||
|
||||
|
||||
private val K_OBJECT_CLASS = Class.forName("kotlin.jvm.internal.KObject")
|
||||
|
||||
// TODO
|
||||
fun <T> kotlinClass(jClass: Class<T>): KClassImpl<T> {
|
||||
if (K_OBJECT_CLASS.isAssignableFrom(jClass)) {
|
||||
val field = jClass.getDeclaredField("\$kotlinClass")
|
||||
return field.get(null) as KClassImpl<T>
|
||||
}
|
||||
throw UnsupportedOperationException("Unsupported class: $jClass")
|
||||
}
|
||||
Reference in New Issue
Block a user