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.ClassDataWithSource;
030    import org.jetbrains.kotlin.serialization.PackageData;
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                return components.getClassDeserializer().deserializeClass(
067                        kotlinClass.getClassId(),
068                        new ClassDataWithSource(classData, sourceElement)
069                );
070            }
071            return null;
072        }
073    
074        @Nullable
075        public JetScope createKotlinPackagePartScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull KotlinJvmBinaryClass kotlinClass) {
076            String[] data = readData(kotlinClass, KOTLIN_FILE_FACADE_OR_MULTIFILE_CLASS_PART);
077            if (data != null) {
078                //all classes are included in java scope
079                PackageData packageData = JvmProtoBufUtil.readPackageDataFrom(data);
080                return new DeserializedPackageMemberScope(
081                        descriptor, packageData.getPackageProto(), packageData.getNameResolver(), components,
082                        new Function0<Collection<Name>>() {
083                            @Override
084                            public Collection<Name> invoke() {
085                                return Collections.emptyList();
086                            }
087                        }
088                );
089            }
090            return null;
091        }
092    
093        @NotNull
094        public JetScope createKotlinPackageScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull List<KotlinJvmBinaryClass> packageParts) {
095            List<JetScope> list = new ArrayList<JetScope>();
096            for (KotlinJvmBinaryClass callable : packageParts) {
097                JetScope scope = createKotlinPackagePartScope(descriptor, callable);
098                if (scope != null) {
099                    list.add(scope);
100                }
101            }
102            if (list.isEmpty()) {
103                return JetScope.Empty.INSTANCE$;
104            }
105            return new ChainedScope(descriptor, "Member scope for union of package parts data", list.toArray(new JetScope[list.size()]));
106        }
107    
108        @Nullable
109        public String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull Set<KotlinClassHeader.Kind> expectedKinds) {
110            KotlinClassHeader header = kotlinClass.getClassHeader();
111            if (!header.getIsCompatibleAbiVersion()) {
112                errorReporter.reportIncompatibleAbiVersion(kotlinClass.getClassId(), kotlinClass.getLocation(), header.getVersion());
113            }
114            else if (expectedKinds.contains(header.getKind())) {
115                return header.getAnnotationData();
116            }
117    
118            return null;
119        }
120    }