From ed6f908049880840fdf559aa652181a9782a5601 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 18 Sep 2013 22:17:53 +0400 Subject: [PATCH] Move JavaProtoBufUtil utilities to where they're used Serialization goes to backend: it's not needed in runtime, at least in the state in which it is now (dependent on ASM). Deserialization goes to AnnotationDescriptorDeserializer. Inline some methods, remove unneeded abstractions. This removes dependency of serialization.java on ASM --- .../jet/codegen/JavaSerializerExtension.java | 112 +++++++- .../AnnotationDescriptorDeserializer.java | 130 ++++++--- .../serialization.java/serialization.java.iml | 3 +- .../serialization/JavaProtoBufUtil.java | 265 ------------------ 4 files changed, 201 insertions(+), 309 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/JavaSerializerExtension.java b/compiler/backend/src/org/jetbrains/jet/codegen/JavaSerializerExtension.java index 8cc04a2cd5c..34e8affa058 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/JavaSerializerExtension.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/JavaSerializerExtension.java @@ -18,15 +18,16 @@ package org.jetbrains.jet.codegen; import com.intellij.openapi.util.Pair; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.asm4.Type; import org.jetbrains.asm4.commons.Method; -import org.jetbrains.jet.descriptors.serialization.JavaProtoBufUtil; -import org.jetbrains.jet.descriptors.serialization.NameTable; -import org.jetbrains.jet.descriptors.serialization.ProtoBuf; -import org.jetbrains.jet.descriptors.serialization.SerializerExtension; +import org.jetbrains.jet.descriptors.serialization.*; import org.jetbrains.jet.lang.descriptors.*; +import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; +import java.util.Arrays; + public class JavaSerializerExtension extends SerializerExtension { private final MemberMap memberMap; @@ -52,7 +53,7 @@ public class JavaSerializerExtension extends SerializerExtension { if (callable instanceof FunctionDescriptor) { Method method = memberMap.getMethodOfDescriptor((FunctionDescriptor) callable); if (method != null) { - JavaProtoBufUtil.saveMethodSignature(proto, method, nameTable); + proto.setExtension(JavaProtoBuf.methodSignature, new SignatureSerializer(nameTable).methodSignature(method)); } } else if (callable instanceof PropertyDescriptor) { @@ -81,8 +82,9 @@ public class JavaSerializerExtension extends SerializerExtension { syntheticMethodName = memberMap.getSyntheticMethodNameOfProperty(property); } - JavaProtoBufUtil.savePropertySignature(proto, fieldType, fieldName, isStaticInOuter, syntheticMethodName, getterMethod, - setterMethod, nameTable); + JavaProtoBuf.JavaPropertySignature signature = new SignatureSerializer(nameTable) + .propertySignature(fieldType, fieldName, isStaticInOuter, syntheticMethodName, getterMethod, setterMethod); + proto.setExtension(JavaProtoBuf.propertySignature, signature); } } @@ -93,7 +95,101 @@ public class JavaSerializerExtension extends SerializerExtension { ) { Name name = memberMap.getSrcClassNameOfCallable(callable); if (name != null) { - JavaProtoBufUtil.saveSrcClassName(proto, name, nameTable); + proto.setExtension(JavaProtoBuf.srcClassName, nameTable.getSimpleNameIndex(name)); + } + } + + private static class SignatureSerializer { + private final NameTable nameTable; + + public SignatureSerializer(@NotNull NameTable nameTable) { + this.nameTable = nameTable; + } + + @NotNull + public JavaProtoBuf.JavaMethodSignature methodSignature(@NotNull Method method) { + JavaProtoBuf.JavaMethodSignature.Builder signature = JavaProtoBuf.JavaMethodSignature.newBuilder(); + + signature.setName(nameTable.getSimpleNameIndex(Name.guess(method.getName()))); + + signature.setReturnType(type(method.getReturnType())); + + for (Type type : method.getArgumentTypes()) { + signature.addParameterType(type(type)); + } + + return signature.build(); + } + + @NotNull + public JavaProtoBuf.JavaPropertySignature propertySignature( + @Nullable Type fieldType, + @Nullable String fieldName, + boolean isStaticInOuter, + @Nullable String syntheticMethodName, + @Nullable Method getter, + @Nullable Method setter + ) { + JavaProtoBuf.JavaPropertySignature.Builder signature = JavaProtoBuf.JavaPropertySignature.newBuilder(); + + if (fieldType != null) { + assert fieldName != null : "Field name shouldn't be null when there's a field type: " + fieldType; + signature.setField(fieldSignature(fieldType, fieldName, isStaticInOuter)); + } + + if (syntheticMethodName != null) { + signature.setSyntheticMethodName(nameTable.getSimpleNameIndex(Name.guess(syntheticMethodName))); + } + + if (getter != null) { + signature.setGetter(methodSignature(getter)); + } + if (setter != null) { + signature.setSetter(methodSignature(setter)); + } + + return signature.build(); + } + + @NotNull + public JavaProtoBuf.JavaFieldSignature fieldSignature(@NotNull Type type, @NotNull String name, boolean isStaticInOuter) { + JavaProtoBuf.JavaFieldSignature.Builder signature = JavaProtoBuf.JavaFieldSignature.newBuilder(); + signature.setName(nameTable.getSimpleNameIndex(Name.guess(name))); + signature.setType(type(type)); + if (isStaticInOuter) { + signature.setIsStaticInOuter(true); + } + return signature.build(); + } + + @NotNull + public JavaProtoBuf.JavaType type(@NotNull Type givenType) { + JavaProtoBuf.JavaType.Builder builder = JavaProtoBuf.JavaType.newBuilder(); + + int arrayDimension = 0; + Type type = givenType; + while (type.getSort() == Type.ARRAY) { + arrayDimension++; + type = type.getElementType(); + } + if (arrayDimension != 0) { + builder.setArrayDimension(arrayDimension); + } + + if (type.getSort() == Type.OBJECT) { + FqName fqName = internalNameToFqName(type.getInternalName()); + builder.setClassFqName(nameTable.getFqNameIndex(fqName)); + } + else { + builder.setPrimitiveType(JavaProtoBuf.JavaType.PrimitiveType.valueOf(type.getSort())); + } + + return builder.build(); + } + + @NotNull + private static FqName internalNameToFqName(@NotNull String internalName) { + return FqName.fromSegments(Arrays.asList(internalName.split("/"))); } } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/AnnotationDescriptorDeserializer.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/AnnotationDescriptorDeserializer.java index d4116a37095..c3a0d462d30 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/AnnotationDescriptorDeserializer.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/AnnotationDescriptorDeserializer.java @@ -21,8 +21,7 @@ import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.asm4.*; -import org.jetbrains.asm4.commons.Method; -import org.jetbrains.jet.descriptors.serialization.JavaProtoBufUtil; +import org.jetbrains.jet.descriptors.serialization.JavaProtoBuf; import org.jetbrains.jet.descriptors.serialization.NameResolver; import org.jetbrains.jet.descriptors.serialization.ProtoBuf; import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationDeserializer; @@ -48,6 +47,7 @@ import java.io.IOException; import java.util.*; import static org.jetbrains.asm4.ClassReader.*; +import static org.jetbrains.asm4.Type.*; import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.IGNORE_KOTLIN_SOURCES; import static org.jetbrains.jet.lang.resolve.java.resolver.DeserializedResolverUtils.kotlinFqNameToJavaFqName; import static org.jetbrains.jet.lang.resolve.java.resolver.DeserializedResolverUtils.naiveKotlinFqName; @@ -247,7 +247,7 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer @NotNull NameResolver nameResolver ) { if (container instanceof NamespaceDescriptor) { - Name name = JavaProtoBufUtil.loadSrcClassName(proto, nameResolver); + Name name = loadSrcClassName(proto, nameResolver); if (name != null) { // To locate a package$src class, we first find the facade virtual file (*Package.class) and then look up the $src file in // the same directory. This hack is needed because FileManager doesn't find classfiles for $src classes @@ -263,7 +263,7 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer } else if (container instanceof ClassDescriptor && ((ClassDescriptor) container).getKind() == ClassKind.CLASS_OBJECT) { // Backing fields of properties of a class object are generated in the outer class - if (JavaProtoBufUtil.isStaticFieldInOuter(proto)) { + if (isStaticFieldInOuter(proto)) { return findVirtualFileByDescriptor((ClassOrNamespaceDescriptor) container.getContainingDeclaration()); } } @@ -271,6 +271,17 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer return findVirtualFileByDescriptor(container); } + @Nullable + private static Name loadSrcClassName(@NotNull ProtoBuf.Callable proto, @NotNull NameResolver nameResolver) { + return proto.hasExtension(JavaProtoBuf.srcClassName) ? nameResolver.getName(proto.getExtension(JavaProtoBuf.srcClassName)) : null; + } + + private static boolean isStaticFieldInOuter(@NotNull ProtoBuf.Callable proto) { + if (!proto.hasExtension(JavaProtoBuf.propertySignature)) return false; + JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); + return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter(); + } + @Nullable private static MemberSignature getCallableSignature( @NotNull ProtoBuf.Callable proto, @@ -279,18 +290,41 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer ) { switch (kind) { case FUNCTION: - return MemberSignature.fromMethod(JavaProtoBufUtil.loadMethodSignature(proto, nameResolver)); + if (proto.hasExtension(JavaProtoBuf.methodSignature)) { + JavaProtoBuf.JavaMethodSignature signature = proto.getExtension(JavaProtoBuf.methodSignature); + return new SignatureDeserializer(nameResolver).methodSignature(signature); + } + break; case PROPERTY_GETTER: - return MemberSignature.fromMethod(JavaProtoBufUtil.loadPropertyGetterSignature(proto, nameResolver)); + if (proto.hasExtension(JavaProtoBuf.propertySignature)) { + JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); + return new SignatureDeserializer(nameResolver).methodSignature(propertySignature.getGetter()); + } + break; case PROPERTY_SETTER: - return MemberSignature.fromMethod(JavaProtoBufUtil.loadPropertySetterSignature(proto, nameResolver)); + if (proto.hasExtension(JavaProtoBuf.propertySignature)) { + JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); + return new SignatureDeserializer(nameResolver).methodSignature(propertySignature.getSetter()); + } + break; case PROPERTY: - JavaProtoBufUtil.PropertyData data = JavaProtoBufUtil.loadPropertyData(proto, nameResolver); - return data == null ? null : - MemberSignature.fromPropertyData(data.getFieldType(), data.getFieldName(), data.getSyntheticMethodName()); - default: - return null; + if (proto.hasExtension(JavaProtoBuf.propertySignature)) { + JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); + + if (propertySignature.hasField()) { + JavaProtoBuf.JavaFieldSignature field = propertySignature.getField(); + Type type = new SignatureDeserializer(nameResolver).type(field.getType()); + Name name = nameResolver.getName(field.getName()); + return MemberSignature.fromFieldNameAndDesc(name.asString(), type.getDescriptor()); + } + else if (propertySignature.hasSyntheticMethodName()) { + Name name = nameResolver.getName(propertySignature.getSyntheticMethodName()); + return MemberSignature.fromMethodNameAndDesc(name.asString(), JvmAbi.ANNOTATED_PROPERTY_METHOD_SIGNATURE); + } + } + break; } + return null; } @NotNull @@ -352,28 +386,6 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer this.signature = signature; } - @Nullable - public static MemberSignature fromPropertyData( - @Nullable Type fieldType, - @Nullable String fieldName, - @Nullable String syntheticMethodName - ) { - if (fieldName != null && fieldType != null) { - return fromFieldNameAndDesc(fieldName, fieldType.getDescriptor()); - } - else if (syntheticMethodName != null) { - return fromMethodNameAndDesc(syntheticMethodName, JvmAbi.ANNOTATED_PROPERTY_METHOD_SIGNATURE); - } - else { - return null; - } - } - - @Nullable - public static MemberSignature fromMethod(@Nullable Method method) { - return method == null ? null : fromMethodNameAndDesc(method.getName(), method.getDescriptor()); - } - @NotNull public static MemberSignature fromMethodNameAndDesc(@NotNull String name, @NotNull String desc) { return new MemberSignature(name + desc); @@ -400,6 +412,56 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer } } + private static class SignatureDeserializer { + // These types are ordered according to their sorts, this is significant for deserialization + private static final Type[] PRIMITIVE_TYPES = new Type[] + { VOID_TYPE, BOOLEAN_TYPE, CHAR_TYPE, BYTE_TYPE, SHORT_TYPE, INT_TYPE, FLOAT_TYPE, LONG_TYPE, DOUBLE_TYPE }; + + private final NameResolver nameResolver; + + public SignatureDeserializer(@NotNull NameResolver nameResolver) { + this.nameResolver = nameResolver; + } + + @NotNull + public MemberSignature methodSignature(@NotNull JavaProtoBuf.JavaMethodSignature signature) { + String name = nameResolver.getName(signature.getName()).asString(); + + StringBuilder sb = new StringBuilder(); + sb.append('('); + for (int i = 0, length = signature.getParameterTypeCount(); i < length; i++) { + sb.append(type(signature.getParameterType(i)).getDescriptor()); + } + sb.append(')'); + sb.append(type(signature.getReturnType()).getDescriptor()); + + return MemberSignature.fromMethodNameAndDesc(name, sb.toString()); + } + + @NotNull + public Type type(@NotNull JavaProtoBuf.JavaType type) { + Type result; + if (type.hasPrimitiveType()) { + result = PRIMITIVE_TYPES[type.getPrimitiveType().ordinal()]; + } + else { + result = Type.getObjectType(fqNameToInternalName(nameResolver.getFqName(type.getClassFqName()))); + } + + StringBuilder brackets = new StringBuilder(type.getArrayDimension()); + for (int i = 0; i < type.getArrayDimension(); i++) { + brackets.append('['); + } + + return Type.getType(brackets + result.getDescriptor()); + } + + @NotNull + private static String fqNameToInternalName(@NotNull FqName fqName) { + return fqName.asString().replace('.', '/'); + } + } + @NotNull @Override public List loadValueParameterAnnotations(@NotNull ProtoBuf.Callable.ValueParameter parameterProto) { diff --git a/core/serialization.java/serialization.java.iml b/core/serialization.java/serialization.java.iml index b0975ee7c27..9a5bb30d03b 100644 --- a/core/serialization.java/serialization.java.iml +++ b/core/serialization.java/serialization.java.iml @@ -7,10 +7,9 @@ - + - diff --git a/core/serialization.java/src/org/jetbrains/jet/descriptors/serialization/JavaProtoBufUtil.java b/core/serialization.java/src/org/jetbrains/jet/descriptors/serialization/JavaProtoBufUtil.java index 4c8ec548f01..bbf431daefb 100644 --- a/core/serialization.java/src/org/jetbrains/jet/descriptors/serialization/JavaProtoBufUtil.java +++ b/core/serialization.java/src/org/jetbrains/jet/descriptors/serialization/JavaProtoBufUtil.java @@ -18,276 +18,11 @@ package org.jetbrains.jet.descriptors.serialization; import com.google.protobuf.ExtensionRegistryLite; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.asm4.Type; -import org.jetbrains.asm4.commons.Method; -import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.lang.resolve.name.Name; - -import java.util.Arrays; - -import static org.jetbrains.asm4.Type.*; public class JavaProtoBufUtil { private JavaProtoBufUtil() { } - @Nullable - public static Method loadMethodSignature(@NotNull ProtoBuf.Callable proto, @NotNull NameResolver nameResolver) { - if (!proto.hasExtension(JavaProtoBuf.methodSignature)) return null; - JavaProtoBuf.JavaMethodSignature signature = proto.getExtension(JavaProtoBuf.methodSignature); - return new Deserializer(nameResolver).methodSignature(signature); - } - - @Nullable - public static Method loadPropertyGetterSignature(@NotNull ProtoBuf.Callable proto, @NotNull NameResolver nameResolver) { - if (!proto.hasExtension(JavaProtoBuf.propertySignature)) return null; - JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); - return new Deserializer(nameResolver).methodSignature(propertySignature.getGetter()); - } - - @Nullable - public static Method loadPropertySetterSignature(@NotNull ProtoBuf.Callable proto, @NotNull NameResolver nameResolver) { - if (!proto.hasExtension(JavaProtoBuf.propertySignature)) return null; - JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); - return new Deserializer(nameResolver).methodSignature(propertySignature.getSetter()); - } - - public static class PropertyData { - private final Type fieldType; - private final String fieldName; - private final String syntheticMethodName; - - public PropertyData(@Nullable Type fieldType, @Nullable String fieldName, @Nullable String syntheticMethodName) { - this.fieldType = fieldType; - this.fieldName = fieldName; - this.syntheticMethodName = syntheticMethodName; - } - - @Nullable - public Type getFieldType() { - return fieldType; - } - - @Nullable - public String getFieldName() { - return fieldName; - } - - @Nullable - public String getSyntheticMethodName() { - return syntheticMethodName; - } - - @Override - public String toString() { - return fieldName != null ? "Field " + fieldName + " " + fieldType : "Synthetic method " + syntheticMethodName; - } - } - - @Nullable - public static PropertyData loadPropertyData(@NotNull ProtoBuf.Callable proto, @NotNull NameResolver nameResolver) { - if (!proto.hasExtension(JavaProtoBuf.propertySignature)) return null; - JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); - - if (propertySignature.hasField()) { - JavaProtoBuf.JavaFieldSignature field = propertySignature.getField(); - Type type = new Deserializer(nameResolver).type(field.getType()); - Name name = nameResolver.getName(field.getName()); - return new PropertyData(type, name.asString(), null); - } - else if (propertySignature.hasSyntheticMethodName()) { - Name name = nameResolver.getName(propertySignature.getSyntheticMethodName()); - return new PropertyData(null, null, name.asString()); - } - else { - return null; - } - } - - @Nullable - public static Name loadSrcClassName(@NotNull ProtoBuf.Callable proto, @NotNull NameResolver nameResolver) { - if (!proto.hasExtension(JavaProtoBuf.srcClassName)) return null; - return nameResolver.getName(proto.getExtension(JavaProtoBuf.srcClassName)); - } - - public static boolean isStaticFieldInOuter(@NotNull ProtoBuf.Callable proto) { - if (!proto.hasExtension(JavaProtoBuf.propertySignature)) return false; - JavaProtoBuf.JavaPropertySignature propertySignature = proto.getExtension(JavaProtoBuf.propertySignature); - return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter(); - } - - public static void saveMethodSignature(@NotNull ProtoBuf.Callable.Builder proto, @NotNull Method method, @NotNull NameTable nameTable) { - proto.setExtension(JavaProtoBuf.methodSignature, new Serializer(nameTable).methodSignature(method)); - } - - public static void savePropertySignature( - @NotNull ProtoBuf.Callable.Builder proto, - @Nullable Type fieldType, - @Nullable String fieldName, - boolean isStaticInOuter, - @Nullable String syntheticMethodName, - @Nullable Method getter, - @Nullable Method setter, - @NotNull NameTable nameTable - ) { - proto.setExtension(JavaProtoBuf.propertySignature, - new Serializer(nameTable).propertySignature(fieldType, fieldName, isStaticInOuter, syntheticMethodName, getter, setter)); - } - - public static void saveSrcClassName( - @NotNull ProtoBuf.Callable.Builder proto, - @NotNull Name name, - @NotNull NameTable nameTable - ) { - proto.setExtension(JavaProtoBuf.srcClassName, nameTable.getSimpleNameIndex(name)); - } - - private static class Serializer { - private final NameTable nameTable; - - public Serializer(@NotNull NameTable nameTable) { - this.nameTable = nameTable; - } - - @NotNull - public JavaProtoBuf.JavaMethodSignature methodSignature(@NotNull Method method) { - JavaProtoBuf.JavaMethodSignature.Builder signature = JavaProtoBuf.JavaMethodSignature.newBuilder(); - - signature.setName(nameTable.getSimpleNameIndex(Name.guess(method.getName()))); - - signature.setReturnType(type(method.getReturnType())); - - for (Type type : method.getArgumentTypes()) { - signature.addParameterType(type(type)); - } - - return signature.build(); - } - - @NotNull - public JavaProtoBuf.JavaPropertySignature propertySignature( - @Nullable Type fieldType, - @Nullable String fieldName, - boolean isStaticInOuter, - @Nullable String syntheticMethodName, - @Nullable Method getter, - @Nullable Method setter - ) { - JavaProtoBuf.JavaPropertySignature.Builder signature = JavaProtoBuf.JavaPropertySignature.newBuilder(); - - if (fieldType != null) { - assert fieldName != null : "Field name shouldn't be null when there's a field type: " + fieldType; - signature.setField(fieldSignature(fieldType, fieldName, isStaticInOuter)); - } - - if (syntheticMethodName != null) { - signature.setSyntheticMethodName(nameTable.getSimpleNameIndex(Name.guess(syntheticMethodName))); - } - - if (getter != null) { - signature.setGetter(methodSignature(getter)); - } - if (setter != null) { - signature.setSetter(methodSignature(setter)); - } - - return signature.build(); - } - - @NotNull - public JavaProtoBuf.JavaFieldSignature fieldSignature(@NotNull Type type, @NotNull String name, boolean isStaticInOuter) { - JavaProtoBuf.JavaFieldSignature.Builder signature = JavaProtoBuf.JavaFieldSignature.newBuilder(); - signature.setName(nameTable.getSimpleNameIndex(Name.guess(name))); - signature.setType(type(type)); - if (isStaticInOuter) { - signature.setIsStaticInOuter(true); - } - return signature.build(); - } - - @NotNull - public JavaProtoBuf.JavaType type(@NotNull Type givenType) { - JavaProtoBuf.JavaType.Builder builder = JavaProtoBuf.JavaType.newBuilder(); - - int arrayDimension = 0; - Type type = givenType; - while (type.getSort() == Type.ARRAY) { - arrayDimension++; - type = type.getElementType(); - } - if (arrayDimension != 0) { - builder.setArrayDimension(arrayDimension); - } - - if (type.getSort() == Type.OBJECT) { - FqName fqName = internalNameToFqName(type.getInternalName()); - builder.setClassFqName(nameTable.getFqNameIndex(fqName)); - } - else { - builder.setPrimitiveType(JavaProtoBuf.JavaType.PrimitiveType.valueOf(type.getSort())); - } - - return builder.build(); - } - - @NotNull - private static FqName internalNameToFqName(@NotNull String internalName) { - return FqName.fromSegments(Arrays.asList(internalName.split("/"))); - } - } - - private static class Deserializer { - // These types are ordered according to their sorts, this is significant for deserialization - private static final Type[] PRIMITIVE_TYPES = new Type[] - { VOID_TYPE, BOOLEAN_TYPE, CHAR_TYPE, BYTE_TYPE, SHORT_TYPE, INT_TYPE, FLOAT_TYPE, LONG_TYPE, DOUBLE_TYPE }; - - private final NameResolver nameResolver; - - public Deserializer(@NotNull NameResolver nameResolver) { - this.nameResolver = nameResolver; - } - - @NotNull - public Method methodSignature(@NotNull JavaProtoBuf.JavaMethodSignature signature) { - String name = nameResolver.getName(signature.getName()).asString(); - - Type returnType = type(signature.getReturnType()); - - int parameters = signature.getParameterTypeCount(); - Type[] parameterTypes = new Type[parameters]; - for (int i = 0; i < parameters; i++) { - parameterTypes[i] = type(signature.getParameterType(i)); - } - - return new Method(name, returnType, parameterTypes); - } - - @NotNull - private Type type(@NotNull JavaProtoBuf.JavaType type) { - Type result; - if (type.hasPrimitiveType()) { - result = PRIMITIVE_TYPES[type.getPrimitiveType().ordinal()]; - } - else { - result = Type.getObjectType(fqNameToInternalName(nameResolver.getFqName(type.getClassFqName()))); - } - - StringBuilder brackets = new StringBuilder(type.getArrayDimension()); - for (int i = 0; i < type.getArrayDimension(); i++) { - brackets.append('['); - } - - return Type.getType(brackets + result.getDescriptor()); - } - - @NotNull - private static String fqNameToInternalName(@NotNull FqName fqName) { - return fqName.asString().replace('.', '/'); - } - } - - @NotNull public static ExtensionRegistryLite getExtensionRegistry() { ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance();