Skip to content

Commit 233b634

Browse files
committed
Fix IllegalAccessException on private annotation in reflection
#KT-14094 Fixed
1 parent fb60f4a commit 233b634

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// IGNORE_BACKEND: JS, NATIVE
2+
// WITH_REFLECT
3+
4+
annotation private class Ann(val name: String)
5+
6+
class A {
7+
@Ann("OK")
8+
fun foo() {}
9+
}
10+
11+
fun box(): String {
12+
val ann = A::class.members.single { it.name == "foo" }.annotations.single() as Ann
13+
return ann.name
14+
}

compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13843,6 +13843,12 @@ public void testFindAnnotation() throws Exception {
1384313843
doTest(fileName);
1384413844
}
1384513845

13846+
@TestMetadata("privateAnnotation.kt")
13847+
public void testPrivateAnnotation() throws Exception {
13848+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/privateAnnotation.kt");
13849+
doTest(fileName);
13850+
}
13851+
1384613852
@TestMetadata("propertyAccessors.kt")
1384713853
public void testPropertyAccessors() throws Exception {
1384813854
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");

compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13843,6 +13843,12 @@ public void testFindAnnotation() throws Exception {
1384313843
doTest(fileName);
1384413844
}
1384513845

13846+
@TestMetadata("privateAnnotation.kt")
13847+
public void testPrivateAnnotation() throws Exception {
13848+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/privateAnnotation.kt");
13849+
doTest(fileName);
13850+
}
13851+
1384613852
@TestMetadata("propertyAccessors.kt")
1384713853
public void testPropertyAccessors() throws Exception {
1384813854
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");

compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13843,6 +13843,12 @@ public void testFindAnnotation() throws Exception {
1384313843
doTest(fileName);
1384413844
}
1384513845

13846+
@TestMetadata("privateAnnotation.kt")
13847+
public void testPrivateAnnotation() throws Exception {
13848+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/privateAnnotation.kt");
13849+
doTest(fileName);
13850+
}
13851+
1384613852
@TestMetadata("propertyAccessors.kt")
1384713853
public void testPropertyAccessors() throws Exception {
1384813854
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");

core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/ReflectKotlinClass.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,16 @@ private object ReflectClassStructure {
169169
annotationType: Class<*>
170170
) {
171171
for (method in annotationType.declaredMethods) {
172-
processAnnotationArgumentValue(visitor, Name.identifier(method.name), method(annotation)!!)
172+
val value = try {
173+
method(annotation)!!
174+
}
175+
catch (e: IllegalAccessException) {
176+
// This is possible if the annotation class is package local. In this case, we can't read the value into descriptor.
177+
// However, this might be OK, because we do not use any data from AnnotationDescriptor in KAnnotatedElement implementations
178+
// anyway; we use the source element and the underlying physical Annotation object to implement the needed API
179+
continue
180+
}
181+
processAnnotationArgumentValue(visitor, Name.identifier(method.name), value)
173182
}
174183
visitor.visitEnd()
175184
}

js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15667,6 +15667,18 @@ public void testFindAnnotation() throws Exception {
1566715667
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
1566815668
}
1566915669

15670+
@TestMetadata("privateAnnotation.kt")
15671+
public void testPrivateAnnotation() throws Exception {
15672+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/privateAnnotation.kt");
15673+
try {
15674+
doTest(fileName);
15675+
}
15676+
catch (Throwable ignore) {
15677+
return;
15678+
}
15679+
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
15680+
}
15681+
1567015682
@TestMetadata("propertyAccessors.kt")
1567115683
public void testPropertyAccessors() throws Exception {
1567215684
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");

0 commit comments

Comments
 (0)