diff --git a/base/src/main/java/proguard/classfile/io/kotlin/KotlinMetadataWriter.java b/base/src/main/java/proguard/classfile/io/kotlin/KotlinMetadataWriter.java index e7e87224e..a0414b8f7 100644 --- a/base/src/main/java/proguard/classfile/io/kotlin/KotlinMetadataWriter.java +++ b/base/src/main/java/proguard/classfile/io/kotlin/KotlinMetadataWriter.java @@ -1820,8 +1820,6 @@ private static int convertTypeParameterFlags(KotlinTypeParameterFlags flags) { Set flagSet = new HashSet<>(); - flagSet.addAll(convertCommonFlags(flags.common)); - if (flags.isReified) flagSet.add(Flag.TypeParameter.IS_REIFIED); return flagsOf(flagSet.toArray(new Flag[0])); diff --git a/base/src/main/java/proguard/classfile/kotlin/KotlinTypeMetadata.java b/base/src/main/java/proguard/classfile/kotlin/KotlinTypeMetadata.java index 8086cf819..3ec159d24 100644 --- a/base/src/main/java/proguard/classfile/kotlin/KotlinTypeMetadata.java +++ b/base/src/main/java/proguard/classfile/kotlin/KotlinTypeMetadata.java @@ -236,7 +236,7 @@ public boolean isStarProjection() private static class KotlinStarProjectionMetadata extends KotlinTypeMetadata { - KotlinStarProjectionMetadata() { super(new KotlinTypeFlags(new KotlinCommonFlags())); } + KotlinStarProjectionMetadata() { super(new KotlinTypeFlags()); } @Override public boolean isStarProjection() diff --git a/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeFlags.java b/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeFlags.java index 4df900a43..2ec119af2 100644 --- a/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeFlags.java +++ b/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeFlags.java @@ -20,16 +20,9 @@ /** * Flags for Kotlin types. - * - * No valid common visibility or modality flags. - * - * hasAnnotation is valid. */ public class KotlinTypeFlags implements KotlinFlags { - - public final KotlinCommonFlags common; - /** * Signifies that the corresponding type is marked as nullable, i.e. has a question mark at the end of its notation. */ @@ -44,9 +37,4 @@ public class KotlinTypeFlags implements KotlinFlags * Signifies that the corresponding type is `suspend`. */ public boolean isSuspend; - - public KotlinTypeFlags(KotlinCommonFlags common) - { - this.common = common; - } } diff --git a/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeParameterFlags.java b/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeParameterFlags.java index c0a909797..0ad83cca1 100644 --- a/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeParameterFlags.java +++ b/base/src/main/java/proguard/classfile/kotlin/flags/KotlinTypeParameterFlags.java @@ -20,22 +20,11 @@ /** * Flags for Kotlin type parameters. - * - * No valid visibility or modality flags. - * - * hasAnnotation is valid. */ public class KotlinTypeParameterFlags implements KotlinFlags { - public final KotlinCommonFlags common; - /** * Signifies that the corresponding type parameter is `reified`. */ public boolean isReified; - - public KotlinTypeParameterFlags(KotlinCommonFlags common) - { - this.common = common; - } } diff --git a/base/src/main/java/proguard/classfile/kotlin/visitor/KotlinMetadataPrinter.java b/base/src/main/java/proguard/classfile/kotlin/visitor/KotlinMetadataPrinter.java index fd7b20300..e51f4c6bc 100644 --- a/base/src/main/java/proguard/classfile/kotlin/visitor/KotlinMetadataPrinter.java +++ b/base/src/main/java/proguard/classfile/kotlin/visitor/KotlinMetadataPrinter.java @@ -240,7 +240,6 @@ public void visitAnyTypeParameter(Clazz clazz, KotlinTypeParameterMetadata kotli pw.println( typeParameterFlags(kotlinTypeParameterMetadata.flags) + - hasAnnotationsFlag(kotlinTypeParameterMetadata.flags.common) + kotlinTypeParameterMetadata.name ); @@ -912,8 +911,6 @@ else if (kotlinTypeMetadata.aliasName != null) } else { - pw.print(hasAnnotationsFlag(kotlinTypeMetadata.flags.common)); - if (kotlinTypeMetadata.className != null) { pw.print(externalClassName(kotlinTypeMetadata.className)); diff --git a/base/src/main/java/proguard/classfile/util/kotlin/KotlinMetadataInitializer.java b/base/src/main/java/proguard/classfile/util/kotlin/KotlinMetadataInitializer.java index af16337f7..5f4d673f9 100644 --- a/base/src/main/java/proguard/classfile/util/kotlin/KotlinMetadataInitializer.java +++ b/base/src/main/java/proguard/classfile/util/kotlin/KotlinMetadataInitializer.java @@ -961,13 +961,6 @@ else if (kmType.getClassifier() instanceof KmClassifier.TypeAlias) .map(KotlinAnnotationUtilKt::convertAnnotation) .collect(Collectors.toList()); - - //PROBBUG if a value parameter or a type parameter has an annotation then - // the annotation will be stored there but the flag will be - // incorrectly set on this type. Sometimes the flag is not set - // when there are annotations, sometimes the flag is set but there are no annotations. - type.flags.common.hasAnnotations = !type.annotations.isEmpty(); - return type; } @@ -1004,12 +997,6 @@ private static KotlinTypeParameterMetadata toKotlinTypeParameterMetadata(KmTypeP .map(KotlinAnnotationUtilKt::convertAnnotation) .collect(Collectors.toList()); - //PROBBUG if a value parameter or a type parameter has an annotation then - // the annotation will be stored there but the flag will be - // incorrectly set on this type. Sometimes the flag is not set - // when there are annotations, sometimes the flag is set but there are no annotations. - kotlinTypeParameterMetadata.flags.common.hasAnnotations = !kotlinTypeParameterMetadata.annotations.isEmpty(); - return kotlinTypeParameterMetadata; } @@ -1302,9 +1289,7 @@ private static KotlinFunctionFlags convertFunctionFlags(int kotlinFlags) private static KotlinTypeFlags convertTypeFlags(int kotlinFlags) { - KotlinTypeFlags flags = new KotlinTypeFlags( - convertCommonFlags(kotlinFlags) - ); + KotlinTypeFlags flags = new KotlinTypeFlags(); flags.isNullable = Flag.Type.IS_NULLABLE.invoke(kotlinFlags); flags.isSuspend = Flag.Type.IS_SUSPEND.invoke(kotlinFlags); @@ -1316,9 +1301,7 @@ private static KotlinTypeFlags convertTypeFlags(int kotlinFlags) private static KotlinTypeParameterFlags convertTypeParameterFlags(int kotlinFlags) { - KotlinTypeParameterFlags flags = new KotlinTypeParameterFlags( - convertCommonFlags(kotlinFlags) - ); + KotlinTypeParameterFlags flags = new KotlinTypeParameterFlags(); flags.isReified = Flag.TypeParameter.IS_REIFIED.invoke(kotlinFlags); diff --git a/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinCommonFlagsTest.kt b/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinCommonFlagsTest.kt index f9a8430f6..dfd547d84 100644 --- a/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinCommonFlagsTest.kt +++ b/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinCommonFlagsTest.kt @@ -183,17 +183,6 @@ internal fun testHasAnnotation(prefix: String, clazz: Clazz, expected: Boolean, } } - test("$prefix: the hasAnnotations flag of the type param in $className should be $expected") { - verify(exactly = 1) { - kotlinTypeParamVisitor.visitAnyTypeParameter( - clazz, - withArg { - it.flags.common.hasAnnotations shouldBe expected - } - ) - } - } - test("$prefix: the hasAnnotation flag of the value param in $className should be $expected") { verify(exactly = 1) { kotlinValueParamVisitor.visitAnyValueParameter( diff --git a/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeFlagsTest.kt b/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeFlagsTest.kt index b3751edb9..944f4c805 100644 --- a/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeFlagsTest.kt +++ b/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeFlagsTest.kt @@ -55,8 +55,6 @@ class KotlinTypeFlagsTest : FreeSpec({ withArg { it.flags.isNullable shouldBe false it.flags.isSuspend shouldBe false - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -73,8 +71,6 @@ class KotlinTypeFlagsTest : FreeSpec({ withArg { it.flags.isNullable shouldBe false it.flags.isSuspend shouldBe false - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -93,8 +89,6 @@ class KotlinTypeFlagsTest : FreeSpec({ withArg { it.flags.isNullable shouldBe true it.flags.isSuspend shouldBe false - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -110,8 +104,6 @@ class KotlinTypeFlagsTest : FreeSpec({ clazz, withArg { it.flags.isNullable shouldBe true - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -130,8 +122,6 @@ class KotlinTypeFlagsTest : FreeSpec({ withArg { it.flags.isNullable shouldBe false it.flags.isSuspend shouldBe true - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -148,8 +138,6 @@ class KotlinTypeFlagsTest : FreeSpec({ withArg { it.flags.isNullable shouldBe false it.flags.isSuspend shouldBe true - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -169,8 +157,6 @@ class KotlinTypeFlagsTest : FreeSpec({ it.flags.isNullable shouldBe false it.flags.isSuspend shouldBe false it.flags.isDefinitelyNonNull shouldBe true - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -214,8 +200,6 @@ class KotlinTypeFlagsTest : FreeSpec({ kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isNullable shouldBe false kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isSuspend shouldBe false kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isDefinitelyNonNull shouldBe false - - kotlinTypeMetadata.typeArguments?.get(0)?.flags?.common?.hasAnnotations shouldBe true } ) ) @@ -235,8 +219,6 @@ class KotlinTypeFlagsTest : FreeSpec({ kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isNullable shouldBe false kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isSuspend shouldBe false kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isDefinitelyNonNull shouldBe false - - kotlinTypeMetadata.typeArguments?.get(0)?.flags?.common?.hasAnnotations shouldBe true } ) ) @@ -267,8 +249,6 @@ class KotlinTypeFlagsTest : FreeSpec({ kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isNullable shouldBe true kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isSuspend shouldBe false kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isDefinitelyNonNull shouldBe false - - kotlinTypeMetadata.typeArguments?.get(0)?.flags?.common?.hasAnnotations shouldBe false } ) ) @@ -288,8 +268,6 @@ class KotlinTypeFlagsTest : FreeSpec({ kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isNullable shouldBe true kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isSuspend shouldBe false kotlinTypeMetadata.typeArguments?.get(0)?.flags?.isDefinitelyNonNull shouldBe false - - kotlinTypeMetadata.typeArguments?.get(0)?.flags?.common?.hasAnnotations shouldBe false } ) ) diff --git a/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeParameterFlagsTest.kt b/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeParameterFlagsTest.kt index 4c8aa5403..b3705d4c9 100644 --- a/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeParameterFlagsTest.kt +++ b/base/src/test/kotlin/proguard/classfile/kotlin/flags/KotlinTypeParameterFlagsTest.kt @@ -55,8 +55,6 @@ class KotlinTypeParameterFlagsTest : FreeSpec({ clazz, withArg { it.flags.isReified shouldBe false - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -72,8 +70,6 @@ class KotlinTypeParameterFlagsTest : FreeSpec({ clazz, withArg { it.flags.isReified shouldBe false - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -92,8 +88,6 @@ class KotlinTypeParameterFlagsTest : FreeSpec({ clazz, withArg { it.flags.isReified shouldBe true - - it.flags.common.hasAnnotations shouldBe false } ) } @@ -109,13 +103,58 @@ class KotlinTypeParameterFlagsTest : FreeSpec({ clazz, withArg { it.flags.isReified shouldBe true - - it.flags.common.hasAnnotations shouldBe false } ) } } } + + "Given a type parameter with annotation" - { + val clazz = ClassPoolBuilder.fromSource( + KotlinSource( + "Test.kt", + """ + @Target(AnnotationTarget.TYPE_PARAMETER) + annotation class Ann + inline fun <@Ann T> bar(): Int = 42 + """ + ) + ).programClassPool.getClass("TestKt") + + "Then the flags should be initialized correctly" { + clazz.accept( + ReferencedKotlinMetadataVisitor( + AllTypeParameterVisitor( + KotlinTypeParameterFilter( + { + true + }, + { _, kotlinTypeMetadata -> + kotlinTypeMetadata.flags.isReified shouldBe false + } + ) + ) + ) + ) + } + + "Then the flags should be written and re-initialized correctly" { + clazz.accept( + ReWritingMetadataVisitor( + AllTypeParameterVisitor( + KotlinTypeParameterFilter( + { + true + }, + { _, kotlinTypeMetadata -> + kotlinTypeMetadata.flags.isReified shouldBe false + } + ) + ) + ) + ) + } + } }) private fun createVisitor(typeName: String, typeVisitor: KotlinTypeParameterVisitor): KotlinMetadataVisitor = diff --git a/docs/md/releasenotes.md b/docs/md/releasenotes.md index 1178011fd..ec2c0505f 100644 --- a/docs/md/releasenotes.md +++ b/docs/md/releasenotes.md @@ -5,6 +5,8 @@ - Enable fix previously behind system property: fix `TypedReferenceValue.generalize()` not setting `mayBeExtension` to true when generalizing to common parent type. - Avoid printing `PartialEvaluator` messages when an `ExcessiveComplexityException` occurs. - Fix incorrect writing of flags for type parameters with name annotations. +- Fix incorrect writing of flags for reified type parameters. +- Fix model for types and type parameters, removing the incorrect `HAS_ANNOTATION` common flag. ### Improved