001 /*
002 * Copyright 2010-2013 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.jet.lang.resolve.kotlin;
018
019 import kotlin.Function0;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.descriptors.serialization.ClassData;
023 import org.jetbrains.jet.descriptors.serialization.ClassId;
024 import org.jetbrains.jet.descriptors.serialization.JavaProtoBufUtil;
025 import org.jetbrains.jet.descriptors.serialization.NameResolver;
026 import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedPackageMemberScope;
027 import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
028 import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
029 import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
030 import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
031 import org.jetbrains.jet.lang.resolve.name.Name;
032 import org.jetbrains.jet.lang.resolve.scopes.JetScope;
033
034 import javax.inject.Inject;
035
036 import java.util.Collection;
037 import java.util.Collections;
038
039 import static org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader.Kind.CLASS;
040 import static org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader.Kind.PACKAGE_FACADE;
041
042 public final class DeserializedDescriptorResolver {
043 private DeserializationGlobalContextForJava context;
044
045 private ErrorReporter errorReporter;
046
047 @Inject
048 public void setContext(DeserializationGlobalContextForJava context) {
049 this.context = context;
050 }
051
052 @Inject
053 public void setErrorReporter(ErrorReporter errorReporter) {
054 this.errorReporter = errorReporter;
055 }
056
057 @Nullable
058 public ClassDescriptor resolveClass(@NotNull KotlinJvmBinaryClass kotlinClass) {
059 String[] data = readData(kotlinClass, CLASS);
060 if (data != null) {
061 return context.getClassDeserializer().deserializeClass(JavaProtoBufUtil.readClassDataFrom(data));
062 }
063 return null;
064 }
065
066 @Nullable
067 public JetScope createKotlinPackageScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull KotlinJvmBinaryClass kotlinClass) {
068 String[] data = readData(kotlinClass, PACKAGE_FACADE);
069 if (data != null) {
070 //all classes are included in java scope
071 return new DeserializedPackageMemberScope(descriptor, JavaProtoBufUtil.readPackageDataFrom(data), context,
072 new Function0<Collection<Name>>() {
073 @Override
074 public Collection<Name> invoke() {
075 return Collections.emptyList();
076 }
077 });
078 }
079 return null;
080 }
081
082 @Nullable
083 public String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull KotlinClassHeader.Kind expectedKind) {
084 KotlinClassHeader header = kotlinClass.getClassHeader();
085 if (header.getKind() == KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION) {
086 errorReporter.reportIncompatibleAbiVersion(kotlinClass, header.getVersion());
087 }
088 else if (header.getKind() == expectedKind) {
089 return header.getAnnotationData();
090 }
091
092 return null;
093 }
094 }