-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix handling of primitive type classes
In cases when metadata contained `@Serializer(forClass = int.class)` `int.class` part was treated as a class element value, and previously, in the refactored method, its internal className was calculated to be null as primitive class names were not taken into account apparently. It led to stacktrace like this: ``` java.lang.NullPointerException: Cannot invoke "String.length()" because "className" is null at com.guardsquare.proguard.kotlin.printer.internal.Context.className(Context.java:83) at com.guardsquare.proguard.kotlin.printer.internal.AnnotationPrinter.visitClassElementValue(AnnotationPrinter.java:138) ... ``` This diff adds a primitive type class check to have the name derived correctly.
- Loading branch information
1 parent
41c8a82
commit c65a182
Showing
2 changed files
with
78 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import com.guardsquare.proguard.kotlin.printer.KotlinMetadataPrinter | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import proguard.classfile.kotlin.visitor.ReferencedKotlinMetadataVisitor | ||
import proguard.testutils.ClassPoolBuilder | ||
import proguard.testutils.KotlinSource | ||
|
||
class PrimitiveInAnnotationTest : FunSpec({ | ||
test("Primitive type class in annotation") { | ||
val (programClassPool, _) = ClassPoolBuilder.fromSource( | ||
KotlinSource( | ||
"Test.kt", | ||
""" | ||
import kotlin.reflect.KClass | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class Serializer(val forClass: KClass<*>) | ||
|
||
@Serializer(forClass = Int::class) | ||
class Test { | ||
var myInt: Int = 0 | ||
} | ||
""".trimIndent() | ||
), | ||
) | ||
|
||
programClassPool.classesAccept( | ||
ReferencedKotlinMetadataVisitor( | ||
KotlinMetadataPrinter( | ||
programClassPool | ||
) | ||
) | ||
) | ||
|
||
val testKtMetadata = programClassPool.getClass("Test").processingInfo as String | ||
|
||
testKtMetadata.trimEnd() shouldBe """ | ||
/** | ||
* Kotlin class (metadata version 1.8.0). | ||
* From Java class: Test | ||
*/ | ||
@Serializer(forClass = Int::class) | ||
class Test { | ||
// Properties | ||
var myInt: Int | ||
// backing field: int myInt | ||
get // getter method: public final int getMyInt() | ||
set() // setter method: public final void setMyInt(int) | ||
} | ||
""".trimIndent() | ||
} | ||
}) |