Skip to content

Commit

Permalink
updated compiler plugin to make @sparkify classes Serializable too, p…
Browse files Browse the repository at this point in the history
…lus added test to see if the plugin is enabled in the project
  • Loading branch information
Jolanrensen committed Mar 27, 2024
1 parent 1c34429 commit 68e830a
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,20 @@ class DataClassSparkifyGenerator(

if (!declaration.isData) return super.visitClass(declaration)

// add superclass
// add superclasses
val scalaProductClass = productFqNames.firstNotNullOfOrNull {
val classId = ClassId.topLevel(FqName(it))
// ClassId(
// packageFqName = FqName("scala"),
// topLevelName = Name.identifier("Product"),
// )
pluginContext.referenceClass(classId)
}!!

declaration.superTypes += scalaProductClass.defaultType

val serializableClass = pluginContext.referenceClass(
ClassId.topLevel(FqName("java.io.Serializable"))
)!!

declaration.superTypes += serializableClass.defaultType

// finding the constructor params
val constructorParams = declaration.primaryConstructor?.valueParameters
?: return super.visitClass(declaration)
Expand Down Expand Up @@ -349,7 +351,7 @@ class DataClassSparkifyGenerator(
}

val ioobClass = pluginContext.referenceClass(
FqName("java.lang.IndexOutOfBoundsException").toClassId()
ClassId(FqName("java.lang"), Name.identifier("IndexOutOfBoundsException"))
)!!
val ioobConstructor = ioobClass.constructors.first { it.owner.valueParameters.isEmpty() }
val throwCall = irThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ FILE fqName:foo.bar fileName:/dataClassInFunctionTest.kt
VALUE_PARAMETER name:n index:0 type:kotlin.Int
FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product]
CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product; java.io.Serializable]
annotations:
Sparkify
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:foo.bar.box.User
Expand Down Expand Up @@ -168,7 +168,7 @@ FILE fqName:foo.bar fileName:/dataClassInFunctionTest.kt
CONST Double type=kotlin.Double value=2.0
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product; java.io.Serializable]'
FUN GENERATED_DATA_CLASS_MEMBER name:component1 visibility:public modality:FINAL <> ($this:foo.bar.box.User) returnType:kotlin.String [operator]
$this: VALUE_PARAMETER name:<this> type:foo.bar.box.User
BLOCK_BODY
Expand Down Expand Up @@ -402,31 +402,31 @@ FILE fqName:foo.bar fileName:/dataClassInFunctionTest.kt
$this: CALL 'public open fun getMethod (p0: @[FlexibleNullability] kotlin.String?, vararg p1: @[FlexibleNullability] java.lang.Class<*>?): @[FlexibleNullability] java.lang.reflect.Method? declared in java.lang.Class' type=@[FlexibleNullability] java.lang.reflect.Method? origin=null
$this: CALL 'public final fun <get-java> <T> (): java.lang.Class<T of kotlin.jvm.<get-java>> declared in kotlin.jvm' type=java.lang.Class<foo.bar.box.User> origin=GET_PROPERTY
<T>: foo.bar.box.User
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product]' type=kotlin.reflect.KClass<foo.bar.box.User>
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product; java.io.Serializable]' type=kotlin.reflect.KClass<foo.bar.box.User>
p0: CONST String type=kotlin.String value="name"
p0: GET_VAR 'val user: foo.bar.box.User declared in foo.bar.box' type=foo.bar.box.User origin=null
VAR name:age type:@[FlexibleNullability] kotlin.Any? [val]
CALL 'public open fun invoke (p0: @[FlexibleNullability] kotlin.Any?, vararg p1: @[FlexibleNullability] kotlin.Any?): @[FlexibleNullability] kotlin.Any? declared in java.lang.reflect.Method' type=@[FlexibleNullability] kotlin.Any? origin=null
$this: CALL 'public open fun getMethod (p0: @[FlexibleNullability] kotlin.String?, vararg p1: @[FlexibleNullability] java.lang.Class<*>?): @[FlexibleNullability] java.lang.reflect.Method? declared in java.lang.Class' type=@[FlexibleNullability] java.lang.reflect.Method? origin=null
$this: CALL 'public final fun <get-java> <T> (): java.lang.Class<T of kotlin.jvm.<get-java>> declared in kotlin.jvm' type=java.lang.Class<foo.bar.box.User> origin=GET_PROPERTY
<T>: foo.bar.box.User
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product]' type=kotlin.reflect.KClass<foo.bar.box.User>
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product; java.io.Serializable]' type=kotlin.reflect.KClass<foo.bar.box.User>
p0: CONST String type=kotlin.String value="age"
p0: GET_VAR 'val user: foo.bar.box.User declared in foo.bar.box' type=foo.bar.box.User origin=null
VAR name:a type:@[FlexibleNullability] kotlin.Any? [val]
CALL 'public open fun invoke (p0: @[FlexibleNullability] kotlin.Any?, vararg p1: @[FlexibleNullability] kotlin.Any?): @[FlexibleNullability] kotlin.Any? declared in java.lang.reflect.Method' type=@[FlexibleNullability] kotlin.Any? origin=null
$this: CALL 'public open fun getMethod (p0: @[FlexibleNullability] kotlin.String?, vararg p1: @[FlexibleNullability] java.lang.Class<*>?): @[FlexibleNullability] java.lang.reflect.Method? declared in java.lang.Class' type=@[FlexibleNullability] java.lang.reflect.Method? origin=null
$this: CALL 'public final fun <get-java> <T> (): java.lang.Class<T of kotlin.jvm.<get-java>> declared in kotlin.jvm' type=java.lang.Class<foo.bar.box.User> origin=GET_PROPERTY
<T>: foo.bar.box.User
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product]' type=kotlin.reflect.KClass<foo.bar.box.User>
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product; java.io.Serializable]' type=kotlin.reflect.KClass<foo.bar.box.User>
p0: CONST String type=kotlin.String value="a"
p0: GET_VAR 'val user: foo.bar.box.User declared in foo.bar.box' type=foo.bar.box.User origin=null
VAR name:b type:@[FlexibleNullability] kotlin.Any? [val]
CALL 'public open fun invoke (p0: @[FlexibleNullability] kotlin.Any?, vararg p1: @[FlexibleNullability] kotlin.Any?): @[FlexibleNullability] kotlin.Any? declared in java.lang.reflect.Method' type=@[FlexibleNullability] kotlin.Any? origin=null
$this: CALL 'public open fun getMethod (p0: @[FlexibleNullability] kotlin.String?, vararg p1: @[FlexibleNullability] java.lang.Class<*>?): @[FlexibleNullability] java.lang.reflect.Method? declared in java.lang.Class' type=@[FlexibleNullability] java.lang.reflect.Method? origin=null
$this: CALL 'public final fun <get-java> <T> (): java.lang.Class<T of kotlin.jvm.<get-java>> declared in kotlin.jvm' type=java.lang.Class<foo.bar.box.User> origin=GET_PROPERTY
<T>: foo.bar.box.User
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product]' type=kotlin.reflect.KClass<foo.bar.box.User>
$receiver: CLASS_REFERENCE 'CLASS CLASS name:User modality:FINAL visibility:local [data] superTypes:[kotlin.Any; foo.bar.Product; java.io.Serializable]' type=kotlin.reflect.KClass<foo.bar.box.User>
p0: CONST String type=kotlin.String value="b"
p0: GET_VAR 'val user: foo.bar.box.User declared in foo.bar.box' type=foo.bar.box.User origin=null
WHEN type=kotlin.Unit origin=IF
Expand Down
Loading

0 comments on commit 68e830a

Please sign in to comment.