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