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
+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)
}