001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.load.kotlin;
018
019 import kotlin.jvm.functions.Function0;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.kotlin.descriptors.ClassDescriptor;
023 import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor;
024 import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader;
025 import org.jetbrains.kotlin.name.Name;
026 import org.jetbrains.kotlin.resolve.scopes.ChainedScope;
027 import org.jetbrains.kotlin.resolve.scopes.JetScope;
028 import org.jetbrains.kotlin.serialization.ClassData;
029 import org.jetbrains.kotlin.serialization.PackageData;
030 import org.jetbrains.kotlin.serialization.deserialization.ClassDataProvider;
031 import org.jetbrains.kotlin.serialization.deserialization.DeserializationComponents;
032 import org.jetbrains.kotlin.serialization.deserialization.ErrorReporter;
033 import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope;
034 import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil;
035
036 import javax.inject.Inject;
037 import java.util.*;
038
039 import static kotlin.KotlinPackage.setOf;
040 import static org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader.Kind.*;
041
042 public final class DeserializedDescriptorResolver {
043 private final ErrorReporter errorReporter;
044 private DeserializationComponents components;
045
046 public static final Set<KotlinClassHeader.Kind> KOTLIN_CLASS = setOf(CLASS);
047 public static final Set<KotlinClassHeader.Kind> KOTLIN_FILE_FACADE_OR_MULTIFILE_CLASS_PART = setOf(FILE_FACADE, MULTIFILE_CLASS_PART);
048 public static final Set<KotlinClassHeader.Kind> KOTLIN_PACKAGE_FACADE = setOf(PACKAGE_FACADE);
049
050 public DeserializedDescriptorResolver(@NotNull ErrorReporter errorReporter) {
051 this.errorReporter = errorReporter;
052 }
053
054 // component dependency cycle
055 @Inject
056 public void setComponents(@NotNull DeserializationComponentsForJava context) {
057 this.components = context.getComponents();
058 }
059
060 @Nullable
061 public ClassDescriptor resolveClass(@NotNull KotlinJvmBinaryClass kotlinClass) {
062 String[] data = readData(kotlinClass, KOTLIN_CLASS);
063 if (data != null) {
064 ClassData classData = JvmProtoBufUtil.readClassDataFrom(data);
065 KotlinJvmBinarySourceElement sourceElement = new KotlinJvmBinarySourceElement(kotlinClass);
066 ClassDataProvider classDataProvider = new ClassDataProvider(classData, sourceElement);
067 return components.getClassDeserializer().deserializeClass(kotlinClass.getClassId(), classDataProvider);
068 }
069 return null;
070 }
071
072 @Nullable
073 public JetScope createKotlinPackagePartScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull KotlinJvmBinaryClass kotlinClass) {
074 String[] data = readData(kotlinClass, KOTLIN_FILE_FACADE_OR_MULTIFILE_CLASS_PART);
075 if (data != null) {
076 //all classes are included in java scope
077 PackageData packageData = JvmProtoBufUtil.readPackageDataFrom(data);
078 return new DeserializedPackageMemberScope(
079 descriptor, packageData.getPackageProto(), packageData.getNameResolver(), components,
080 new Function0<Collection<Name>>() {
081 @Override
082 public Collection<Name> invoke() {
083 return Collections.emptyList();
084 }
085 }
086 );
087 }
088 return null;
089 }
090
091 @NotNull
092 public JetScope createKotlinPackageScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull List<KotlinJvmBinaryClass> packageParts) {
093 List<JetScope> list = new ArrayList<JetScope>();
094 for (KotlinJvmBinaryClass callable : packageParts) {
095 JetScope scope = createKotlinPackagePartScope(descriptor, callable);
096 if (scope != null) {
097 list.add(scope);
098 }
099 }
100 if (list.isEmpty()) {
101 return JetScope.Empty.INSTANCE$;
102 }
103 return new ChainedScope(descriptor, "Member scope for union of package parts data", list.toArray(new JetScope[list.size()]));
104 }
105
106 @Nullable
107 public String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull Set<KotlinClassHeader.Kind> expectedKinds) {
108 KotlinClassHeader header = kotlinClass.getClassHeader();
109 if (!header.getIsCompatibleAbiVersion()) {
110 errorReporter.reportIncompatibleAbiVersion(kotlinClass.getClassId(), kotlinClass.getLocation(), header.getVersion());
111 }
112 else if (expectedKinds.contains(header.getKind())) {
113 return header.getAnnotationData();
114 }
115
116 return null;
117 }
118 }