Get rid of SerializationUtil, move methods to DescriptorSerializer

This commit is contained in:
Alexander Udalov
2015-09-16 18:14:16 +03:00
parent 04b8991bdd
commit 93b5065cf5
9 changed files with 55 additions and 149 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.serialization;
import com.google.protobuf.MessageLite;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,7 +28,11 @@ import org.jetbrains.kotlin.resolve.MemberComparator;
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
import org.jetbrains.kotlin.resolve.constants.NullValue;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.utils.UtilsPackage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -48,6 +53,24 @@ public class DescriptorSerializer {
this.extension = extension;
}
@NotNull
public byte[] serialize(@NotNull MessageLite message) {
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
serializeStringTable(result);
message.writeTo(result);
return result.toByteArray();
}
catch (IOException e) {
throw UtilsPackage.rethrow(e);
}
}
public void serializeStringTable(@NotNull OutputStream out) throws IOException {
stringTable.serializeSimpleNames().writeDelimitedTo(out);
stringTable.serializeQualifiedNames().writeDelimitedTo(out);
}
@NotNull
public static DescriptorSerializer createTopLevel(@NotNull SerializerExtension extension) {
return new DescriptorSerializer(new StringTable(extension), new Interner<TypeParameterDescriptor>(), extension);
@@ -1,87 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.serialization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.serialization.deserialization.NameResolver;
import org.jetbrains.kotlin.utils.UtilsPackage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SerializationUtil {
private SerializationUtil() {
}
@NotNull
public static byte[] serializeClassData(@NotNull ClassData classData) {
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
serializeNameResolver(result, classData.getNameResolver());
classData.getClassProto().writeTo(result);
return result.toByteArray();
}
catch (IOException e) {
throw UtilsPackage.rethrow(e);
}
}
@NotNull
public static byte[] serializePackageData(@NotNull PackageData packageData) {
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
serializeNameResolver(result, packageData.getNameResolver());
packageData.getPackageProto().writeTo(result);
return result.toByteArray();
}
catch (IOException e) {
throw UtilsPackage.rethrow(e);
}
}
@NotNull
public static byte[] serializeCallableData(@NotNull NameResolver nameResolver, @NotNull ProtoBuf.Callable callableProto) {
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
serializeNameResolver(result, nameResolver);
callableProto.writeTo(result);
return result.toByteArray();
}
catch (IOException e) {
throw UtilsPackage.rethrow(e);
}
}
private static void serializeNameResolver(@NotNull OutputStream out, @NotNull NameResolver nameResolver) {
serializeStringTable(out, nameResolver.getStringTable(), nameResolver.getQualifiedNameTable());
}
public static void serializeStringTable(
@NotNull OutputStream out,
@NotNull ProtoBuf.StringTable stringTable,
@NotNull ProtoBuf.QualifiedNameTable qualifiedNameTable
) {
try {
stringTable.writeDelimitedTo(out);
qualifiedNameTable.writeDelimitedTo(out);
}
catch (IOException e) {
throw UtilsPackage.rethrow(e);
}
}
}