diff --git a/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForJavaDescriptorResolver.java b/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForJavaDescriptorResolver.java index 2008d862816..328c2e77b2b 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForJavaDescriptorResolver.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForJavaDescriptorResolver.java @@ -39,9 +39,7 @@ import org.jetbrains.jet.lang.resolve.java.lazy.GlobalJavaResolverContext; import org.jetbrains.jet.lang.resolve.kotlin.DeserializedDescriptorResolver; import org.jetbrains.jet.lang.resolve.kotlin.DeserializationComponentsForJava; import org.jetbrains.jet.lang.resolve.kotlin.JavaClassDataFinder; -import org.jetbrains.jet.lang.resolve.kotlin.AnnotationDescriptorLoader; -import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage; -import org.jetbrains.jet.lang.resolve.kotlin.ConstantDescriptorLoader; +import org.jetbrains.jet.lang.resolve.kotlin.BinaryClassAnnotationAndConstantLoader; import org.jetbrains.annotations.NotNull; import javax.annotation.PreDestroy; @@ -72,9 +70,7 @@ public class InjectorForJavaDescriptorResolver { private final DeserializedDescriptorResolver deserializedDescriptorResolver; private final DeserializationComponentsForJava deserializationComponentsForJava; private final JavaClassDataFinder javaClassDataFinder; - private final AnnotationDescriptorLoader annotationDescriptorLoader; - private final DescriptorLoadersStorage descriptorLoadersStorage; - private final ConstantDescriptorLoader constantDescriptorLoader; + private final BinaryClassAnnotationAndConstantLoader binaryClassAnnotationAndConstantLoader; public InjectorForJavaDescriptorResolver( @NotNull Project project, @@ -102,10 +98,8 @@ public class InjectorForJavaDescriptorResolver { this.javaDescriptorResolver = new JavaDescriptorResolver(lazyJavaPackageFragmentProvider, getModule()); this.globalSearchScope = com.intellij.psi.search.GlobalSearchScope.allScope(project); this.javaClassDataFinder = new JavaClassDataFinder(virtualFileFinder, deserializedDescriptorResolver); - this.descriptorLoadersStorage = new DescriptorLoadersStorage(lockBasedStorageManager, getModule()); - this.annotationDescriptorLoader = new AnnotationDescriptorLoader(getModule(), descriptorLoadersStorage, virtualFileFinder, traceBasedErrorReporter); - this.constantDescriptorLoader = new ConstantDescriptorLoader(descriptorLoadersStorage, virtualFileFinder, traceBasedErrorReporter); - this.deserializationComponentsForJava = new DeserializationComponentsForJava(lockBasedStorageManager, getModule(), javaClassDataFinder, annotationDescriptorLoader, constantDescriptorLoader, lazyJavaPackageFragmentProvider); + this.binaryClassAnnotationAndConstantLoader = new BinaryClassAnnotationAndConstantLoader(getModule(), lockBasedStorageManager, virtualFileFinder, traceBasedErrorReporter); + this.deserializationComponentsForJava = new DeserializationComponentsForJava(lockBasedStorageManager, getModule(), javaClassDataFinder, binaryClassAnnotationAndConstantLoader, lazyJavaPackageFragmentProvider); this.javaClassFinder.setProject(project); this.javaClassFinder.setScope(globalSearchScope); diff --git a/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForLazyResolveWithJava.java b/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForLazyResolveWithJava.java index 628cc321f84..986ee547eaf 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForLazyResolveWithJava.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForLazyResolveWithJava.java @@ -67,9 +67,7 @@ import org.jetbrains.jet.lang.resolve.java.lazy.GlobalJavaResolverContext; import org.jetbrains.jet.lang.resolve.kotlin.DeserializedDescriptorResolver; import org.jetbrains.jet.lang.resolve.kotlin.DeserializationComponentsForJava; import org.jetbrains.jet.lang.resolve.kotlin.JavaClassDataFinder; -import org.jetbrains.jet.lang.resolve.kotlin.AnnotationDescriptorLoader; -import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage; -import org.jetbrains.jet.lang.resolve.kotlin.ConstantDescriptorLoader; +import org.jetbrains.jet.lang.resolve.kotlin.BinaryClassAnnotationAndConstantLoader; import org.jetbrains.annotations.NotNull; import javax.annotation.PreDestroy; @@ -128,9 +126,7 @@ public class InjectorForLazyResolveWithJava { private final DeserializedDescriptorResolver deserializedDescriptorResolver; private final DeserializationComponentsForJava deserializationComponentsForJava; private final JavaClassDataFinder javaClassDataFinder; - private final AnnotationDescriptorLoader annotationDescriptorLoader; - private final DescriptorLoadersStorage descriptorLoadersStorage; - private final ConstantDescriptorLoader constantDescriptorLoader; + private final BinaryClassAnnotationAndConstantLoader binaryClassAnnotationAndConstantLoader; public InjectorForLazyResolveWithJava( @NotNull Project project, @@ -191,10 +187,8 @@ public class InjectorForLazyResolveWithJava { this.scopeProvider = new ScopeProvider(getResolveSession()); this.scriptBodyResolver = new ScriptBodyResolver(); this.javaClassDataFinder = new JavaClassDataFinder(virtualFileFinder, deserializedDescriptorResolver); - this.descriptorLoadersStorage = new DescriptorLoadersStorage(storageManager, module); - this.annotationDescriptorLoader = new AnnotationDescriptorLoader(module, descriptorLoadersStorage, virtualFileFinder, traceBasedErrorReporter); - this.constantDescriptorLoader = new ConstantDescriptorLoader(descriptorLoadersStorage, virtualFileFinder, traceBasedErrorReporter); - this.deserializationComponentsForJava = new DeserializationComponentsForJava(storageManager, module, javaClassDataFinder, annotationDescriptorLoader, constantDescriptorLoader, lazyJavaPackageFragmentProvider); + this.binaryClassAnnotationAndConstantLoader = new BinaryClassAnnotationAndConstantLoader(module, storageManager, virtualFileFinder, traceBasedErrorReporter); + this.deserializationComponentsForJava = new DeserializationComponentsForJava(storageManager, module, javaClassDataFinder, binaryClassAnnotationAndConstantLoader, lazyJavaPackageFragmentProvider); this.resolveSession.setAnnotationResolve(annotationResolver); this.resolveSession.setDescriptorResolver(descriptorResolver); diff --git a/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForTopDownAnalyzerForJvm.java b/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForTopDownAnalyzerForJvm.java index b807cff2e41..3fc6600595d 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForTopDownAnalyzerForJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/di/InjectorForTopDownAnalyzerForJvm.java @@ -78,9 +78,7 @@ import org.jetbrains.jet.lang.resolve.java.lazy.LazyJavaPackageFragmentProvider; import org.jetbrains.jet.lang.resolve.java.lazy.GlobalJavaResolverContext; import org.jetbrains.jet.lang.resolve.kotlin.DeserializedDescriptorResolver; import org.jetbrains.jet.lang.resolve.kotlin.JavaClassDataFinder; -import org.jetbrains.jet.lang.resolve.kotlin.AnnotationDescriptorLoader; -import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage; -import org.jetbrains.jet.lang.resolve.kotlin.ConstantDescriptorLoader; +import org.jetbrains.jet.lang.resolve.kotlin.BinaryClassAnnotationAndConstantLoader; import org.jetbrains.annotations.NotNull; import javax.annotation.PreDestroy; @@ -150,9 +148,7 @@ public class InjectorForTopDownAnalyzerForJvm { private final GlobalJavaResolverContext globalJavaResolverContext; private final DeserializedDescriptorResolver deserializedDescriptorResolver; private final JavaClassDataFinder javaClassDataFinder; - private final AnnotationDescriptorLoader annotationDescriptorLoader; - private final DescriptorLoadersStorage descriptorLoadersStorage; - private final ConstantDescriptorLoader constantDescriptorLoader; + private final BinaryClassAnnotationAndConstantLoader binaryClassAnnotationAndConstantLoader; public InjectorForTopDownAnalyzerForJvm( @NotNull Project project, @@ -185,10 +181,8 @@ public class InjectorForTopDownAnalyzerForJvm { this.lazyJavaPackageFragmentProvider = new LazyJavaPackageFragmentProvider(globalJavaResolverContext, getModuleDescriptor()); this.javaDescriptorResolver = new JavaDescriptorResolver(lazyJavaPackageFragmentProvider, getModuleDescriptor()); this.javaClassDataFinder = new JavaClassDataFinder(virtualFileFinder, deserializedDescriptorResolver); - this.descriptorLoadersStorage = new DescriptorLoadersStorage(storageManager, getModuleDescriptor()); - this.annotationDescriptorLoader = new AnnotationDescriptorLoader(getModuleDescriptor(), descriptorLoadersStorage, virtualFileFinder, traceBasedErrorReporter); - this.constantDescriptorLoader = new ConstantDescriptorLoader(descriptorLoadersStorage, virtualFileFinder, traceBasedErrorReporter); - this.deserializationComponentsForJava = new DeserializationComponentsForJava(storageManager, getModuleDescriptor(), javaClassDataFinder, annotationDescriptorLoader, constantDescriptorLoader, lazyJavaPackageFragmentProvider); + this.binaryClassAnnotationAndConstantLoader = new BinaryClassAnnotationAndConstantLoader(getModuleDescriptor(), storageManager, virtualFileFinder, traceBasedErrorReporter); + this.deserializationComponentsForJava = new DeserializationComponentsForJava(storageManager, getModuleDescriptor(), javaClassDataFinder, binaryClassAnnotationAndConstantLoader, lazyJavaPackageFragmentProvider); this.additionalCheckerProvider = org.jetbrains.jet.lang.resolve.kotlin.JavaDeclarationCheckerProvider.INSTANCE$; this.globalSearchScope = com.intellij.psi.search.GlobalSearchScope.allScope(project); this.javaFlexibleTypeCapabilitiesProvider = new JavaFlexibleTypeCapabilitiesProvider(); diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/AnnotationDescriptorLoader.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/AnnotationDescriptorLoader.kt deleted file mode 100644 index fc5ca27d493..00000000000 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/AnnotationDescriptorLoader.kt +++ /dev/null @@ -1,199 +0,0 @@ -/* - * 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.lang.resolve.kotlin - -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.* -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 -import org.jetbrains.jet.lang.resolve.constants.* -import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames -import org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils -import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter -import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass.AnnotationArrayArgumentVisitor -import org.jetbrains.jet.lang.resolve.name.ClassId -import org.jetbrains.jet.lang.resolve.name.Name -import org.jetbrains.jet.lang.types.ErrorUtils - -import java.util.* - -import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature -import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.javaClassIdToKotlinClassId - -public class AnnotationDescriptorLoader( - private val module: ModuleDescriptor, - storage: DescriptorLoadersStorage, - kotlinClassFinder: KotlinClassFinder, - errorReporter: ErrorReporter -) : BaseDescriptorLoader(kotlinClassFinder, errorReporter, storage), AnnotationLoader { - - override fun loadClassAnnotations(classProto: ProtoBuf.Class, nameResolver: NameResolver): List { - val classId = nameResolver.getClassId(classProto.getFqName()) - val 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 - errorReporter.reportLoadingError("Kotlin class for loading class annotations is not found: ${classId.asSingleFqName()}", null) - return listOf() - } - - val result = ArrayList(1) - - kotlinClass.loadClassAnnotations(object : KotlinJvmBinaryClass.AnnotationVisitor { - override fun visitAnnotation(classId: ClassId): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { - return resolveAnnotation(classId, result, module) - } - - override fun visitEnd() { - } - }) - - return result - } - - override fun loadCallableAnnotations( - container: ProtoContainer, - proto: ProtoBuf.Callable, - nameResolver: NameResolver, - kind: AnnotatedCallableKind - ): List { - val signature = getCallableSignature(proto, nameResolver, kind) ?: return listOf() - return findClassAndLoadMemberAnnotations(container, proto, nameResolver, kind, signature) - } - - private fun findClassAndLoadMemberAnnotations( - container: ProtoContainer, - proto: ProtoBuf.Callable, - nameResolver: NameResolver, - kind: AnnotatedCallableKind, - signature: MemberSignature - ): List { - val kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind) - if (kotlinClass == null) { - errorReporter.reportLoadingError("Kotlin class for loading member annotations is not found: $container", null) - return listOf() - } - - return storage.getStorageForClass(kotlinClass).memberAnnotations[signature] ?: listOf() - } - - override fun loadValueParameterAnnotations( - container: ProtoContainer, - callable: ProtoBuf.Callable, - nameResolver: NameResolver, - kind: AnnotatedCallableKind, - proto: ProtoBuf.Callable.ValueParameter - ): List { - val methodSignature = getCallableSignature(callable, nameResolver, kind) - if (methodSignature != null) { - if (proto.hasExtension(JavaProtoBuf.index)) { - val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, proto.getExtension(JavaProtoBuf.index)) - return findClassAndLoadMemberAnnotations(container, callable, nameResolver, kind, paramSignature) - } - } - - return listOf() - } - - class object { - - public fun resolveAnnotation( - classId: ClassId, - result: MutableList, - moduleDescriptor: ModuleDescriptor - ): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { - if (JvmAnnotationNames.isSpecialAnnotation(classId, true)) return null - - val annotationClass = resolveClass(classId, moduleDescriptor) - - return object : KotlinJvmBinaryClass.AnnotationArgumentVisitor { - private val arguments = HashMap>() - - override fun visit(name: Name?, value: Any?) { - if (name != null) { - setArgumentValueByName(name, createConstant(name, value)) - } - } - - override fun visitEnum(name: Name, enumClassId: ClassId, enumEntryName: Name) { - setArgumentValueByName(name, enumEntryValue(enumClassId, enumEntryName)) - } - - override fun visitArray(name: Name): AnnotationArrayArgumentVisitor? { - return object : KotlinJvmBinaryClass.AnnotationArrayArgumentVisitor { - private val elements = ArrayList>() - - override fun visit(value: Any?) { - elements.add(createConstant(name, value)) - } - - override fun visitEnum(enumClassId: ClassId, enumEntryName: Name) { - elements.add(enumEntryValue(enumClassId, enumEntryName)) - } - - override fun visitEnd() { - val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass) - if (parameter != null) { - elements.trimToSize() - arguments[parameter] = ArrayValue(elements, parameter.getType(), true, false) - } - } - } - } - - private fun enumEntryValue(enumClassId: ClassId, name: Name): CompileTimeConstant<*> { - val enumClass = resolveClass(enumClassId, moduleDescriptor) - if (enumClass.getKind() == ClassKind.ENUM_CLASS) { - val classifier = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(name) - if (classifier is ClassDescriptor) { - return EnumValue(classifier, false) - } - } - return ErrorValue.create("Unresolved enum entry: $enumClassId.$name") - } - - override fun visitEnd() { - result.add(AnnotationDescriptorImpl(annotationClass.getDefaultType(), arguments)) - } - - private fun createConstant(name: Name?, value: Any?): CompileTimeConstant<*> { - return createCompileTimeConstant(value, true, false, false, null) - ?: ErrorValue.create("Unsupported annotation argument: $name") - } - - private fun setArgumentValueByName(name: Name, argumentValue: CompileTimeConstant<*>) { - val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass) - if (parameter != null) { - arguments[parameter] = argumentValue - } - } - } - } - - private fun resolveClass(javaClassId: ClassId, moduleDescriptor: ModuleDescriptor): ClassDescriptor { - val classId = javaClassIdToKotlinClassId(javaClassId) - return moduleDescriptor.findClassAcrossModuleDependencies(classId) - ?: ErrorUtils.createErrorClass(classId.asSingleFqName().asString()) - } - } -} diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/BaseDescriptorLoader.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/BaseDescriptorLoader.kt deleted file mode 100644 index 2df45c1e448..00000000000 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/BaseDescriptorLoader.kt +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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.lang.resolve.kotlin - -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.resolve.java.resolver.ErrorReporter; -import org.jetbrains.jet.lang.resolve.name.ClassId; - -import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature -import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinClassIdToJavaClassId -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, - protected val errorReporter: ErrorReporter, - protected val storage: DescriptorLoadersStorage -) { - - protected fun findClassWithAnnotationsAndInitializers( - container: ProtoContainer, - proto: ProtoBuf.Callable, - nameResolver: NameResolver, - annotatedCallableKind: AnnotatedCallableKind - ): KotlinJvmBinaryClass? { - val packageFqName = container.packageFqName - if (packageFqName != null) { - return findPackagePartClass(packageFqName, proto, nameResolver) - } - 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 findKotlinClassById(classId.getOuterClassId()) - } - else if (classKind == ProtoBuf.Class.Kind.TRAIT && annotatedCallableKind == AnnotatedCallableKind.PROPERTY) { - if (proto.hasExtension(implClassName)) { - val parentPackageFqName = classId.getPackageFqName() - val tImplName = nameResolver.getName(proto.getExtension(implClassName)) - // TODO: store accurate name for nested traits - return findKotlinClassById(ClassId(parentPackageFqName, tImplName)) - } - return null - } - - return findKotlinClassById(classId) - } - - private fun findPackagePartClass( - packageFqName: FqName, - proto: ProtoBuf.Callable, - nameResolver: NameResolver - ): KotlinJvmBinaryClass? { - if (proto.hasExtension(implClassName)) { - return findKotlinClassById(ClassId(packageFqName, nameResolver.getName(proto.getExtension(implClassName)))) - } - return null - } - - protected fun findKotlinClassById(classId: ClassId): KotlinJvmBinaryClass? { - return kotlinClassFinder.findKotlinClass(kotlinClassIdToJavaClassId(classId)) - } - - private fun isStaticFieldInOuter(proto: ProtoBuf.Callable): Boolean { - if (!proto.hasExtension(propertySignature)) return false - val propertySignature = proto.getExtension(propertySignature) - return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter() - } - - 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 - } -} diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/BinaryClassAnnotationAndConstantLoader.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/BinaryClassAnnotationAndConstantLoader.kt new file mode 100644 index 00000000000..2d1148c96b4 --- /dev/null +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/BinaryClassAnnotationAndConstantLoader.kt @@ -0,0 +1,385 @@ +/* + * 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.lang.resolve.kotlin + +import org.jetbrains.jet.descriptors.serialization.* +import org.jetbrains.jet.descriptors.serialization.descriptors.* +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.descriptors.annotations.* +import org.jetbrains.jet.lang.resolve.constants.* +import org.jetbrains.jet.descriptors.serialization.JavaProtoBuf.* +import org.jetbrains.jet.lang.resolve.name.* +import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames +import org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils +import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter +import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass.AnnotationArrayArgumentVisitor +import org.jetbrains.jet.lang.types.ErrorUtils +import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.javaClassIdToKotlinClassId +import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinClassIdToJavaClassId +import org.jetbrains.jet.storage.StorageManager +import kotlin.platform.platformStatic + +import java.util.* + +public class BinaryClassAnnotationAndConstantLoader( + private val module: ModuleDescriptor, + storageManager: StorageManager, + private val kotlinClassFinder: KotlinClassFinder, + private val errorReporter: ErrorReporter +) : AnnotationAndConstantLoader { + + private val storage = storageManager.createMemoizedFunction{ + kotlinClass -> + loadAnnotationsAndInitializers(kotlinClass) + } + + override fun loadClassAnnotations(classProto: ProtoBuf.Class, nameResolver: NameResolver): List { + val classId = nameResolver.getClassId(classProto.getFqName()) + val 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 + errorReporter.reportLoadingError("Kotlin class for loading class annotations is not found: ${classId.asSingleFqName()}", null) + return listOf() + } + + val result = ArrayList(1) + + kotlinClass.loadClassAnnotations(object : KotlinJvmBinaryClass.AnnotationVisitor { + override fun visitAnnotation(classId: ClassId): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { + return resolveAnnotation(classId, result, module) + } + + override fun visitEnd() { + } + }) + + return result + } + + override fun loadCallableAnnotations( + container: ProtoContainer, + proto: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind + ): List { + val signature = getCallableSignature(proto, nameResolver, kind) ?: return listOf() + return findClassAndLoadMemberAnnotations(container, proto, nameResolver, kind, signature) + } + + private fun findClassAndLoadMemberAnnotations( + container: ProtoContainer, + proto: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind, + signature: MemberSignature + ): List { + val kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind) + if (kotlinClass == null) { + errorReporter.reportLoadingError("Kotlin class for loading member annotations is not found: $container", null) + return listOf() + } + + return storage(kotlinClass).memberAnnotations[signature] ?: listOf() + } + + override fun loadValueParameterAnnotations( + container: ProtoContainer, + callable: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind, + proto: ProtoBuf.Callable.ValueParameter + ): List { + val methodSignature = getCallableSignature(callable, nameResolver, kind) + if (methodSignature != null) { + if (proto.hasExtension(index)) { + val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, proto.getExtension(JavaProtoBuf.index)) + return findClassAndLoadMemberAnnotations(container, callable, nameResolver, kind, paramSignature) + } + } + + return listOf() + } + + class object { + + public fun resolveAnnotation( + classId: ClassId, + result: MutableList, + moduleDescriptor: ModuleDescriptor + ): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { + if (JvmAnnotationNames.isSpecialAnnotation(classId, true)) return null + + val annotationClass = resolveClass(classId, moduleDescriptor) + + return object : KotlinJvmBinaryClass.AnnotationArgumentVisitor { + private val arguments = HashMap>() + + override fun visit(name: Name?, value: Any?) { + if (name != null) { + setArgumentValueByName(name, createConstant(name, value)) + } + } + + override fun visitEnum(name: Name, enumClassId: ClassId, enumEntryName: Name) { + setArgumentValueByName(name, enumEntryValue(enumClassId, enumEntryName)) + } + + override fun visitArray(name: Name): AnnotationArrayArgumentVisitor? { + return object : KotlinJvmBinaryClass.AnnotationArrayArgumentVisitor { + private val elements = ArrayList>() + + override fun visit(value: Any?) { + elements.add(createConstant(name, value)) + } + + override fun visitEnum(enumClassId: ClassId, enumEntryName: Name) { + elements.add(enumEntryValue(enumClassId, enumEntryName)) + } + + override fun visitEnd() { + val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass) + if (parameter != null) { + elements.trimToSize() + arguments[parameter] = ArrayValue(elements, parameter.getType(), true, false) + } + } + } + } + + private fun enumEntryValue(enumClassId: ClassId, name: Name): CompileTimeConstant<*> { + val enumClass = resolveClass(enumClassId, moduleDescriptor) + if (enumClass.getKind() == ClassKind.ENUM_CLASS) { + val classifier = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(name) + if (classifier is ClassDescriptor) { + return EnumValue(classifier, false) + } + } + return ErrorValue.create("Unresolved enum entry: $enumClassId.$name") + } + + override fun visitEnd() { + result.add(AnnotationDescriptorImpl(annotationClass.getDefaultType(), arguments)) + } + + private fun createConstant(name: Name?, value: Any?): CompileTimeConstant<*> { + return createCompileTimeConstant(value, true, false, false, null) + ?: ErrorValue.create("Unsupported annotation argument: $name") + } + + private fun setArgumentValueByName(name: Name, argumentValue: CompileTimeConstant<*>) { + val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass) + if (parameter != null) { + arguments[parameter] = argumentValue + } + } + } + } + + private fun resolveClass(javaClassId: ClassId, moduleDescriptor: ModuleDescriptor): ClassDescriptor { + val classId = javaClassIdToKotlinClassId(javaClassId) + return moduleDescriptor.findClassAcrossModuleDependencies(classId) + ?: ErrorUtils.createErrorClass(classId.asSingleFqName().asString()) + } + } + + override fun loadPropertyConstant( + container: ProtoContainer, + proto: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind + ): CompileTimeConstant<*>? { + val signature = getCallableSignature(proto, nameResolver, kind) ?: return null + + val kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind) + if (kotlinClass == null) { + errorReporter.reportLoadingError("Kotlin class for loading property constant is not found: $container", null) + return null + } + + return storage(kotlinClass).propertyConstants[signature] + } + + + protected fun findClassWithAnnotationsAndInitializers( + container: ProtoContainer, + proto: ProtoBuf.Callable, + nameResolver: NameResolver, + annotatedCallableKind: AnnotatedCallableKind + ): KotlinJvmBinaryClass? { + val packageFqName = container.packageFqName + if (packageFqName != null) { + return findPackagePartClass(packageFqName, proto, nameResolver) + } + 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 findKotlinClassById(classId.getOuterClassId()) + } + else if (classKind == ProtoBuf.Class.Kind.TRAIT && annotatedCallableKind == AnnotatedCallableKind.PROPERTY) { + if (proto.hasExtension(implClassName)) { + val parentPackageFqName = classId.getPackageFqName() + val tImplName = nameResolver.getName(proto.getExtension(implClassName)) + // TODO: store accurate name for nested traits + return findKotlinClassById(ClassId(parentPackageFqName, tImplName)) + } + return null + } + + return findKotlinClassById(classId) + } + + private fun findPackagePartClass( + packageFqName: FqName, + proto: ProtoBuf.Callable, + nameResolver: NameResolver + ): KotlinJvmBinaryClass? { + if (proto.hasExtension(implClassName)) { + return findKotlinClassById(ClassId(packageFqName, nameResolver.getName(proto.getExtension(implClassName)))) + } + return null + } + + protected fun findKotlinClassById(classId: ClassId): KotlinJvmBinaryClass? { + return kotlinClassFinder.findKotlinClass(kotlinClassIdToJavaClassId(classId)) + } + + private fun isStaticFieldInOuter(proto: ProtoBuf.Callable): Boolean { + if (!proto.hasExtension(propertySignature)) return false + val propertySignature = proto.getExtension(propertySignature) + return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter() + } + + 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 loadAnnotationsAndInitializers(kotlinClass: KotlinJvmBinaryClass): Storage { + val memberAnnotations = HashMap>() + val propertyConstants = HashMap>() + + kotlinClass.visitMembers(object : KotlinJvmBinaryClass.MemberVisitor { + override fun visitMethod(name: Name, desc: String): KotlinJvmBinaryClass.MethodAnnotationVisitor? { + return AnnotationVisitorForMethod(MemberSignature.fromMethodNameAndDesc(name.asString() + desc)) + } + + override fun visitField(name: Name, desc: String, initializer: Any?): KotlinJvmBinaryClass.AnnotationVisitor? { + val signature = MemberSignature.fromFieldNameAndDesc(name, desc) + + if (initializer != null) { + val normalizedValue: Any + if (desc in "ZBCS") { + val intValue = initializer as Int + if ("Z" == desc) { + normalizedValue = intValue != 0 + } + else if ("B" == desc) { + normalizedValue = (intValue.toByte()) + } + else if ("C" == desc) { + normalizedValue = (intValue.toChar()) + } + else if ("S" == desc) { + normalizedValue = (intValue.toShort()) + } + else { + throw AssertionError(desc) + } + } + else { + normalizedValue = initializer + } + + propertyConstants[signature] = createCompileTimeConstant( + normalizedValue, canBeUsedInAnnotation = true, isPureIntConstant = true, + usesVariableAsConstant = true, expectedType = null + ) + } + return MemberAnnotationVisitor(signature) + } + + inner class AnnotationVisitorForMethod(signature: MemberSignature) : MemberAnnotationVisitor(signature), KotlinJvmBinaryClass.MethodAnnotationVisitor { + + override fun visitParameterAnnotation(index: Int, classId: ClassId): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { + val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(signature, index) + var result = memberAnnotations[paramSignature] + if (result == null) { + result = ArrayList() + memberAnnotations[paramSignature] = result + } + return BinaryClassAnnotationAndConstantLoader.resolveAnnotation(classId, result, module) + } + } + + open inner class MemberAnnotationVisitor(protected val signature: MemberSignature) : KotlinJvmBinaryClass.AnnotationVisitor { + private val result = ArrayList() + + override fun visitAnnotation(classId: ClassId): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { + return BinaryClassAnnotationAndConstantLoader.resolveAnnotation(classId, result, module) + } + + override fun visitEnd() { + if (result.isNotEmpty()) { + memberAnnotations[signature] = result + } + } + } + }) + + return Storage(memberAnnotations, propertyConstants) + } + + class Storage( + public val memberAnnotations: Map>, + public val propertyConstants: Map> + ) { + class object { + public val EMPTY: Storage = Storage(mapOf(), mapOf()) + } + } +} diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/ConstantDescriptorLoader.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/ConstantDescriptorLoader.kt deleted file mode 100644 index 675943e5015..00000000000 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/ConstantDescriptorLoader.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.lang.resolve.kotlin - -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.descriptors.serialization.descriptors.ProtoContainer -import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant -import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter - -import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature - -public class ConstantDescriptorLoader( - storage: DescriptorLoadersStorage, - kotlinClassFinder: KotlinClassFinder, - errorReporter: ErrorReporter -) : BaseDescriptorLoader(kotlinClassFinder, errorReporter, storage), ConstantLoader { - - override fun loadPropertyConstant( - container: ProtoContainer, - proto: ProtoBuf.Callable, - nameResolver: NameResolver, - kind: AnnotatedCallableKind - ): CompileTimeConstant<*>? { - val signature = getCallableSignature(proto, nameResolver, kind) ?: return null - - val kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind) - if (kotlinClass == null) { - errorReporter.reportLoadingError("Kotlin class for loading property constant is not found: $container", null) - return null - } - - return storage.getStorageForClass(kotlinClass).propertyConstants[signature] - } -} diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DescriptorLoadersStorage.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DescriptorLoadersStorage.kt deleted file mode 100644 index dbcfda80ba3..00000000000 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DescriptorLoadersStorage.kt +++ /dev/null @@ -1,143 +0,0 @@ -/* - * 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.lang.resolve.kotlin - -import org.jetbrains.jet.lang.descriptors.ModuleDescriptor -import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.jet.lang.resolve.constants.* -import org.jetbrains.jet.lang.resolve.name.ClassId -import org.jetbrains.jet.lang.resolve.name.Name -import org.jetbrains.jet.storage.StorageManager - -import java.util.* -import kotlin.platform.platformStatic - -public class DescriptorLoadersStorage(storageManager: StorageManager, private val module: ModuleDescriptor) { - private val storage = storageManager.createMemoizedFunction{ - kotlinClass -> - loadAnnotationsAndInitializers(kotlinClass) - } - - public fun getStorageForClass(kotlinClass: KotlinJvmBinaryClass): Storage = storage(kotlinClass) - - private fun loadAnnotationsAndInitializers(kotlinClass: KotlinJvmBinaryClass): Storage { - val memberAnnotations = HashMap>() - val propertyConstants = HashMap>() - - kotlinClass.visitMembers(object : KotlinJvmBinaryClass.MemberVisitor { - override fun visitMethod(name: Name, desc: String): KotlinJvmBinaryClass.MethodAnnotationVisitor? { - return AnnotationVisitorForMethod(MemberSignature.fromMethodNameAndDesc(name.asString() + desc)) - } - - override fun visitField(name: Name, desc: String, initializer: Any?): KotlinJvmBinaryClass.AnnotationVisitor? { - val signature = MemberSignature.fromFieldNameAndDesc(name, desc) - - if (initializer != null) { - val normalizedValue: Any - if (desc in "ZBCS") { - val intValue = initializer as Int - if ("Z" == desc) { - normalizedValue = intValue != 0 - } - else if ("B" == desc) { - normalizedValue = (intValue.toByte()) - } - else if ("C" == desc) { - normalizedValue = (intValue.toChar()) - } - else if ("S" == desc) { - normalizedValue = (intValue.toShort()) - } - else { - throw AssertionError(desc) - } - } - else { - normalizedValue = initializer - } - - propertyConstants[signature] = createCompileTimeConstant( - normalizedValue, canBeUsedInAnnotation = true, isPureIntConstant = true, - usesVariableAsConstant = true, expectedType = null - ) - } - return MemberAnnotationVisitor(signature) - } - - inner class AnnotationVisitorForMethod(signature: MemberSignature) : MemberAnnotationVisitor(signature), KotlinJvmBinaryClass.MethodAnnotationVisitor { - - override fun visitParameterAnnotation(index: Int, classId: ClassId): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { - val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(signature, index) - var result = memberAnnotations[paramSignature] - if (result == null) { - result = ArrayList() - memberAnnotations[paramSignature] = result - } - return AnnotationDescriptorLoader.resolveAnnotation(classId, result, module) - } - } - - open inner class MemberAnnotationVisitor(protected val signature: MemberSignature) : KotlinJvmBinaryClass.AnnotationVisitor { - private val result = ArrayList() - - override fun visitAnnotation(classId: ClassId): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { - return AnnotationDescriptorLoader.resolveAnnotation(classId, result, module) - } - - override fun visitEnd() { - if (result.isNotEmpty()) { - memberAnnotations[signature] = result - } - } - } - }) - - return Storage(memberAnnotations, propertyConstants) - } - - // The purpose of this class is to hold a unique signature of either a method or a field, so that annotations on a member can be put - // into a map indexed by these signatures - public data class MemberSignature private(private val signature: String) { - - override fun toString(): String { - return signature - } - - class object { - platformStatic public fun fromMethodNameAndDesc(nameAndDesc: String): MemberSignature { - return MemberSignature(nameAndDesc) - } - - platformStatic public fun fromFieldNameAndDesc(name: Name, desc: String): MemberSignature { - return MemberSignature(name.asString() + "#" + desc) - } - - platformStatic public fun fromMethodSignatureAndParameterIndex(signature: MemberSignature, index: Int): MemberSignature { - return MemberSignature(signature.signature + "@" + index) - } - } - } - - class Storage( - public val memberAnnotations: Map>, - public val propertyConstants: Map> - ) { - class object { - public val EMPTY: Storage = Storage(mapOf(), mapOf()) - } - } -} diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DeserializationComponentsForJava.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DeserializationComponentsForJava.kt index bf5af064c59..500e00a82cc 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DeserializationComponentsForJava.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DeserializationComponentsForJava.kt @@ -27,12 +27,11 @@ public class DeserializationComponentsForJava( storageManager: StorageManager, moduleDescriptor: ModuleDescriptor, classDataFinder: JavaClassDataFinder, - annotationLoader: AnnotationDescriptorLoader, - constantLoader: ConstantDescriptorLoader, + annotationAndConstantLoader: BinaryClassAnnotationAndConstantLoader, packageFragmentProvider: LazyJavaPackageFragmentProvider ) { - val components = DeserializationComponents( - storageManager, moduleDescriptor, classDataFinder, annotationLoader, constantLoader, packageFragmentProvider, - JavaFlexibleTypeCapabilitiesDeserializer - ) + val components = DeserializationComponents( + storageManager, moduleDescriptor, classDataFinder, annotationAndConstantLoader, packageFragmentProvider, + JavaFlexibleTypeCapabilitiesDeserializer + ) } diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/MemberSignature.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/MemberSignature.kt new file mode 100644 index 00000000000..d95efa4ad75 --- /dev/null +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/MemberSignature.kt @@ -0,0 +1,38 @@ +/* + * 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.lang.resolve.kotlin + +import kotlin.platform.platformStatic +import org.jetbrains.jet.lang.resolve.name.Name + +// The purpose of this class is to hold a unique signature of either a method or a field, so that annotations on a member can be put +// into a map indexed by these signatures +data class MemberSignature private(private val signature: String) { + class object { + platformStatic public fun fromMethodNameAndDesc(nameAndDesc: String): MemberSignature { + return MemberSignature(nameAndDesc) + } + + platformStatic public fun fromFieldNameAndDesc(name: Name, desc: String): MemberSignature { + return MemberSignature(name.asString() + "#" + desc) + } + + platformStatic public fun fromMethodSignatureAndParameterIndex(signature: MemberSignature, index: Int): MemberSignature { + return MemberSignature(signature.signature + "@" + index) + } + } +} \ No newline at end of file diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/SignatureDeserializer.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/SignatureDeserializer.java index 2ef9c08eea2..92a48ec9e22 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/SignatureDeserializer.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/SignatureDeserializer.java @@ -24,7 +24,7 @@ import org.jetbrains.jet.lang.resolve.name.Name; public class SignatureDeserializer { // These types are ordered according to their sorts, this is significant for deserialization - private static final char[] PRIMITIVE_TYPES = new char[] { 'V', 'Z', 'C', 'B', 'S', 'I', 'F', 'J', 'D' }; + private static final char[] PRIMITIVE_TYPES = new char[] {'V', 'Z', 'C', 'B', 'S', 'I', 'F', 'J', 'D'}; private final NameResolver nameResolver; @@ -48,8 +48,8 @@ public class SignatureDeserializer { } @NotNull - public DescriptorLoadersStorage.MemberSignature methodSignature(@NotNull JavaProtoBuf.JavaMethodSignature signature) { - return DescriptorLoadersStorage.MemberSignature.fromMethodNameAndDesc(methodSignatureString(signature)); + public MemberSignature methodSignature(@NotNull JavaProtoBuf.JavaMethodSignature signature) { + return MemberSignature.fromMethodNameAndDesc(methodSignatureString(signature)); } @NotNull diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.kt b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.kt index 0abd1dd3e2c..74f9ce827b9 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.kt +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.kt @@ -17,8 +17,7 @@ package org.jetbrains.jet.lang.types.lang import org.jetbrains.jet.descriptors.serialization.* -import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader -import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader +import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationAndConstantLoader import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedPackageMemberScope import org.jetbrains.jet.lang.descriptors.ModuleDescriptor import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor @@ -47,8 +46,8 @@ class BuiltinsPackageFragment(storageManager: StorageManager, module: ModuleDesc loadPackage(), nameResolver, DeserializationComponents( - storageManager, module, BuiltInsClassDataFinder(), AnnotationLoader.UNSUPPORTED, // TODO: support annotations - ConstantLoader.UNSUPPORTED, provider, FlexibleTypeCapabilitiesDeserializer.ThrowException + storageManager, module, BuiltInsClassDataFinder(), AnnotationAndConstantLoader.UNSUPPORTED, // TODO: support annotations + provider, FlexibleTypeCapabilitiesDeserializer.ThrowException ), { readClassNames() } ) diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt index 48a178a5c5a..6e1600d49f3 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt @@ -114,7 +114,7 @@ public class MemberDeserializer(private val c: DeserializationContext) { property.setCompileTimeInitializer( c.storageManager.createNullableLazyValue { val container = c.containingDeclaration.asProtoContainer() - c.components.constantLoader.loadPropertyConstant(container, proto, c.nameResolver, AnnotatedCallableKind.PROPERTY) + c.components.annotationAndConstantLoader.loadPropertyConstant(container, proto, c.nameResolver, AnnotatedCallableKind.PROPERTY) } ) } @@ -165,7 +165,7 @@ public class MemberDeserializer(private val c: DeserializationContext) { return Annotations.EMPTY } return DeserializedAnnotations(c.storageManager) { - c.components.annotationLoader.loadCallableAnnotations( + c.components.annotationAndConstantLoader.loadCallableAnnotations( c.containingDeclaration.asProtoContainer(), proto, c.nameResolver, kind ) } @@ -197,7 +197,7 @@ public class MemberDeserializer(private val c: DeserializationContext) { return Annotations.EMPTY } return DeserializedAnnotations(c.storageManager) { - c.components.annotationLoader.loadValueParameterAnnotations(container, callable, c.nameResolver, kind, valueParameter) + c.components.annotationAndConstantLoader.loadValueParameterAnnotations(container, callable, c.nameResolver, kind, valueParameter) } } diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt index 0812cb7d7ab..9b576c8be49 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt @@ -19,16 +19,14 @@ package org.jetbrains.jet.descriptors.serialization.context import org.jetbrains.jet.storage.StorageManager import org.jetbrains.jet.lang.descriptors.* import org.jetbrains.jet.descriptors.serialization.* -import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader -import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader +import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationAndConstantLoader import org.jetbrains.jet.lang.resolve.name.ClassId public class DeserializationComponents( public val storageManager: StorageManager, public val moduleDescriptor: ModuleDescriptor, public val classDataFinder: ClassDataFinder, - public val annotationLoader: AnnotationLoader, - public val constantLoader: ConstantLoader, + public val annotationAndConstantLoader: AnnotationAndConstantLoader, public val packageFragmentProvider: PackageFragmentProvider, public val flexibleTypeCapabilitiesDeserializer: FlexibleTypeCapabilitiesDeserializer ) { diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/AnnotationLoader.java b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/AnnotationAndConstantLoader.java similarity index 71% rename from core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/AnnotationLoader.java rename to core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/AnnotationAndConstantLoader.java index 4b6dca8dd20..e223dd8c605 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/AnnotationLoader.java +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/AnnotationAndConstantLoader.java @@ -17,21 +17,23 @@ package org.jetbrains.jet.descriptors.serialization.descriptors; 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.annotations.AnnotationDescriptor; +import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant; import java.util.List; -public interface AnnotationLoader { - AnnotationLoader UNSUPPORTED = new AnnotationLoader() { +public interface AnnotationAndConstantLoader { + AnnotationAndConstantLoader UNSUPPORTED = new AnnotationAndConstantLoader() { @NotNull @Override public List loadClassAnnotations( @NotNull ProtoBuf.Class classProto, @NotNull NameResolver nameResolver ) { - return notSupported(); + return annotationsNotSupported(); } @NotNull @@ -42,7 +44,7 @@ public interface AnnotationLoader { @NotNull NameResolver nameResolver, @NotNull AnnotatedCallableKind kind ) { - return notSupported(); + return annotationsNotSupported(); } @NotNull @@ -54,11 +56,22 @@ public interface AnnotationLoader { @NotNull AnnotatedCallableKind kind, @NotNull ProtoBuf.Callable.ValueParameter proto ) { - return notSupported(); + return annotationsNotSupported(); + } + + @Nullable + @Override + public CompileTimeConstant loadPropertyConstant( + @NotNull ProtoContainer container, + @NotNull ProtoBuf.Callable proto, + @NotNull NameResolver nameResolver, + @NotNull AnnotatedCallableKind kind + ) { + throw new UnsupportedOperationException("Constants are not supported"); } @NotNull - private List notSupported() { + private List annotationsNotSupported() { throw new UnsupportedOperationException("Annotations are not supported"); } }; @@ -85,4 +98,12 @@ public interface AnnotationLoader { @NotNull AnnotatedCallableKind kind, @NotNull ProtoBuf.Callable.ValueParameter proto ); + + @Nullable + CompileTimeConstant loadPropertyConstant( + @NotNull ProtoContainer container, + @NotNull ProtoBuf.Callable proto, + @NotNull NameResolver nameResolver, + @NotNull AnnotatedCallableKind kind + ); } diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/ConstantLoader.java b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/ConstantLoader.java deleted file mode 100644 index a63ad0b686d..00000000000 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/ConstantLoader.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.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.resolve.constants.CompileTimeConstant; - -public interface ConstantLoader { - ConstantLoader UNSUPPORTED = new ConstantLoader() { - @Nullable - @Override - public CompileTimeConstant loadPropertyConstant( - @NotNull ProtoContainer container, - @NotNull ProtoBuf.Callable proto, - @NotNull NameResolver nameResolver, - @NotNull AnnotatedCallableKind kind - ) { - throw new UnsupportedOperationException("Constants are not supported"); - } - }; - - @Nullable - CompileTimeConstant loadPropertyConstant( - @NotNull ProtoContainer container, - @NotNull ProtoBuf.Callable proto, - @NotNull NameResolver nameResolver, - @NotNull AnnotatedCallableKind kind - ); -} diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt index f080d23308d..8ccd7a93ed4 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt @@ -70,7 +70,7 @@ public class DeserializedClassDescriptor( Annotations.EMPTY } else DeserializedAnnotations(c.storageManager) { - c.components.annotationLoader.loadClassAnnotations(classProto, c.nameResolver) + c.components.annotationAndConstantLoader.loadClassAnnotations(classProto, c.nameResolver) } override fun getContainingDeclaration(): DeclarationDescriptor = containingDeclaration diff --git a/idea/idea-analysis/src/org/jetbrains/jet/plugin/decompiler/textBuilder/DeserializerForDecompiler.kt b/idea/idea-analysis/src/org/jetbrains/jet/plugin/decompiler/textBuilder/DeserializerForDecompiler.kt index fa6d0e457b7..59c4640a4a1 100644 --- a/idea/idea-analysis/src/org/jetbrains/jet/plugin/decompiler/textBuilder/DeserializerForDecompiler.kt +++ b/idea/idea-analysis/src/org/jetbrains/jet/plugin/decompiler/textBuilder/DeserializerForDecompiler.kt @@ -92,11 +92,8 @@ public class DeserializerForDecompiler(val packageDirectory: VirtualFile, val di private val storageManager = LockBasedStorageManager.NO_LOCKS - private val loadersStorage = DescriptorLoadersStorage(storageManager, moduleDescriptor) - - private val annotationLoader = AnnotationDescriptorLoader(moduleDescriptor, loadersStorage, localClassFinder, LOGGING_REPORTER) - - private val constantLoader = ConstantDescriptorLoader(loadersStorage, localClassFinder, LOGGING_REPORTER) + private val annotationAndConstantLoader = + BinaryClassAnnotationAndConstantLoader(moduleDescriptor, storageManager, localClassFinder, LOGGING_REPORTER) private val classDataFinder = object : ClassDataFinder { override fun findClassData(classId: ClassId): ClassData? { @@ -135,7 +132,7 @@ public class DeserializerForDecompiler(val packageDirectory: VirtualFile, val di } private val deserializationComponents = DeserializationComponents( - storageManager, moduleDescriptor, classDataFinder, annotationLoader, constantLoader, packageFragmentProvider, + storageManager, moduleDescriptor, classDataFinder, annotationAndConstantLoader, packageFragmentProvider, JavaFlexibleTypeCapabilitiesDeserializer )