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.serialization.deserialization;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.name.ClassId;
021 import org.jetbrains.kotlin.name.FqName;
022 import org.jetbrains.kotlin.name.Name;
023 import org.jetbrains.kotlin.serialization.ProtoBuf;
024 import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
025
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.util.LinkedList;
029
030 import static org.jetbrains.kotlin.serialization.ProtoBuf.QualifiedNameTable.QualifiedName;
031
032 public class NameResolverImpl implements NameResolver {
033 @NotNull
034 public static NameResolverImpl read(@NotNull InputStream in) {
035 try {
036 ProtoBuf.StringTable simpleNames = ProtoBuf.StringTable.parseDelimitedFrom(in);
037 ProtoBuf.QualifiedNameTable qualifiedNames = ProtoBuf.QualifiedNameTable.parseDelimitedFrom(in);
038 return new NameResolverImpl(simpleNames, qualifiedNames);
039 }
040 catch (IOException e) {
041 throw ExceptionUtilsKt.rethrow(e);
042 }
043 }
044
045 private final ProtoBuf.StringTable strings;
046 private final ProtoBuf.QualifiedNameTable qualifiedNames;
047
048 private NameResolverImpl(
049 @NotNull ProtoBuf.StringTable strings,
050 @NotNull ProtoBuf.QualifiedNameTable qualifiedNames
051 ) {
052 this.strings = strings;
053 this.qualifiedNames = qualifiedNames;
054 }
055
056 @Override
057 @NotNull
058 public String getString(int index) {
059 return strings.getString(index);
060 }
061
062 @Override
063 @NotNull
064 public Name getName(int index) {
065 return Name.guess(strings.getString(index));
066 }
067
068 @Override
069 @NotNull
070 public ClassId getClassId(int index) {
071 LinkedList<String> packageFqName = new LinkedList<String>();
072 LinkedList<String> relativeClassName = new LinkedList<String>();
073 boolean local = false;
074
075 while (index != -1) {
076 QualifiedName proto = qualifiedNames.getQualifiedName(index);
077 String shortName = strings.getString(proto.getShortName());
078 switch (proto.getKind()) {
079 case CLASS:
080 relativeClassName.addFirst(shortName);
081 break;
082 case PACKAGE:
083 packageFqName.addFirst(shortName);
084 break;
085 case LOCAL:
086 relativeClassName.addFirst(shortName);
087 local = true;
088 break;
089 }
090
091 index = proto.getParentQualifiedName();
092 }
093
094 return new ClassId(FqName.fromSegments(packageFqName), FqName.fromSegments(relativeClassName), local);
095 }
096 }