Generate separate anonymous class for each property reference

Each property reference obtained by the '::' operator now causes back-end to
generate an anonymous subclass of the corresponding KProperty class, with the
customized behavior. This fixes a number of issues:

- get/set/name of property references now works without kotlin-reflect.jar in
  the classpath
- get/set/name methods are now overridden with statically-generated property
  access instead of the default KPropertyImpl's behavior of using Java
  reflection, which should be a lot faster
- references to private/protected properties now work without the need to set
  'accessible' flag, because corresponding synthetic accessors are generated at
  compile-time near the target property

 #KT-6870 Fixed
 #KT-6873 Fixed
 #KT-7033 Fixed
This commit is contained in:
Alexander Udalov
2015-06-30 16:07:51 +03:00
parent 30794060a9
commit 048a9b686e
40 changed files with 833 additions and 304 deletions
@@ -0,0 +1,68 @@
/*
* 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.jvm.internal;
import kotlin.jvm.KotlinReflectionNotSupportedError;
import kotlin.reflect.KCallable;
import kotlin.reflect.KDeclarationContainer;
/**
* A superclass for all classes generated by Kotlin compiler for callable references.
*
* All methods from reflection API should be implemented here to throw informative exceptions (see KotlinReflectionNotSupportedError)
*/
public abstract class CallableReference implements KCallable {
// The following methods provide the information identifying this callable, which is used by the reflection implementation.
// They are supposed to be overridden in each subclass (each anonymous class generated for a callable reference).
/**
* @return the class or package where the callable should be located, usually specified on the LHS of the '::' operator
*/
public KDeclarationContainer getOwner() {
throw error();
}
/**
* @return Kotlin name of the callable, the one which was declared in the source code (@platformName doesn't change it)
*/
@Override
public String getName() {
throw error();
}
/**
* @return JVM signature of the callable, e.g. "println(Ljava/lang/Object;)V". If this is a property reference,
* returns the JVM signature of its getter, e.g. "getFoo(Ljava/lang/String;)I". If the property has no getter in the bytecode
* (e.g. private property in a class), it's still the signature of the imaginary default getter that would be generated otherwise.
*
* Note that technically the signature itself is not even used as a signature per se in reflection implementation,
* but only as a unique and unambiguous way to map a function/property descriptor to a string.
*/
public String getSignature() {
throw error();
}
// The following methods are the stub implementations of reflection functions.
// They are called when you're using reflection on a property reference without the reflection implementation in the classpath.
// (nothing here yet)
protected static Error error() {
throw new KotlinReflectionNotSupportedError();
}
}
@@ -37,29 +37,22 @@ public class FunctionReference extends FunctionImpl implements KFunction {
return arity;
}
// The following methods provide the information identifying this function, which is used by the reflection implementation.
// They are supposed to be overridden in each subclass (each anonymous class generated for a function reference).
// Most of the following methods are copies from CallableReference, since this class cannot inherit from it
public KDeclarationContainer getOwner() {
throw error();
}
// Kotlin name of the function, the one which was declared in the source code (@platformName can't change it)
@Override
public String getName() {
throw error();
}
// JVM signature of the function, e.g. "println(Ljava/lang/Object;)V"
public String getSignature() {
throw error();
}
// The following methods are the stub implementations of reflection functions.
// They are called when you're using reflection on a function reference without the reflection implementation in the classpath.
// (nothing here yet)
private static Error error() {
protected static Error error() {
throw new KotlinReflectionNotSupportedError();
}
}
@@ -0,0 +1,22 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KMutableProperty;
public abstract class MutablePropertyReference extends PropertyReference implements KMutableProperty {
}
@@ -0,0 +1,31 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KMutableProperty0;
public class MutablePropertyReference0 extends MutablePropertyReference implements KMutableProperty0 {
@Override
public Object get() {
throw error();
}
@Override
public void set(Object value) {
throw error();
}
}
@@ -0,0 +1,31 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KMutableProperty1;
public class MutablePropertyReference1 extends MutablePropertyReference implements KMutableProperty1 {
@Override
public Object get(Object receiver) {
throw error();
}
@Override
public void set(Object receiver, Object value) {
throw error();
}
}
@@ -0,0 +1,31 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KMutableProperty2;
public class MutablePropertyReference2 extends MutablePropertyReference implements KMutableProperty2 {
@Override
public Object get(Object receiver1, Object receiver2) {
throw error();
}
@Override
public void set(Object receiver1, Object receiver2, Object value) {
throw error();
}
}
@@ -0,0 +1,22 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KProperty;
public abstract class PropertyReference extends CallableReference implements KProperty {
}
@@ -0,0 +1,26 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KProperty0;
public class PropertyReference0 extends PropertyReference implements KProperty0 {
@Override
public Object get() {
throw error();
}
}
@@ -0,0 +1,26 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KProperty1;
public class PropertyReference1 extends PropertyReference implements KProperty1 {
@Override
public Object get(Object receiver) {
throw error();
}
}
@@ -0,0 +1,26 @@
/*
* 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.jvm.internal;
import kotlin.reflect.KProperty2;
public class PropertyReference2 extends PropertyReference implements KProperty2 {
@Override
public Object get(Object receiver1, Object receiver2) {
throw error();
}
}
@@ -67,27 +67,27 @@ public class Reflection {
// Properties
public static KProperty1 memberProperty(String name, KClass owner) {
return factory.memberProperty(name, owner);
public static KProperty0 property0(PropertyReference0 p) {
return factory.property0(p);
}
public static KMutableProperty1 mutableMemberProperty(String name, KClass owner) {
return factory.mutableMemberProperty(name, owner);
public static KMutableProperty0 mutableProperty0(MutablePropertyReference0 p) {
return factory.mutableProperty0(p);
}
public static KProperty0 topLevelVariable(String name, KPackage owner) {
return factory.topLevelVariable(name, owner);
public static KProperty1 property1(PropertyReference1 p) {
return factory.property1(p);
}
public static KMutableProperty0 mutableTopLevelVariable(String name, KPackage owner) {
return factory.mutableTopLevelVariable(name, owner);
public static KMutableProperty1 mutableProperty1(MutablePropertyReference1 p) {
return factory.mutableProperty1(p);
}
public static KProperty1 topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return factory.topLevelExtensionProperty(name, owner, receiver);
public static KProperty2 property2(PropertyReference2 p) {
return factory.property2(p);
}
public static KMutableProperty1 mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return factory.mutableTopLevelExtensionProperty(name, owner, receiver);
public static KMutableProperty2 mutableProperty2(MutablePropertyReference2 p) {
return factory.mutableProperty2(p);
}
}
@@ -40,28 +40,28 @@ public class ReflectionFactory {
// Properties
public KProperty1 memberProperty(String name, KClass owner) {
throw error();
public KProperty0 property0(PropertyReference0 p) {
return p;
}
public KMutableProperty1 mutableMemberProperty(String name, KClass owner) {
throw error();
public KMutableProperty0 mutableProperty0(MutablePropertyReference0 p) {
return p;
}
public KProperty0 topLevelVariable(String name, KPackage owner) {
throw error();
public KProperty1 property1(PropertyReference1 p) {
return p;
}
public KMutableProperty0 mutableTopLevelVariable(String name, KPackage owner) {
throw error();
public KMutableProperty1 mutableProperty1(MutablePropertyReference1 p) {
return p;
}
public KProperty1 topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
throw error();
public KProperty2 property2(PropertyReference2 p) {
return p;
}
public KMutableProperty1 mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
throw error();
public KMutableProperty2 mutableProperty2(MutablePropertyReference2 p) {
return p;
}
private Error error() {