Deserialize annotations on classes

First version, not all kinds of value arguments of annotations are supported
This commit is contained in:
Alexander Udalov
2013-07-04 21:04:53 +04:00
parent 9a33567287
commit 4aeeafdff0
31 changed files with 804 additions and 152 deletions
@@ -26,14 +26,15 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.ConfigurationKind;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationDeserializer;
import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedClassDescriptor;
import org.jetbrains.jet.di.InjectorForJavaDescriptorResolver;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.NamespaceDescriptorImpl;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.java.resolver.DeserializedDescriptorResolver;
import org.jetbrains.jet.lang.resolve.lazy.KotlinTestWithEnvironment;
import org.jetbrains.jet.lang.resolve.lazy.LazyResolveTestUtil;
import org.jetbrains.jet.lang.resolve.lazy.storage.LockBasedStorageManager;
@@ -44,17 +45,48 @@ import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.test.util.NamespaceComparator;
import java.io.*;
import java.util.*;
import static org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationDeserializer.UNSUPPORTED;
import static org.jetbrains.jet.lang.resolve.java.resolver.DeserializedResolverUtils.naiveKotlinFqName;
public abstract class AbstractDescriptorSerializationTest extends KotlinTestWithEnvironment {
public static final Name TEST_PACKAGE_NAME = Name.identifier("test");
public static final AnnotationDeserializer DUMMY_ANNOTATION_DESERIALIZER = new AnnotationDeserializer() {
@NotNull
@Override
public List<AnnotationDescriptor> loadClassAnnotations(@NotNull ClassDescriptor descriptor, @NotNull ProtoBuf.Class classProto) {
// This is a hack for tests: only data annotations are present in test data so far
AnnotationDescriptor annotationDescriptor = new AnnotationDescriptor();
annotationDescriptor.setAnnotationType(KotlinBuiltIns.getInstance().getDataClassAnnotation().getDefaultType());
return Collections.singletonList(annotationDescriptor);
}
@NotNull
@Override
public List<AnnotationDescriptor> loadCallableAnnotations(@NotNull ProtoBuf.Callable callableProto) {
throw new UnsupportedOperationException(); // TODO
}
@NotNull
@Override
public List<AnnotationDescriptor> loadSetterAnnotations(@NotNull ProtoBuf.Callable callableProto) {
throw new UnsupportedOperationException(); // TODO
}
@NotNull
@Override
public List<AnnotationDescriptor> loadValueParameterAnnotations(@NotNull ProtoBuf.Callable.ValueParameter parameterProto) {
throw new UnsupportedOperationException(); // TODO
}
};
@Override
protected JetCoreEnvironment createEnvironment() {
return createEnvironmentWithMockJdk(ConfigurationKind.JDK_ONLY);
@@ -124,7 +156,7 @@ public abstract class AbstractDescriptorSerializationTest extends KotlinTestWith
ProtoBuf.QualifiedNameTable qualifiedNames = ProtoBuf.QualifiedNameTable.parseDelimitedFrom(in);
ProtoBuf.Class proto = ProtoBuf.Class.parseFrom(in);
classMetadata.put(getNaiveFqName(classDescriptor), new ClassMetadata(simpleNames, qualifiedNames, proto));
classMetadata.put(naiveKotlinFqName(classDescriptor).asString(), new ClassMetadata(simpleNames, qualifiedNames, proto));
}
NamespaceDescriptorImpl namespace = JetTestUtils.createTestNamespace(TEST_PACKAGE_NAME);
@@ -184,25 +216,6 @@ public abstract class AbstractDescriptorSerializationTest extends KotlinTestWith
return namespace;
}
private static String getNaiveFqName(ClassDescriptor classDescriptor) {
return getNaiveFqName(classDescriptor, new StringBuilder()).toString();
}
@NotNull
private static StringBuilder getNaiveFqName(@NotNull DeclarationDescriptor descriptor, @NotNull StringBuilder builder) {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (containingDeclaration instanceof ClassDescriptor
|| (containingDeclaration instanceof NamespaceDescriptor
&& !DescriptorUtils.isRootNamespace((NamespaceDescriptor) containingDeclaration))) {
getNaiveFqName(containingDeclaration, builder);
builder.append(".");
}
builder.append(descriptor.getName().asString());
return builder;
}
public static void serialize(
List<ClassDescriptor> classes, List<CallableMemberDescriptor> callables,
Map<ClassDescriptor, byte[]> serializedClasses, OutputStream serializedCallables
@@ -309,7 +322,7 @@ public abstract class AbstractDescriptorSerializationTest extends KotlinTestWith
NameResolver nameResolver = new NameResolver(classMetadata.simpleNames, classMetadata.qualifiedNames);
return new DeserializedClassDescriptor(classId, new LockBasedStorageManager(), containingDeclaration, nameResolver,
DeserializedDescriptorResolver.DUMMY_ANNOTATION_DESERIALIZER, this,
DUMMY_ANNOTATION_DESERIALIZER, this,
classMetadata.classProto, null);
}
@@ -31,12 +31,75 @@ import org.jetbrains.jet.descriptors.serialization.AbstractDescriptorSerializati
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/loadKotlin")
@InnerTestClasses({DescriptorSerializationTestGenerated.Class.class, DescriptorSerializationTestGenerated.ClassFun.class, DescriptorSerializationTestGenerated.ClassObject.class, DescriptorSerializationTestGenerated.Constructor.class, DescriptorSerializationTestGenerated.DataClass.class, DescriptorSerializationTestGenerated.Fun.class, DescriptorSerializationTestGenerated.Prop.class, DescriptorSerializationTestGenerated.Type.class, DescriptorSerializationTestGenerated.Visibility.class})
@InnerTestClasses({DescriptorSerializationTestGenerated.Annotations.class, DescriptorSerializationTestGenerated.Class.class, DescriptorSerializationTestGenerated.ClassFun.class, DescriptorSerializationTestGenerated.ClassObject.class, DescriptorSerializationTestGenerated.Constructor.class, DescriptorSerializationTestGenerated.DataClass.class, DescriptorSerializationTestGenerated.Fun.class, DescriptorSerializationTestGenerated.Prop.class, DescriptorSerializationTestGenerated.Type.class, DescriptorSerializationTestGenerated.Visibility.class})
public class DescriptorSerializationTestGenerated extends AbstractDescriptorSerializationTest {
public void testAllFilesPresentInLoadKotlin() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/loadKotlin/annotations")
@InnerTestClasses({Annotations.Classes.class})
public static class Annotations extends AbstractDescriptorSerializationTest {
public void testAllFilesPresentInAnnotations() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin/annotations"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/loadKotlin/annotations/classes")
public static class Classes extends AbstractDescriptorSerializationTest {
public void testAllFilesPresentInClasses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin/annotations/classes"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("ClassInClassObject.kt")
public void testClassInClassObject() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/ClassInClassObject.kt");
}
@TestMetadata("ClassObject.kt")
public void testClassObject() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/ClassObject.kt");
}
@TestMetadata("Deprecated.kt")
public void testDeprecated() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/Deprecated.kt");
}
@TestMetadata("MultipleAnnotations.kt")
public void testMultipleAnnotations() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/MultipleAnnotations.kt");
}
@TestMetadata("NestedClass.kt")
public void testNestedClass() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/NestedClass.kt");
}
@TestMetadata("Simple.kt")
public void testSimple() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/Simple.kt");
}
@TestMetadata("WithArgument.kt")
public void testWithArgument() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/WithArgument.kt");
}
@TestMetadata("WithMultipleArguments.kt")
public void testWithMultipleArguments() throws Exception {
doTest("compiler/testData/loadKotlin/annotations/classes/WithMultipleArguments.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Annotations");
suite.addTestSuite(Annotations.class);
suite.addTestSuite(Classes.class);
return suite;
}
}
@TestMetadata("compiler/testData/loadKotlin/class")
public static class Class extends AbstractDescriptorSerializationTest {
public void testAllFilesPresentInClass() throws Exception {
@@ -1129,6 +1192,7 @@ public class DescriptorSerializationTestGenerated extends AbstractDescriptorSeri
public static Test suite() {
TestSuite suite = new TestSuite("DescriptorSerializationTestGenerated");
suite.addTestSuite(DescriptorSerializationTestGenerated.class);
suite.addTest(Annotations.innerSuite());
suite.addTestSuite(Class.class);
suite.addTestSuite(ClassFun.class);
suite.addTestSuite(ClassObject.class);
@@ -31,12 +31,75 @@ import org.jetbrains.jet.jvm.compiler.AbstractLoadCompiledKotlinTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/loadKotlin")
@InnerTestClasses({LoadCompiledKotlinTestGenerated.Class.class, LoadCompiledKotlinTestGenerated.ClassFun.class, LoadCompiledKotlinTestGenerated.ClassObject.class, LoadCompiledKotlinTestGenerated.Constructor.class, LoadCompiledKotlinTestGenerated.DataClass.class, LoadCompiledKotlinTestGenerated.Fun.class, LoadCompiledKotlinTestGenerated.Prop.class, LoadCompiledKotlinTestGenerated.Type.class, LoadCompiledKotlinTestGenerated.Visibility.class})
@InnerTestClasses({LoadCompiledKotlinTestGenerated.Annotations.class, LoadCompiledKotlinTestGenerated.Class.class, LoadCompiledKotlinTestGenerated.ClassFun.class, LoadCompiledKotlinTestGenerated.ClassObject.class, LoadCompiledKotlinTestGenerated.Constructor.class, LoadCompiledKotlinTestGenerated.DataClass.class, LoadCompiledKotlinTestGenerated.Fun.class, LoadCompiledKotlinTestGenerated.Prop.class, LoadCompiledKotlinTestGenerated.Type.class, LoadCompiledKotlinTestGenerated.Visibility.class})
public class LoadCompiledKotlinTestGenerated extends AbstractLoadCompiledKotlinTest {
public void testAllFilesPresentInLoadKotlin() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/loadKotlin/annotations")
@InnerTestClasses({Annotations.Classes.class})
public static class Annotations extends AbstractLoadCompiledKotlinTest {
public void testAllFilesPresentInAnnotations() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin/annotations"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/loadKotlin/annotations/classes")
public static class Classes extends AbstractLoadCompiledKotlinTest {
public void testAllFilesPresentInClasses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin/annotations/classes"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("ClassInClassObject.kt")
public void testClassInClassObject() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/ClassInClassObject.kt");
}
@TestMetadata("ClassObject.kt")
public void testClassObject() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/ClassObject.kt");
}
@TestMetadata("Deprecated.kt")
public void testDeprecated() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/Deprecated.kt");
}
@TestMetadata("MultipleAnnotations.kt")
public void testMultipleAnnotations() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/MultipleAnnotations.kt");
}
@TestMetadata("NestedClass.kt")
public void testNestedClass() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/NestedClass.kt");
}
@TestMetadata("Simple.kt")
public void testSimple() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/Simple.kt");
}
@TestMetadata("WithArgument.kt")
public void testWithArgument() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/WithArgument.kt");
}
@TestMetadata("WithMultipleArguments.kt")
public void testWithMultipleArguments() throws Exception {
doTestWithAccessors("compiler/testData/loadKotlin/annotations/classes/WithMultipleArguments.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Annotations");
suite.addTestSuite(Annotations.class);
suite.addTestSuite(Classes.class);
return suite;
}
}
@TestMetadata("compiler/testData/loadKotlin/class")
public static class Class extends AbstractLoadCompiledKotlinTest {
public void testAllFilesPresentInClass() throws Exception {
@@ -1129,6 +1192,7 @@ public class LoadCompiledKotlinTestGenerated extends AbstractLoadCompiledKotlinT
public static Test suite() {
TestSuite suite = new TestSuite("LoadCompiledKotlinTestGenerated");
suite.addTestSuite(LoadCompiledKotlinTestGenerated.class);
suite.addTest(Annotations.innerSuite());
suite.addTestSuite(Class.class);
suite.addTestSuite(ClassFun.class);
suite.addTestSuite(ClassObject.class);
@@ -33,12 +33,75 @@ import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparing
@InnerTestClasses({LazyResolveNamespaceComparingTestGenerated.LoadKotlin.class, LazyResolveNamespaceComparingTestGenerated.CompiledJavaCompareWithKotlin.class, LazyResolveNamespaceComparingTestGenerated.NamespaceComparator.class})
public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyResolveNamespaceComparingTest {
@TestMetadata("compiler/testData/loadKotlin")
@InnerTestClasses({LoadKotlin.Class.class, LoadKotlin.ClassFun.class, LoadKotlin.ClassObject.class, LoadKotlin.Constructor.class, LoadKotlin.DataClass.class, LoadKotlin.Fun.class, LoadKotlin.Prop.class, LoadKotlin.Type.class, LoadKotlin.Visibility.class})
@InnerTestClasses({LoadKotlin.Annotations.class, LoadKotlin.Class.class, LoadKotlin.ClassFun.class, LoadKotlin.ClassObject.class, LoadKotlin.Constructor.class, LoadKotlin.DataClass.class, LoadKotlin.Fun.class, LoadKotlin.Prop.class, LoadKotlin.Type.class, LoadKotlin.Visibility.class})
public static class LoadKotlin extends AbstractLazyResolveNamespaceComparingTest {
public void testAllFilesPresentInLoadKotlin() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/loadKotlin/annotations")
@InnerTestClasses({Annotations.Classes.class})
public static class Annotations extends AbstractLazyResolveNamespaceComparingTest {
public void testAllFilesPresentInAnnotations() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin/annotations"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/loadKotlin/annotations/classes")
public static class Classes extends AbstractLazyResolveNamespaceComparingTest {
public void testAllFilesPresentInClasses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadKotlin/annotations/classes"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("ClassInClassObject.kt")
public void testClassInClassObject() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/ClassInClassObject.kt");
}
@TestMetadata("ClassObject.kt")
public void testClassObject() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/ClassObject.kt");
}
@TestMetadata("Deprecated.kt")
public void testDeprecated() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/Deprecated.kt");
}
@TestMetadata("MultipleAnnotations.kt")
public void testMultipleAnnotations() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/MultipleAnnotations.kt");
}
@TestMetadata("NestedClass.kt")
public void testNestedClass() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/NestedClass.kt");
}
@TestMetadata("Simple.kt")
public void testSimple() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/Simple.kt");
}
@TestMetadata("WithArgument.kt")
public void testWithArgument() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/WithArgument.kt");
}
@TestMetadata("WithMultipleArguments.kt")
public void testWithMultipleArguments() throws Exception {
doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/annotations/classes/WithMultipleArguments.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Annotations");
suite.addTestSuite(Annotations.class);
suite.addTestSuite(Classes.class);
return suite;
}
}
@TestMetadata("compiler/testData/loadKotlin/class")
public static class Class extends AbstractLazyResolveNamespaceComparingTest {
public void testAllFilesPresentInClass() throws Exception {
@@ -1131,6 +1194,7 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso
public static Test innerSuite() {
TestSuite suite = new TestSuite("LoadKotlin");
suite.addTestSuite(LoadKotlin.class);
suite.addTest(Annotations.innerSuite());
suite.addTestSuite(Class.class);
suite.addTestSuite(ClassFun.class);
suite.addTestSuite(ClassObject.class);