Example #1
0
        public static string ConvertKotlinTypeSignature(KotlinType type, KotlinFile metadata = null)
        {
            if (type is null)
            {
                return(string.Empty);
            }

            var class_name = type.ClassName;

            if (string.IsNullOrWhiteSpace(class_name))
            {
                if (metadata is KotlinClass klass)
                {
                    var tp = klass.TypeParameters.FirstOrDefault(t => t.Id == type.TypeParameter);

                    if (tp?.UpperBounds.FirstOrDefault()?.ClassName != null)
                    {
                        return(ConvertKotlinClassToJava(tp.UpperBounds.FirstOrDefault()?.ClassName));
                    }
                }

                return("Ljava/lang/Object;");
            }

            var result = ConvertKotlinClassToJava(class_name);

            if (result == "[")
            {
                result += ConvertKotlinTypeSignature(type.Arguments.FirstOrDefault()?.Type);
            }

            return(result);
        }
Example #2
0
        internal static KotlinTypeTable FromProtobuf(TypeTable ta, JvmNameResolver resolver)
        {
            if (ta is null)
            {
                return(null);
            }

            return(new KotlinTypeTable {
                Types = ta.Types?.Select(t => KotlinType.FromProtobuf(t, resolver)).ToList(),
                FirstNullable = ta.FirstNullable
            });
        }
Example #3
0
        internal static KotlinTypeArgument FromProtobuf(Type.Argument ta, JvmNameResolver resolver)
        {
            if (ta is null)
            {
                return(null);
            }

            return(new KotlinTypeArgument {
                Projection = (KotlinProjection)ta.projection,
                Type = KotlinType.FromProtobuf(ta.Type, resolver),
                TypeId = ta.TypeId
            });
        }
Example #4
0
        internal static KotlinValueParameter FromProtobuf(ValueParameter vp, JvmNameResolver resolver)
        {
            if (vp is null)
            {
                return(null);
            }

            return(new KotlinValueParameter {
                Flags = vp.Flags,
                Name = resolver.GetString(vp.Name),
                Type = KotlinType.FromProtobuf(vp.Type, resolver),
                TypeId = vp.TypeId,
                VarArgElementType = KotlinType.FromProtobuf(vp.VarargElementType, resolver),
                VarArgElementTypeId = vp.VarargElementTypeId
            });
        }
Example #5
0
        internal static KotlinTypeParameter FromProtobuf(TypeParameter vp, JvmNameResolver resolver)
        {
            if (vp is null)
            {
                return(null);
            }

            return(new KotlinTypeParameter {
                Id = vp.Id,
                Name = resolver.GetString(vp.Name),
                Reified = vp.Reified,
                Variance = (KotlinVariance)vp.variance,
                UpperBounds = vp.UpperBounds?.Select(ub => KotlinType.FromProtobuf(ub, resolver)).ToList(),
                UpperBoundsIds = vp.UpperBoundIds
            });
        }
Example #6
0
        internal static KotlinExpression FromProtobuf(Expression exp, JvmNameResolver resolver)
        {
            if (exp is null)
            {
                return(null);
            }

            return(new KotlinExpression {
                Flags = exp.Flags,
                ValueParameterReference = exp.ValueParameterReference,
                ConstantValue = (KotlinConstantValue)exp.constant_value,
                IsInstanceType = KotlinType.FromProtobuf(exp.IsInstanceType, resolver),
                IsInstanceTypeId = exp.IsInstanceTypeId,
                AndArguments = exp.AndArguments?.Select(tp => KotlinExpression.FromProtobuf(tp, resolver)).ToList(),
                OrArguments = exp.OrArguments?.Select(vp => KotlinExpression.FromProtobuf(vp, resolver)).ToList()
            });
        }
Example #7
0
        internal static KotlinTypeAlias FromProtobuf(TypeAlias ta, JvmNameResolver resolver)
        {
            if (ta is null)
            {
                return(null);
            }

            return(new KotlinTypeAlias {
                Flags = ta.Flags,
                Name = resolver.GetString(ta.Name),
                TypeParameters = ta.TypeParameters?.Select(tp => KotlinTypeParameter.FromProtobuf(tp, resolver)).ToList(),
                UnderlyingType = KotlinType.FromProtobuf(ta.UnderlyingType, resolver),
                UnderlyingTypeId = ta.UnderlyingTypeId,
                ExpandedType = KotlinType.FromProtobuf(ta.ExpandedType, resolver),
                ExpandedTypeId = ta.ExpandedTypeId,
                VersionRequirements = ta.VersionRequirements
            });
        }
Example #8
0
        internal static KotlinFunction FromProtobuf(Function f, JvmNameResolver resolver)
        {
            if (f is null)
            {
                return(null);
            }

            return(new KotlinFunction {
                Flags = f.Flags,
                Name = resolver.GetString(f.Name),
                ReturnType = KotlinType.FromProtobuf(f.ReturnType, resolver),
                ReturnTypeId = f.ReturnTypeId,
                ReceiverType = KotlinType.FromProtobuf(f.ReceiverType, resolver),
                ReceiverTypeId = f.ReceiverTypeId,
                TypeParameters = f.TypeParameters?.Select(tp => KotlinTypeParameter.FromProtobuf(tp, resolver)).ToList(),
                ValueParameters = f.ValueParameters?.Select(vp => KotlinValueParameter.FromProtobuf(vp, resolver)).ToList(),
                VersionRequirements = f.VersionRequirements
            });
        }
Example #9
0
 internal static KotlinClass FromProtobuf(Class c, JvmNameResolver resolver)
 {
     return(new KotlinClass {
         CompanionObjectName = c.CompanionObjectName > 0 ? resolver.GetString(c.CompanionObjectName) : null,
         Constructors = c.Constructors?.Select(ctor => KotlinConstructor.FromProtobuf(ctor, resolver)).ToList(),
         EnumEntries = c.EnumEntries?.Select(e => resolver.GetString(e.Name)).ToList(),
         Flags = (KotlinClassFlags)c.Flags,
         FullyQualifiedName = c.FqName > 0 ? resolver.GetString(c.FqName) : null,
         Functions = c.Functions?.Select(f => KotlinFunction.FromProtobuf(f, resolver)).ToList(),
         NestedClassNames = c.NestedClassNames?.Select(n => resolver.GetString(n)).ToList(),
         Properties = c.Properties?.Select(p => KotlinProperty.FromProtobuf(p, resolver)).ToList(),
         SealedSubclassFullyQualifiedNames = c.SealedSubclassFqNames?.Select(n => resolver.GetString(n)).ToList(),
         SuperTypeIds = c.SupertypeIds?.Select(n => resolver.GetString(n)).ToList(),
         SuperTypes = c.Supertypes?.Select(st => KotlinType.FromProtobuf(st, resolver)).ToList(),
         TypeAliases = c.TypeAlias?.Select(tp => KotlinTypeAlias.FromProtobuf(tp, resolver)).ToList(),
         TypeParameters = c.TypeParameters?.Select(tp => KotlinTypeParameter.FromProtobuf(tp, resolver)).ToList(),
         VersionRequirements = c.VersionRequirements,
         TypeTable = KotlinTypeTable.FromProtobuf(c.TypeTable, resolver),
         VersionRequirementTable = KotlinVersionRequirementTable.FromProtobuf(c.VersionRequirementTable, resolver)
     });
 }
Example #10
0
        internal static KotlinProperty FromProtobuf(Property p, JvmNameResolver resolver)
        {
            if (p is null)
            {
                return(null);
            }

            return(new KotlinProperty {
                Flags = p.Flags,
                Name = resolver.GetString(p.Name),
                ReturnTypeId = p.ReturnTypeId,
                ReturnType = KotlinType.FromProtobuf(p.ReturnType, resolver),
                ReceiverType = KotlinType.FromProtobuf(p.ReceiverType, resolver),
                ReceiverTypeId = p.ReceiverTypeId,
                SetterValueParameter = KotlinValueParameter.FromProtobuf(p.SetterValueParameter, resolver),
                GetterFlags = p.GetterFlags,
                SetterFlags = p.SetterFlags,
                TypeParameters = p.TypeParameters?.Select(tp => KotlinTypeParameter.FromProtobuf(tp, resolver)).ToList(),
                VersionRequirements = p.VersionRequirements
            });
        }
Example #11
0
        static bool TypesMatch(TypeInfo javaType, KotlinType kotlinType, KotlinFile kotlinFile)
        {
            // Generic type
            if (!string.IsNullOrWhiteSpace(kotlinType.TypeParameterName) && $"T{kotlinType.TypeParameterName};" == javaType.TypeSignature)
            {
                return(true);
            }

            if (javaType.BinaryName == KotlinUtilities.ConvertKotlinTypeSignature(kotlinType, kotlinFile))
            {
                return(true);
            }

            // Could be a generic type erasure
            if (javaType.BinaryName == "Ljava/lang/Object;")
            {
                return(true);
            }

            // Sometimes Kotlin keeps its native types rather than converting them to Java native types
            // ie: "Lkotlin/UShort;" instead of "S"
            if (javaType.BinaryName.StartsWith("L", StringComparison.Ordinal) && javaType.BinaryName.EndsWith(";", StringComparison.Ordinal))
            {
                if (KotlinUtilities.ConvertKotlinClassToJava(javaType.BinaryName.Substring(1, javaType.BinaryName.Length - 2)) == KotlinUtilities.ConvertKotlinTypeSignature(kotlinType, kotlinFile))
                {
                    return(true);
                }
            }

            // Same for some arrays
            if (javaType.BinaryName.StartsWith("[L", StringComparison.Ordinal) && javaType.BinaryName.EndsWith(";", StringComparison.Ordinal))
            {
                if ("[" + KotlinUtilities.ConvertKotlinClassToJava(javaType.BinaryName.Substring(2, javaType.BinaryName.Length - 3)) == KotlinUtilities.ConvertKotlinTypeSignature(kotlinType, kotlinFile))
                {
                    return(true);
                }
            }

            return(false);
        }