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.descriptors.serialization;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.utils.UtilsPackage;
021
022 import java.io.IOException;
023 import java.io.InputStream;
024 import java.io.OutputStream;
025
026 public class NameSerializationUtil {
027 private NameSerializationUtil() {
028 }
029
030 @NotNull
031 public static NameResolver deserializeNameResolver(@NotNull InputStream in) {
032 try {
033 ProtoBuf.SimpleNameTable simpleNames = ProtoBuf.SimpleNameTable.parseDelimitedFrom(in);
034 ProtoBuf.QualifiedNameTable qualifiedNames = ProtoBuf.QualifiedNameTable.parseDelimitedFrom(in);
035 return new NameResolver(simpleNames, qualifiedNames);
036 }
037 catch (IOException e) {
038 throw UtilsPackage.rethrow(e);
039 }
040 }
041
042 public static void serializeNameResolver(@NotNull OutputStream out, @NotNull NameResolver nameResolver) {
043 serializeNameTable(out, nameResolver.getSimpleNameTable(), nameResolver.getQualifiedNameTable());
044 }
045
046 public static void serializeNameTable(@NotNull OutputStream out, @NotNull NameTable nameTable) {
047 serializeNameTable(out, toSimpleNameTable(nameTable), toQualifiedNameTable(nameTable));
048 }
049
050 private static void serializeNameTable(
051 @NotNull OutputStream out,
052 @NotNull ProtoBuf.SimpleNameTable simpleNameTable,
053 @NotNull ProtoBuf.QualifiedNameTable qualifiedNameTable
054 ) {
055 try {
056 simpleNameTable.writeDelimitedTo(out);
057 qualifiedNameTable.writeDelimitedTo(out);
058 }
059 catch (IOException e) {
060 throw UtilsPackage.rethrow(e);
061 }
062 }
063
064 @NotNull
065 public static ProtoBuf.SimpleNameTable toSimpleNameTable(@NotNull NameTable nameTable) {
066 ProtoBuf.SimpleNameTable.Builder simpleNames = ProtoBuf.SimpleNameTable.newBuilder();
067 for (String simpleName : nameTable.getSimpleNames()) {
068 simpleNames.addName(simpleName);
069 }
070 return simpleNames.build();
071 }
072
073 @NotNull
074 public static ProtoBuf.QualifiedNameTable toQualifiedNameTable(@NotNull NameTable nameTable) {
075 ProtoBuf.QualifiedNameTable.Builder qualifiedNames = ProtoBuf.QualifiedNameTable.newBuilder();
076 for (ProtoBuf.QualifiedNameTable.QualifiedName.Builder qName : nameTable.getFqNames()) {
077 qualifiedNames.addQualifiedName(qName);
078 }
079 return qualifiedNames.build();
080 }
081
082 @NotNull
083 public static NameResolver createNameResolver(@NotNull NameTable table) {
084 return new NameResolver(toSimpleNameTable(table), toQualifiedNameTable(table));
085 }
086 }