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