Make AnnotationLoader and ConstantLoader accept descriptor-independent data

Rewrite BaseDescriptorLoader to operate on data derived from proto
Intdoduce ProtoContainer to replace containing descriptor as input data
This commit is contained in:
Pavel V. Talanov
2014-11-26 18:36:34 +03:00
parent c73ac97ecf
commit e4d3a65124
8 changed files with 108 additions and 93 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.SerializationPackage;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotatedCallableKind;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader;
import org.jetbrains.jet.descriptors.serialization.descriptors.ProtoContainer;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptorImpl;
@@ -57,15 +58,15 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
@Override
public List<AnnotationDescriptor> loadClassAnnotations(
@NotNull ClassDescriptor descriptor,
@NotNull ProtoBuf.Class classProto,
@NotNull NameResolver nameResolver
) {
KotlinJvmBinaryClass kotlinClass = findKotlinClassByDescriptor(descriptor);
ClassId classId = nameResolver.getClassId(classProto.getFqName());
KotlinJvmBinaryClass kotlinClass = findKotlinClassById(classId);
if (kotlinClass == null) {
// This means that the resource we're constructing the descriptor from is no longer present: KotlinClassFinder had found the
// class earlier, but it can't now
getErrorReporter().reportLoadingError("Kotlin class for loading class annotations is not found: " + descriptor, null);
getErrorReporter().reportLoadingError("Kotlin class for loading class annotations is not found: " + classId.asSingleFqName(), null);
return Collections.emptyList();
}
@@ -183,7 +184,7 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
@Override
public List<AnnotationDescriptor> loadCallableAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
@@ -196,7 +197,7 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
private List<AnnotationDescriptor> findClassAndLoadMemberAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind,
@@ -215,7 +216,7 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
@Override
public List<AnnotationDescriptor> loadValueParameterAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind,
@@ -20,21 +20,14 @@ import org.jetbrains.jet.descriptors.serialization.JavaProtoBuf.*;
import org.jetbrains.jet.descriptors.serialization.NameResolver;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotatedCallableKind;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassOrPackageFragmentDescriptor;
import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
import org.jetbrains.jet.lang.resolve.name.ClassId;
import org.jetbrains.jet.lang.resolve.DescriptorUtils.isClassObject
import org.jetbrains.jet.lang.resolve.DescriptorUtils.isTrait
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils.getPackageClassId
import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature
import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.getClassId
import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinClassIdToJavaClassId
import kotlin.platform.platformStatic
import org.jetbrains.kotlin.util.sure
import org.jetbrains.jet.descriptors.serialization.descriptors.ProtoContainer
import org.jetbrains.jet.descriptors.serialization.Flags
import org.jetbrains.jet.lang.resolve.name.FqName
public abstract class BaseDescriptorLoader protected(
private val kotlinClassFinder: KotlinClassFinder,
@@ -43,88 +36,86 @@ public abstract class BaseDescriptorLoader protected(
) {
protected fun findClassWithAnnotationsAndInitializers(
container: ClassOrPackageFragmentDescriptor,
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
annotatedCallableKind: AnnotatedCallableKind
): KotlinJvmBinaryClass? {
if (container is PackageFragmentDescriptor) {
return findPackagePartClass(container, proto, nameResolver)
val packageFqName = container.packageFqName
if (packageFqName != null) {
return findPackagePartClass(packageFqName, proto, nameResolver)
}
else if (isClassObject(container) && isStaticFieldInOuter(proto)) {
val classProto = container.classProto!!
val classKind = Flags.CLASS_KIND[classProto.getFlags()]
val classId = nameResolver.getClassId(classProto.getFqName())
if (classKind == ProtoBuf.Class.Kind.CLASS_OBJECT && isStaticFieldInOuter(proto)) {
// Backing fields of properties of a class object are generated in the outer class
return findKotlinClassByDescriptor(container.getContainingDeclaration() as ClassOrPackageFragmentDescriptor)
return findKotlinClassById(classId.getOuterClassId())
}
else if (isTrait(container) && kind == AnnotatedCallableKind.PROPERTY) {
else if (classKind == ProtoBuf.Class.Kind.TRAIT && annotatedCallableKind == AnnotatedCallableKind.PROPERTY) {
if (proto.hasExtension(implClassName)) {
val packageFqName = getClassId(container as ClassDescriptor).getPackageFqName()
val parentPackageFqName = classId.getPackageFqName()
val tImplName = nameResolver.getName(proto.getExtension(implClassName))
// TODO: store accurate name for nested traits
return kotlinClassFinder.findKotlinClass(ClassId(packageFqName, tImplName))
return findKotlinClassById(ClassId(parentPackageFqName, tImplName))
}
return null
}
return findKotlinClassByDescriptor(container)
return findKotlinClassById(classId)
}
private fun findPackagePartClass(
container: PackageFragmentDescriptor,
packageFqName: FqName,
proto: ProtoBuf.Callable,
nameResolver: NameResolver
): KotlinJvmBinaryClass? {
if (proto.hasExtension(implClassName)) {
return kotlinClassFinder.findKotlinClass(ClassId(container.fqName, nameResolver.getName(proto.getExtension(implClassName))))
return findKotlinClassById(ClassId(packageFqName, nameResolver.getName(proto.getExtension(implClassName))))
}
return null
}
protected fun findKotlinClassByDescriptor(descriptor: ClassOrPackageFragmentDescriptor): KotlinJvmBinaryClass? {
return when (descriptor) {
is ClassDescriptor -> kotlinClassFinder.findKotlinClass(kotlinClassIdToJavaClassId(getClassId(descriptor)))
is PackageFragmentDescriptor -> kotlinClassFinder.findKotlinClass(getPackageClassId((descriptor).fqName))
else -> throw IllegalStateException("Unrecognized descriptor: " + descriptor)
}
protected fun findKotlinClassById(classId: ClassId): KotlinJvmBinaryClass? {
return kotlinClassFinder.findKotlinClass(kotlinClassIdToJavaClassId(classId))
}
class object {
platformStatic fun getCallableSignature(
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): MemberSignature? {
val deserializer = SignatureDeserializer(nameResolver)
when (kind) {
AnnotatedCallableKind.FUNCTION -> if (proto.hasExtension(methodSignature)) {
return deserializer.methodSignature(proto.getExtension(methodSignature))
}
AnnotatedCallableKind.PROPERTY_GETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getGetter())
}
AnnotatedCallableKind.PROPERTY_SETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getSetter())
}
AnnotatedCallableKind.PROPERTY -> if (proto.hasExtension(propertySignature)) {
val propertySignature = proto.getExtension(propertySignature)
private fun isStaticFieldInOuter(proto: ProtoBuf.Callable): Boolean {
if (!proto.hasExtension(propertySignature)) return false
val propertySignature = proto.getExtension(propertySignature)
return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter()
}
if (propertySignature.hasField()) {
val field = propertySignature.getField()
val type = deserializer.typeDescriptor(field.getType())
val name = nameResolver.getName(field.getName())
return MemberSignature.fromFieldNameAndDesc(name, type)
}
else if (propertySignature.hasSyntheticMethod()) {
return deserializer.methodSignature(propertySignature.getSyntheticMethod())
}
fun getCallableSignature(
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): MemberSignature? {
val deserializer = SignatureDeserializer(nameResolver)
when (kind) {
AnnotatedCallableKind.FUNCTION -> if (proto.hasExtension(methodSignature)) {
return deserializer.methodSignature(proto.getExtension(methodSignature))
}
AnnotatedCallableKind.PROPERTY_GETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getGetter())
}
AnnotatedCallableKind.PROPERTY_SETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getSetter())
}
AnnotatedCallableKind.PROPERTY -> if (proto.hasExtension(propertySignature)) {
val propertySignature = proto.getExtension(propertySignature)
if (propertySignature.hasField()) {
val field = propertySignature.getField()
val type = deserializer.typeDescriptor(field.getType())
val name = nameResolver.getName(field.getName())
return MemberSignature.fromFieldNameAndDesc(name, type)
}
else if (propertySignature.hasSyntheticMethod()) {
return deserializer.methodSignature(propertySignature.getSyntheticMethod())
}
}
return null
}
private fun isStaticFieldInOuter(proto: ProtoBuf.Callable): Boolean {
if (!proto.hasExtension(propertySignature)) return false
val propertySignature = proto.getExtension(propertySignature)
return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter()
}
return null
}
}
@@ -22,7 +22,7 @@ import org.jetbrains.jet.descriptors.serialization.NameResolver;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotatedCallableKind;
import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader;
import org.jetbrains.jet.lang.descriptors.ClassOrPackageFragmentDescriptor;
import org.jetbrains.jet.descriptors.serialization.descriptors.ProtoContainer;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
@@ -40,12 +40,12 @@ public class ConstantDescriptorLoader extends BaseDescriptorLoader implements Co
@Nullable
@Override
public CompileTimeConstant<?> loadPropertyConstant(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
) {
MemberSignature signature = BaseDescriptorLoader.getCallableSignature(proto, nameResolver, kind);
MemberSignature signature = getCallableSignature(proto, nameResolver, kind);
if (signature == null) return null;
KotlinJvmBinaryClass kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind);
@@ -113,7 +113,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
if (Flags.HAS_CONSTANT.get(flags)) {
property.setCompileTimeInitializer(
c.storageManager.createNullableLazyValue {
val container = c.containingDeclaration.asClassOrPackage()
val container = c.containingDeclaration.asProtoContainer()
c.components.constantLoader.loadPropertyConstant(container, proto, c.nameResolver, AnnotatedCallableKind.PROPERTY)
}
)
@@ -166,13 +166,13 @@ public class MemberDeserializer(private val c: DeserializationContext) {
}
return DeserializedAnnotations(c.storageManager) {
c.components.annotationLoader.loadCallableAnnotations(
c.containingDeclaration.asClassOrPackage(), proto, c.nameResolver, kind
c.containingDeclaration.asProtoContainer(), proto, c.nameResolver, kind
)
}
}
private fun valueParameters(callable: Callable, kind: AnnotatedCallableKind): List<ValueParameterDescriptor> {
val containerOfCallable = c.containingDeclaration.getContainingDeclaration().asClassOrPackage()
val containerOfCallable = c.containingDeclaration.getContainingDeclaration().asProtoContainer()
return callable.getValueParameterList().withIndices().map { val (i, proto) = it
ValueParameterDescriptorImpl(
@@ -188,7 +188,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
}
private fun getParameterAnnotations(
classOrPackage: ClassOrPackageFragmentDescriptor,
container: ProtoContainer,
callable: Callable,
kind: AnnotatedCallableKind,
valueParameter: Callable.ValueParameter
@@ -197,11 +197,13 @@ public class MemberDeserializer(private val c: DeserializationContext) {
return Annotations.EMPTY
}
return DeserializedAnnotations(c.storageManager) {
c.components.annotationLoader.loadValueParameterAnnotations(classOrPackage, callable, c.nameResolver, kind, valueParameter)
c.components.annotationLoader.loadValueParameterAnnotations(container, callable, c.nameResolver, kind, valueParameter)
}
}
private fun DeclarationDescriptor.asClassOrPackage(): ClassOrPackageFragmentDescriptor =
this as? ClassOrPackageFragmentDescriptor
?: error("Only members in classes or package fragments should be serialized: $this")
private fun DeclarationDescriptor.asProtoContainer(): ProtoContainer = when(this) {
is PackageFragmentDescriptor -> ProtoContainer(null, fqName)
is DeserializedClassDescriptor -> ProtoContainer(classProto, null)
else -> error("Only members in classes or package fragments should be serialized: $this")
}
}
@@ -19,8 +19,6 @@ package org.jetbrains.jet.descriptors.serialization.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.descriptors.serialization.NameResolver;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassOrPackageFragmentDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import java.util.List;
@@ -30,7 +28,6 @@ public interface AnnotationLoader {
@NotNull
@Override
public List<AnnotationDescriptor> loadClassAnnotations(
@NotNull ClassDescriptor descriptor,
@NotNull ProtoBuf.Class classProto,
@NotNull NameResolver nameResolver
) {
@@ -40,7 +37,7 @@ public interface AnnotationLoader {
@NotNull
@Override
public List<AnnotationDescriptor> loadCallableAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
@@ -51,7 +48,7 @@ public interface AnnotationLoader {
@NotNull
@Override
public List<AnnotationDescriptor> loadValueParameterAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind,
@@ -68,14 +65,13 @@ public interface AnnotationLoader {
@NotNull
List<AnnotationDescriptor> loadClassAnnotations(
@NotNull ClassDescriptor descriptor,
@NotNull ProtoBuf.Class classProto,
@NotNull NameResolver nameResolver
);
@NotNull
List<AnnotationDescriptor> loadCallableAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
@@ -83,7 +79,7 @@ public interface AnnotationLoader {
@NotNull
List<AnnotationDescriptor> loadValueParameterAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind,
@@ -20,7 +20,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.descriptors.serialization.NameResolver;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.lang.descriptors.ClassOrPackageFragmentDescriptor;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
public interface ConstantLoader {
@@ -28,7 +27,7 @@ public interface ConstantLoader {
@Nullable
@Override
public CompileTimeConstant<?> loadPropertyConstant(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
@@ -39,7 +38,7 @@ public interface ConstantLoader {
@Nullable
CompileTimeConstant<?> loadPropertyConstant(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
@@ -40,7 +40,7 @@ import org.jetbrains.jet.descriptors.serialization.NameResolver
public class DeserializedClassDescriptor(
outerContext: DeserializationContext,
private val classProto: ProtoBuf.Class,
val classProto: ProtoBuf.Class,
nameResolver: NameResolver
) : ClassDescriptor, AbstractClassDescriptor(
outerContext.storageManager,
@@ -70,7 +70,7 @@ public class DeserializedClassDescriptor(
Annotations.EMPTY
}
else DeserializedAnnotations(c.storageManager) {
c.components.annotationLoader.loadClassAnnotations(this, classProto, c.nameResolver)
c.components.annotationLoader.loadClassAnnotations(classProto, c.nameResolver)
}
override fun getContainingDeclaration(): DeclarationDescriptor = containingDeclaration
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2014 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.
*/
package org.jetbrains.jet.descriptors.serialization.descriptors
import org.jetbrains.jet.lang.resolve.name.FqName
import org.jetbrains.jet.descriptors.serialization.ProtoBuf
public data class ProtoContainer(val classProto: ProtoBuf.Class?, val packageFqName: FqName?) {
{
assert(classProto != null || packageFqName != null)
}
}