Reflection: add KAnnotatedElement.findAnnotation

#KT-12250 Fixed
This commit is contained in:
Alexander Udalov
2016-07-12 19:54:30 +03:00
parent 414daef001
commit c811c1bea5
7 changed files with 89 additions and 0 deletions
@@ -0,0 +1,15 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.findAnnotation
annotation class Yes(val value: String)
annotation class No(val value: String)
@Yes("OK")
@No("Fail")
class Foo
fun box(): String {
return Foo::class.findAnnotation<Yes>()?.value ?: "Fail: no annotation"
}
@@ -0,0 +1,19 @@
public final class FindAnnotationKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@Yes
@No
public final class Foo {
public method <init>(): void
}
@java.lang.annotation.Retention
public annotation class No {
public abstract method value(): java.lang.String
}
@java.lang.annotation.Retention
public annotation class Yes {
public abstract method value(): java.lang.String
}
@@ -12199,6 +12199,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("findAnnotation.kt")
public void testFindAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt");
doTest(fileName);
}
@TestMetadata("propertyAccessors.kt")
public void testPropertyAccessors() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");
@@ -12199,6 +12199,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("findAnnotation.kt")
public void testFindAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt");
doTest(fileName);
}
@TestMetadata("propertyAccessors.kt")
public void testPropertyAccessors() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");
@@ -12199,6 +12199,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis
doTest(fileName);
}
@TestMetadata("findAnnotation.kt")
public void testFindAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt");
doTest(fileName);
}
@TestMetadata("propertyAccessors.kt")
public void testPropertyAccessors() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* 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.
*/
@file:JvmName("KAnnotatedElements")
package kotlin.reflect
/**
* Returns an annotation of the given type on this element.
*/
@Suppress("UNCHECKED_CAST")
inline fun <reified T : Annotation> KAnnotatedElement.findAnnotation(): T? =
annotations.first { it is T } as T
@@ -14894,6 +14894,18 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
}
@TestMetadata("findAnnotation.kt")
public void testFindAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/findAnnotation.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
}
@TestMetadata("propertyAccessors.kt")
public void testPropertyAccessors() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/annotations/propertyAccessors.kt");