-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom implementations for toString, equals and hashCode
- Loading branch information
Christian Melchior
committed
Aug 10, 2023
1 parent
273fd31
commit f402006
Showing
10 changed files
with
454 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...in-compiler/src/main/kotlin/io/realm/kotlin/compiler/RealmModelDefaultMethodGeneration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package io.realm.kotlin.compiler | ||
|
||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext | ||
import org.jetbrains.kotlin.ir.builders.irGet | ||
import org.jetbrains.kotlin.ir.builders.irGetObject | ||
import org.jetbrains.kotlin.ir.builders.irReturn | ||
import org.jetbrains.kotlin.ir.declarations.IrClass | ||
import org.jetbrains.kotlin.ir.declarations.IrProperty | ||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction | ||
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl | ||
import org.jetbrains.kotlin.ir.types.IrType | ||
import org.jetbrains.kotlin.ir.util.functions | ||
import org.jetbrains.kotlin.name.Name | ||
|
||
/** | ||
* Class responsible for adding Realm specific logic to the default object methods like: | ||
* - toString() | ||
* - hashCode() | ||
* - equals() | ||
*/ | ||
class RealmModelDefaultMethodGeneration(private val pluginContext: IrPluginContext) { | ||
|
||
private val realmObjectHelper: IrClass = pluginContext.lookupClassOrThrow(FqNames.REALM_OBJECT_HELPER) | ||
private val realmToString: IrSimpleFunction = realmObjectHelper.lookupFunction(Name.identifier("realmToString")) | ||
private val realmEquals: IrSimpleFunction = realmObjectHelper.lookupFunction(Name.identifier("realmEquals")) | ||
private val realmHashCode: IrSimpleFunction = realmObjectHelper.lookupFunction(Name.identifier("realmHashCode")) | ||
private lateinit var objectReferenceProperty: IrProperty | ||
private lateinit var objectReferenceType: IrType | ||
|
||
fun addDefaultMethods(irClass: IrClass) { | ||
objectReferenceProperty = irClass.lookupProperty(Names.OBJECT_REFERENCE) | ||
objectReferenceType = objectReferenceProperty.backingField!!.type | ||
|
||
if (syntheticMethodExists(irClass, "toString")) { | ||
addToStringMethodBody(irClass) | ||
} | ||
if (syntheticMethodExists(irClass, "hashCode")) { | ||
addHashCodeMethod(irClass) | ||
} | ||
if (syntheticMethodExists(irClass, "equals")) { | ||
addEqualsMethod(irClass) | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a synthetic method exists in the given class. Methods in super classes | ||
* are ignored, only methods actually declared in the class will return `true`. | ||
* | ||
* These methods are created by an earlier step by the Realm compiler plugin and are | ||
* recognized by not being fake and having an empty body.ß | ||
*/ | ||
private fun syntheticMethodExists(irClass: IrClass, methodName: String): Boolean { | ||
return irClass.functions.firstOrNull { | ||
!it.isFakeOverride && it.body == null && it.name == Name.identifier(methodName) | ||
} != null | ||
} | ||
|
||
private fun addEqualsMethod(irClass: IrClass) { | ||
val function: IrSimpleFunction = irClass.symbol.owner.functions.single { it.name.toString() == "equals" } | ||
function.body = pluginContext.blockBody(function.symbol) { | ||
+irReturn( | ||
IrCallImpl( | ||
startOffset = startOffset, | ||
endOffset = endOffset, | ||
type = pluginContext.irBuiltIns.booleanType, | ||
symbol = realmEquals.symbol, | ||
typeArgumentsCount = 0, | ||
valueArgumentsCount = 2 | ||
).apply { | ||
dispatchReceiver = irGetObject(realmObjectHelper.symbol) | ||
putValueArgument(0, irGet(function.dispatchReceiverParameter!!.type, function.dispatchReceiverParameter!!.symbol)) | ||
putValueArgument(1, irGet(function.dispatchReceiverParameter!!.type, function.dispatchReceiverParameter!!.symbol)) | ||
} | ||
) | ||
} | ||
} | ||
|
||
private fun addHashCodeMethod(irClass: IrClass) { | ||
val function: IrSimpleFunction = irClass.symbol.owner.functions.single { it.name.toString() == "hashCode" } | ||
function.body = pluginContext.blockBody(function.symbol) { | ||
+irReturn( | ||
IrCallImpl( | ||
startOffset = startOffset, | ||
endOffset = endOffset, | ||
type = pluginContext.irBuiltIns.intType, | ||
symbol = realmHashCode.symbol, | ||
typeArgumentsCount = 0, | ||
valueArgumentsCount = 1 | ||
).apply { | ||
dispatchReceiver = irGetObject(realmObjectHelper.symbol) | ||
putValueArgument(0, irGet(function.dispatchReceiverParameter!!.type, function.dispatchReceiverParameter!!.symbol)) | ||
} | ||
) | ||
} | ||
} | ||
|
||
private fun addToStringMethodBody(irClass: IrClass) { | ||
val function: IrSimpleFunction = irClass.symbol.owner.functions.single { it.name.toString() == "toString" } | ||
function.body = pluginContext.blockBody(function.symbol) { | ||
+irReturn( | ||
IrCallImpl( | ||
startOffset = startOffset, | ||
endOffset = endOffset, | ||
type = pluginContext.irBuiltIns.stringType, | ||
symbol = realmToString.symbol, | ||
typeArgumentsCount = 0, | ||
valueArgumentsCount = 1 | ||
).apply { | ||
dispatchReceiver = irGetObject(realmObjectHelper.symbol) | ||
putValueArgument(0, irGet(function.dispatchReceiverParameter!!.type, function.dispatchReceiverParameter!!.symbol)) | ||
} | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...-compiler/src/main/kotlin/io/realm/kotlin/compiler/RealmModelSyntheticMethodsExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2020 Realm Inc. | ||
* | ||
* 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 io.realm.kotlin.compiler | ||
|
||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor | ||
import org.jetbrains.kotlin.descriptors.ClassDescriptor | ||
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities | ||
import org.jetbrains.kotlin.descriptors.Modality | ||
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor | ||
import org.jetbrains.kotlin.descriptors.annotations.Annotations | ||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl | ||
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl | ||
import org.jetbrains.kotlin.name.Name | ||
import org.jetbrains.kotlin.resolve.BindingContext | ||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns | ||
import org.jetbrains.kotlin.resolve.descriptorUtil.parents | ||
import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension | ||
import org.jetbrains.kotlin.types.SimpleType | ||
|
||
/** | ||
* Triggers generation of synthetic methods on Realm model classes, in particular | ||
* `toString()`, `equals()` and `hashCode()`. | ||
*/ | ||
class RealmModelSyntheticMethodsExtension : SyntheticResolveExtension { | ||
|
||
override fun generateSyntheticMethods( | ||
thisDescriptor: ClassDescriptor, | ||
name: Name, | ||
bindingContext: BindingContext, | ||
fromSupertypes: List<SimpleFunctionDescriptor>, | ||
result: MutableCollection<SimpleFunctionDescriptor> | ||
) { | ||
if (thisDescriptor.isRealmObject | ||
&& !thisDescriptor.isCompanionObject /* Do not override companion object methods */ | ||
&& !thisDescriptor.isInner /* Do not override inner class methods */ | ||
&& !isNestedInRealmModelClass(thisDescriptor) /* do not override nested class methods */ | ||
&& result.isEmpty() /* = no method has been declared in the current class */ | ||
) { | ||
when(name.identifier) { | ||
"toString" -> { | ||
result.add( | ||
createMethod( | ||
classDescriptor = thisDescriptor, | ||
methodName = name, | ||
arguments = emptyList(), | ||
returnType = thisDescriptor.builtIns.stringType | ||
) | ||
) | ||
} | ||
"equals" -> { | ||
result.add( | ||
createMethod( | ||
classDescriptor = thisDescriptor, | ||
methodName = name, | ||
arguments = listOf(Pair("other", thisDescriptor.builtIns.nullableAnyType)), | ||
returnType = thisDescriptor.builtIns.booleanType | ||
) | ||
) | ||
} | ||
"hashCode" -> { | ||
result.add( | ||
createMethod( | ||
classDescriptor = thisDescriptor, | ||
methodName = name, | ||
arguments = emptyList(), | ||
returnType = thisDescriptor.builtIns.intType | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun createMethod( | ||
classDescriptor: ClassDescriptor, | ||
methodName: Name, | ||
arguments: List<Pair<String, SimpleType>>, | ||
returnType: SimpleType | ||
): SimpleFunctionDescriptor { | ||
return SimpleFunctionDescriptorImpl.create( | ||
classDescriptor, | ||
Annotations.EMPTY, | ||
methodName, | ||
CallableMemberDescriptor.Kind.SYNTHESIZED, | ||
classDescriptor.source | ||
).apply { | ||
initialize( | ||
null, | ||
classDescriptor.thisAsReceiverParameter, | ||
emptyList(), | ||
emptyList(), | ||
arguments.map { (argumentName, argumentType) -> | ||
ValueParameterDescriptorImpl( | ||
containingDeclaration = this, | ||
original = null, | ||
index = 0, | ||
annotations = Annotations.EMPTY, | ||
name = Name.identifier(argumentName), | ||
outType = argumentType, | ||
declaresDefaultValue = false, | ||
isCrossinline = false, | ||
isNoinline = false, | ||
varargElementType = null, | ||
source = this.source | ||
) | ||
}, | ||
returnType, | ||
Modality.OPEN, | ||
DescriptorVisibilities.PUBLIC | ||
) | ||
} | ||
} | ||
|
||
private fun isNestedInRealmModelClass(classDescriptor: ClassDescriptor): Boolean { | ||
return classDescriptor.parents.firstOrNull { | ||
if (it is ClassDescriptor) { | ||
it.isRealmObject | ||
} else { | ||
false | ||
} | ||
} != null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.