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:
@@ -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");
|
||||
|
||||
+3
@@ -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");
|
||||
|
||||
+6
-15
@@ -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(
|
||||
|
||||
+12
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user