Annotations have now retention of "RUNTIME" by default. Java retention is generated as given by kotlin annotation. Annotation rendering changed.

Annotation arguments with default values are rendered as ... if renderDefaultAnnotationArguments is true.
Tests: java retention does not taken into account by Descriptor comparator.
Java retentinon changed to kotlin retention in some tests + one new test with java retention added.
More accurate tests for intentions in byte code (visibility controlled).
This commit is contained in:
Mikhail Glukhikh
2015-07-03 12:11:08 +03:00
parent 4a27b4d614
commit 609d696202
68 changed files with 214 additions and 242 deletions
@@ -119,6 +119,12 @@ public abstract class AnnotationCodegen {
generateNullabilityAnnotation(descriptor.getReturnType(), annotationDescriptorsAlreadyPresent);
}
}
if (annotated instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) annotated;
if (classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS) {
generateRetentionAnnotation(classDescriptor, annotationDescriptorsAlreadyPresent);
}
}
}
private static boolean isInvisibleFromTheOutside(@Nullable DeclarationDescriptor descriptor) {
@@ -160,6 +166,15 @@ public abstract class AnnotationCodegen {
generateAnnotationIfNotPresent(annotationDescriptorsAlreadyPresent, annotationClass);
}
private void generateRetentionAnnotation(@NotNull ClassDescriptor classDescriptor, @NotNull Set<String> annotationDescriptorsAlreadyPresent) {
RetentionPolicy policy = getRetentionPolicy(classDescriptor);
String descriptor = Type.getType(Retention.class).getDescriptor();
if (!annotationDescriptorsAlreadyPresent.add(descriptor)) return;
AnnotationVisitor visitor = visitAnnotation(descriptor, true);
visitor.visitEnum("value", Type.getType(RetentionPolicy.class).getDescriptor(), policy.name());
visitor.visitEnd();
}
private void generateAnnotationIfNotPresent(Set<String> annotationDescriptorsAlreadyPresent, Class<?> annotationClass) {
String descriptor = Type.getType(annotationClass).getDescriptor();
if (!annotationDescriptorsAlreadyPresent.contains(descriptor)) {
@@ -318,8 +333,37 @@ public abstract class AnnotationCodegen {
value.accept(argumentVisitor, null);
}
private enum KotlinRetention {
SOURCE(RetentionPolicy.SOURCE),
BINARY(RetentionPolicy.CLASS),
RUNTIME(RetentionPolicy.RUNTIME);
final RetentionPolicy mapped;
KotlinRetention(RetentionPolicy mapped) {
this.mapped = mapped;
}
}
@NotNull
private RetentionPolicy getRetentionPolicy(@NotNull Annotated descriptor) {
AnnotationDescriptor kotlinAnnotation = descriptor.getAnnotations().findAnnotation(KotlinBuiltIns.FQ_NAMES.annotation);
if (kotlinAnnotation != null) {
for (Map.Entry<ValueParameterDescriptor, CompileTimeConstant<?>> argument: kotlinAnnotation.getAllValueArguments().entrySet()) {
if ("retention".equals(argument.getKey().getName().asString()) && argument.getValue() instanceof EnumValue) {
ClassDescriptor enumEntry = ((EnumValue) argument.getValue()).getValue();
JetType classObjectType = getClassObjectType(enumEntry);
if (classObjectType != null) {
if ("kotlin/annotation/AnnotationRetention".equals(typeMapper.mapType(classObjectType).getInternalName())) {
String entryName = enumEntry.getName().asString();
for (KotlinRetention retention: KotlinRetention.values()) {
if (retention.name().equals(entryName)) return retention.mapped;
}
}
}
}
}
}
AnnotationDescriptor retentionAnnotation = descriptor.getAnnotations().findAnnotation(new FqName(Retention.class.getName()));
if (retentionAnnotation != null) {
Collection<CompileTimeConstant<?>> valueArguments = retentionAnnotation.getAllValueArguments().values();
@@ -337,7 +381,7 @@ public abstract class AnnotationCodegen {
}
}
return RetentionPolicy.CLASS;
return RetentionPolicy.RUNTIME;
}
@NotNull
@@ -1,6 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b) class MyClass
fun box(): String {
@@ -15,8 +12,7 @@ fun box(): String {
return "OK"
}
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val i: Int,
val s: Short,
val f: Float,
@@ -1,6 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b, Foo.bool, Foo.c, Foo.str) class MyClass
fun box(): String {
@@ -18,8 +15,7 @@ fun box(): String {
return "OK"
}
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val i: Int,
val s: Short,
val f: Float,
@@ -1,6 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b, Foo.c) class MyClass
fun box(): String {
@@ -16,8 +13,7 @@ fun box(): String {
return "OK"
}
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val i: Int,
val s: Short,
val f: Float,
@@ -1,10 +1,8 @@
import java.lang.annotation.*
import java.lang.reflect.Method
import kotlin.reflect.jvm.java
import kotlin.test.assertEquals
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(val x: String)
annotation(retention = AnnotationRetention.RUNTIME) class Ann(val x: String)
fun testMethod(method: Method, name: String) {
assertEquals("OK", method.getAnnotation(javaClass<Ann>()).x, "On method of test named `$name`")
@@ -1,8 +1,6 @@
import kotlin.reflect.KClass
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation
annotation(retention = AnnotationRetention.RUNTIME)
class Ann(val args: Array<KClass<*>>)
fun box(): String {
@@ -1,8 +1,6 @@
import kotlin.reflect.KClass
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation
annotation(retention = AnnotationRetention.RUNTIME)
class Ann(val arg: KClass<*>)
fun box(): String {
@@ -1,8 +1,6 @@
import kotlin.reflect.KClass
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation
annotation(retention = AnnotationRetention.RUNTIME)
class Ann(vararg val args: KClass<*>)
fun box(): String {
@@ -1,9 +1,6 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import kotlin.platform.platformStatic
Retention(RetentionPolicy.RUNTIME)
annotation class testAnnotation
annotation(retention = AnnotationRetention.RUNTIME) class testAnnotation
class A {
@@ -1,12 +1,8 @@
// KT-5665
import java.lang.annotation.*
annotation(retention = AnnotationRetention.RUNTIME) class First
Retention(RetentionPolicy.RUNTIME)
annotation class First
Retention(RetentionPolicy.RUNTIME)
annotation class Second(val value: String)
annotation(retention = AnnotationRetention.RUNTIME) class Second(val value: String)
enum class E {
@First
@@ -1,10 +1,8 @@
import java.lang.annotation.*
import java.lang.reflect.Method
import kotlin.reflect.jvm.java
import kotlin.test.assertEquals
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(val x: String)
annotation(retention = AnnotationRetention.RUNTIME) class Ann(val x: String)
fun foo0(block: () -> Unit) = block.javaClass
fun foo1(block: (String) -> Unit) = block.javaClass
@@ -1,8 +1,6 @@
import java.lang.annotation.*
import kotlin.test.assertEquals
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(val x: Int)
annotation(retention = AnnotationRetention.RUNTIME) class Ann(val x: Int)
class A {
Ann(1) fun foo(x: Int, y: Int = 2, z: Int) {}
@@ -1,10 +1,7 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import kotlin.reflect.KClass
import kotlin.reflect.jvm.java
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val i: Int = 1,
val s: String = "a",
val a: Ann2 = Ann2(),
@@ -1,7 +1,4 @@
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation class First
annotation(retention = AnnotationRetention.RUNTIME) class First
class MyClass() {
public var x: String by Delegate()
@@ -1,6 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b, Foo.bool, Foo.c, Foo.str) class MyClass
fun box(): String {
@@ -18,8 +15,7 @@ fun box(): String {
return "OK"
}
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val i: Int,
val s: Short,
val f: Float,
@@ -1,6 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann(i, s, f, d, l, b, bool, c, str) class MyClass
fun box(): String {
@@ -18,8 +15,7 @@ fun box(): String {
return "OK"
}
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val i: Int,
val s: Short,
val f: Float,
@@ -1,6 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann(A.B.i) class MyClass
fun box(): String {
@@ -10,8 +7,7 @@ fun box(): String {
return "OK"
}
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(val i: Int)
annotation(retention = AnnotationRetention.RUNTIME) class Ann(val i: Int)
class A {
class B {
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val b: Byte,
val s: Short,
val i: Int,
@@ -1,6 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann(i) class MyClass
fun box(): String {
@@ -10,8 +7,7 @@ fun box(): String {
return "OK"
}
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(val i: Int)
annotation(retention = AnnotationRetention.RUNTIME) class Ann(val i: Int)
val i2: Int = 1
val i: Int = i2
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(vararg val p: Int)
annotation(retention = AnnotationRetention.RUNTIME) class Ann(vararg val p: Int)
Ann() class MyClass1
Ann(1) class MyClass2
+1 -5
View File
@@ -1,10 +1,6 @@
package test
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(val c1: Int)
annotation(retention = AnnotationRetention.RUNTIME) class Ann(val c1: Int)
Ann('a' - 'a') class MyClass
+1 -5
View File
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val b: Byte,
val s: Short,
val i: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Int,
val p2: Int,
val p3: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Int,
val p2: Short,
val p3: Byte,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Int,
val p2: Int,
val p3: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Int,
val p2: Byte,
val p4: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Int,
val p2: Int,
val p4: Long,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Byte,
val p2: Short,
val p3: Int,
+1 -5
View File
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Byte,
val p2: Short,
val p3: Int,
+1 -5
View File
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Byte,
val p2: Short,
val p3: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Byte,
val p2: Short,
val p3: Int,
+1 -5
View File
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Byte,
val p2: Short,
val p3: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Int,
val p2: Int,
val p3: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Byte,
val p2: Short,
val p3: Int,
@@ -1,8 +1,4 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val p1: Byte,
val p2: Short,
val p3: Int,
@@ -1,8 +1,6 @@
import kotlin.reflect.KClass
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation
annotation(retention = AnnotationRetention.RUNTIME)
class Ann(val args: Array<KClass<*>>)
class O
@@ -1,8 +1,6 @@
import kotlin.reflect.KClass
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation
annotation(retention = AnnotationRetention.RUNTIME)
class Ann(val arg: KClass<*>)
class OK
@@ -1,8 +1,6 @@
import kotlin.reflect.KClass
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation
annotation(retention = AnnotationRetention.RUNTIME)
class Ann(vararg val args: KClass<*>)
class O
@@ -1,8 +1,6 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Annotation
Retention(RetentionPolicy.RUNTIME) annotation class foo(val name : String)
annotation(retention = AnnotationRetention.RUNTIME) class foo(val name : String)
class Test() {
foo("OK") fun hello(input : String) {
@@ -0,0 +1,9 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann class MyClass
Retention(RetentionPolicy.RUNTIME)
annotation class Ann
// 1 @LAnn;()
@@ -1,9 +1,7 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann class MyClass
Retention(RetentionPolicy.CLASS)
annotation class Ann
annotation(retention = AnnotationRetention.BINARY) class Ann
// 1 @LAnn;()
// 1 invisible
// 1 @LAnn;()
@@ -1,9 +1,6 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann class MyClass
Retention(RetentionPolicy.RUNTIME)
annotation class Ann
annotation(retention = AnnotationRetention.RUNTIME) class Ann
// 1 @LAnn;()
// 1 @LAnn;()
// 0 invisible
@@ -1,9 +1,5 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
Ann class MyClass
Retention(RetentionPolicy.SOURCE)
annotation class Ann
annotation(retention = AnnotationRetention.SOURCE) class Ann
// 0 @LAnn;()
@@ -1,6 +1,4 @@
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME) annotation class SomeAnnotation(val value: String)
annotation(retention = AnnotationRetention.RUNTIME) class SomeAnnotation(val value: String)
class A {
@SomeAnnotation("OK") val property: Int
@@ -1,6 +1,4 @@
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME) annotation class SomeAnnotation(val value: String)
annotation(retention = AnnotationRetention.RUNTIME) class SomeAnnotation(val value: String)
interface T {
@SomeAnnotation("OK") val property: Int
@@ -1,6 +1,4 @@
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME) annotation class SomeAnnotation(val value: String)
annotation(retention = AnnotationRetention.RUNTIME) class SomeAnnotation(val value: String)
@SomeAnnotation("OK") val property: Int
get() = 42
@@ -1,9 +1,6 @@
package a
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation class Ann
annotation(retention = AnnotationRetention.RUNTIME) class Ann
interface Tr {
Ann
@@ -1,8 +1,5 @@
package constants
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
public val b: Byte = 100
public val s: Short = 20000
public val i: Int = 2000000
@@ -14,5 +11,4 @@ public val c: Char = '\u03c0' // pi symbol
public val str: String = ":)"
@Retention(RetentionPolicy.RUNTIME)
public annotation class AnnotationClass(public val value: String)
public annotation(retention = AnnotationRetention.RUNTIME) class AnnotationClass(public val value: String)
@@ -1,5 +1,3 @@
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import a.*
Ann(i, s, f, d, l, b, bool, c, str)
@@ -8,8 +6,7 @@ class MyClass1
Ann(i2, s2, f2, d2, l2, b2, bool2, c2, str2)
class MyClass2
Retention(RetentionPolicy.RUNTIME)
annotation class Ann(
annotation(retention = AnnotationRetention.RUNTIME) class Ann(
val i: Int,
val s: Short,
val f: Float,
@@ -1,13 +1,13 @@
package
kotlin.annotation.annotation() java.lang.annotation.Retention(value = RetentionPolicy.CLASS) internal final annotation class my : kotlin.Annotation {
kotlin.annotation.annotation() internal final annotation class my : kotlin.Annotation {
public constructor my()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
kotlin.annotation.annotation() java.lang.annotation.Retention(value = RetentionPolicy.RUNTIME) java.lang.annotation.Target(value = {ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR}) internal final annotation class my1 : kotlin.Annotation {
kotlin.annotation.annotation() java.lang.annotation.Target(value = {ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR}) internal final annotation class my1 : kotlin.Annotation {
public constructor my1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
@@ -0,0 +1,6 @@
// ALLOW_AST_ACCESS
package test
target(AnnotationTarget.CLASSIFIER)
public annotation class TargetedAnnotation
@@ -0,0 +1,5 @@
package test
kotlin.annotation.target(allowedTargets = {AnnotationTarget.CLASSIFIER}) kotlin.annotation.annotation() public final annotation class TargetedAnnotation : kotlin.Annotation {
/*primary*/ public constructor TargetedAnnotation()
}
@@ -0,0 +1,3 @@
package test
data class My(val x: Int)
@@ -0,0 +1,9 @@
package test
kotlin.data() internal final class My {
/*primary*/ public constructor My(/*0*/ x: kotlin.Int)
internal final val x: kotlin.Int
internal final fun <get-x>(): kotlin.Int
internal final /*synthesized*/ fun component1(): kotlin.Int
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Int = ...): test.My
}
@@ -2,6 +2,4 @@
//SKIP_IN_RUNTIME_TEST
package test
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME) annotation class Anno
annotation(retention = AnnotationRetention.RUNTIME) class Anno
@@ -1,5 +1,5 @@
package test
java.lang.annotation.Retention(value = RetentionPolicy.RUNTIME) kotlin.annotation.annotation() internal final annotation class Anno : kotlin.Annotation {
kotlin.annotation.annotation(retention = AnnotationRetention.RUNTIME) internal final annotation class Anno : kotlin.Annotation {
/*primary*/ public constructor Anno()
}
@@ -47,6 +47,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("annotationJavaRetentionPolicyRuntime.kt")
public void testAnnotationJavaRetentionPolicyRuntime() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/annotationJavaRetentionPolicyRuntime.kt");
doTest(fileName);
}
@TestMetadata("annotationRetentionPolicyClass.kt")
public void testAnnotationRetentionPolicyClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/annotationRetentionPolicyClass.kt");
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.analyzer.AnalysisResult;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.renderer.DescriptorRendererOptions;
@@ -35,6 +36,7 @@ import org.junit.Assert;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.util.Collections;
import static org.jetbrains.kotlin.test.JetTestUtils.*;
@@ -53,6 +55,7 @@ public abstract class AbstractCompileJavaAgainstKotlinTest extends TestCaseWithT
options.setWithDefinedIn(false);
options.setParameterNameRenderingPolicy(ParameterNameRenderingPolicy.NONE);
options.setVerbose(true);
options.setExcludedAnnotationClasses(Collections.singleton(new FqName(Retention.class.getName())));
return Unit.INSTANCE$;
}
}
@@ -1942,6 +1942,12 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
doTestCompiledKotlin(fileName);
}
@TestMetadata("TargetedAnnotation.kt")
public void testTargetedAnnotation() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/TargetedAnnotation.kt");
doTestCompiledKotlin(fileName);
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classMembers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -2031,6 +2037,12 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
doTestCompiledKotlin(fileName);
}
@TestMetadata("DataClass.kt")
public void testDataClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classes/DataClass.kt");
doTestCompiledKotlin(fileName);
}
@TestMetadata("Deprecated.kt")
public void testDeprecated() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classes/Deprecated.kt");
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.Configuratio
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.utils.sure
import java.io.File
import java.lang.annotation.Retention
import java.net.URLClassLoader
import java.util.regex.Pattern
@@ -52,21 +53,18 @@ public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdi
private val renderer = DescriptorRenderer.withOptions {
withDefinedIn = false
excludedAnnotationClasses = (listOf(
ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME,
// TODO: add these annotations when they are retained at runtime
"kotlin.deprecated",
"kotlin.data",
"kotlin.inline"
).map { FqName(it) } + JvmAnnotationNames.ANNOTATIONS_COPIED_TO_TYPES).toSet()
FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)
) + JvmAnnotationNames.ANNOTATIONS_COPIED_TO_TYPES).toSet()
overrideRenderingPolicy = OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE
parameterNameRenderingPolicy = ParameterNameRenderingPolicy.NONE
includePropertyConstant = false
verbose = true
renderDefaultAnnotationArguments = true
}
}
// NOTE: this test does a dirty hack of text substitution to make all annotations defined in source code retain at runtime.
// Specifically each "annotation class" in Kotlin sources is replaced by "Retention(RUNTIME) annotation class", and the same in Java
// Specifically each @interface in Java sources is extended by @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
// Also type related annotations are removed from Java because they are invisible at runtime
protected fun doTest(fileName: String) {
val file = File(fileName)
@@ -123,7 +121,7 @@ public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdi
val environment = JetTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(
myTestRootDisposable, ConfigurationKind.ALL, jdkKind
)
val jetFile = JetTestUtils.createFile(file.getPath(), addRuntimeRetentionToKotlinSource(text), environment.project)
val jetFile = JetTestUtils.createFile(file.getPath(), text, environment.project)
GenerationUtils.compileFileGetClassFileFactoryForTest(jetFile).writeAllTo(tmpdir)
}
}
@@ -166,13 +164,6 @@ public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdi
return SyntheticPackageViewForTest(module, packageScopes, classes)
}
private fun addRuntimeRetentionToKotlinSource(text: String): String {
return text.replace(
"annotation class",
"@[java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)] annotation class"
)
}
private fun adaptJavaSource(text: String): String {
val typeAnnotations = arrayOf("NotNull", "Nullable", "ReadOnly", "Mutable")
return typeAnnotations.fold(text) { text, annotation -> text.replace("@$annotation", "") }.replace(
@@ -81,6 +81,12 @@ public class JvmRuntimeDescriptorLoaderTestGenerated extends AbstractJvmRuntimeD
doTest(fileName);
}
@TestMetadata("TargetedAnnotation.kt")
public void testTargetedAnnotation() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/TargetedAnnotation.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classMembers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -170,6 +176,12 @@ public class JvmRuntimeDescriptorLoaderTestGenerated extends AbstractJvmRuntimeD
doTest(fileName);
}
@TestMetadata("DataClass.kt")
public void testDataClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classes/DataClass.kt");
doTest(fileName);
}
@TestMetadata("Deprecated.kt")
public void testDeprecated() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classes/Deprecated.kt");
@@ -40,20 +40,26 @@ import org.jetbrains.kotlin.utils.Printer;
import org.junit.Assert;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.lang.annotation.Retention;
import java.util.*;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry;
import static org.jetbrains.kotlin.test.util.DescriptorValidator.ValidationVisitor.errorTypesForbidden;
public class RecursiveDescriptorComparator {
private static final Set<FqName> excludedAnnotations = new HashSet<FqName>();
static {
excludedAnnotations.add(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME));
excludedAnnotations.add(new FqName(Retention.class.getName()));
}
private static final DescriptorRenderer DEFAULT_RENDERER = DescriptorRenderer.Companion.withOptions(
new Function1<DescriptorRendererOptions, Unit>() {
@Override
public Unit invoke(DescriptorRendererOptions options) {
options.setWithDefinedIn(false);
options.setExcludedAnnotationClasses(Collections.singleton(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)));
options.setExcludedAnnotationClasses(excludedAnnotations);
options.setOverrideRenderingPolicy(OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE);
options.setIncludePropertyConstant(true);
options.setNameShortness(NameShortness.FULLY_QUALIFIED);
@@ -176,6 +176,7 @@ public interface DescriptorRendererOptions {
public var flexibleTypesForCode: Boolean
public var secondaryConstructorsAsPrimary: Boolean
public var renderAccessors: Boolean
public var renderDefaultAnnotationArguments: Boolean
}
public enum class RenderingFormat {
@@ -366,13 +366,22 @@ internal class DescriptorRendererImpl(
}
private fun renderAndSortAnnotationArguments(descriptor: AnnotationDescriptor): List<String> {
return descriptor.getAllValueArguments().entrySet()
val allValueArguments = descriptor.getAllValueArguments()
val classDescriptor = if (renderDefaultAnnotationArguments) TypeUtils.getClassDescriptor(descriptor.getType()) else null
val parameterDescriptorsWithDefaultValue = classDescriptor?.getUnsubstitutedPrimaryConstructor()?.getValueParameters()?.filter {
it.declaresDefaultValue()
} ?: emptyList()
val defaultList = parameterDescriptorsWithDefaultValue.filter { !allValueArguments.containsKey(it) }.map {
"${it.getName().asString()} = ..."
}.sort()
val argumentList = allValueArguments.entrySet()
.map { entry ->
val name = entry.key.getName().asString()
val value = renderConstant(entry.value)
val value = if (!parameterDescriptorsWithDefaultValue.contains(entry.key)) renderConstant(entry.value) else "..."
"$name = $value"
}
.sort()
return (defaultList + argumentList).sort()
}
private fun renderConstant(value: CompileTimeConstant<*>): String {
@@ -85,6 +85,7 @@ internal class DescriptorRendererOptionsImpl : DescriptorRendererOptions {
override var receiverAfterName by property(false)
override var renderCompanionObjectName by property(false)
override var renderAccessors by property(false)
override var renderDefaultAnnotationArguments by property(false)
override var excludedAnnotationClasses by property(emptySet<FqName>())
@@ -79,6 +79,12 @@ public class ResolveByStubTestGenerated extends AbstractResolveByStubTest {
doTest(fileName);
}
@TestMetadata("TargetedAnnotation.kt")
public void testTargetedAnnotation() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/TargetedAnnotation.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classMembers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -168,6 +174,12 @@ public class ResolveByStubTestGenerated extends AbstractResolveByStubTest {
doTest(fileName);
}
@TestMetadata("DataClass.kt")
public void testDataClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classes/DataClass.kt");
doTest(fileName);
}
@TestMetadata("Deprecated.kt")
public void testDeprecated() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/annotations/classes/Deprecated.kt");
+1 -4
View File
@@ -3,11 +3,8 @@ package test.annotations
import kotlin.*
import kotlin.test.assertTrue
import org.junit.Test as test
import java.lang.annotation.*
Retention(RetentionPolicy.RUNTIME)
annotation class MyAnno
annotation(retention = AnnotationRetention.RUNTIME) class MyAnno
MyAnno
Deprecated
@@ -1,6 +1,8 @@
a kotlin.annotation.annotation 0
p org.test 0
c 0 0/SomeAnnotation
a org.test.SomeAnnotation 1
c 1 0/SomeClass
m 1 0/SomeClass annotatedFunction
a java.lang.annotation.Retention 1
c 1 0/SomeAnnotation
a org.test.SomeAnnotation 2
c 2 0/SomeClass
m 2 0/SomeClass annotatedFunction