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); }
static FieldInfo FindJavaFieldProperty(KotlinFile kotlinClass, KotlinProperty property, ClassFile klass) { var possible_methods = klass.Fields.Where(field => field.Name == property.Name && TypesMatch(new TypeInfo(field.Descriptor, field.Descriptor), property.ReturnType, kotlinClass)); return(possible_methods.FirstOrDefault()); }
static MethodInfo?FindJavaMethod(KotlinFile kotlinFile, KotlinFunction function, ClassFile klass) { var possible_methods = klass.Methods.Where(method => method.Name == function.JvmName && method.GetFilteredParameters().Length == function.ValueParameters?.Count); foreach (var method in possible_methods) { if (function.ReturnType == null) { continue; } if (!TypesMatch(method.ReturnType, function.ReturnType, kotlinFile)) { continue; } if (!ParametersMatch(kotlinFile, method, function.ValueParameters !)) { continue; } return(method); } return(null); }
static MethodInfo FindJavaPropertyGetter(KotlinFile kotlinClass, KotlinProperty property, ClassFile klass) { var possible_methods = klass.Methods.Where(method => string.Compare(method.GetMethodNameWithoutSuffix(), $"get{property.Name}", true) == 0 && method.GetParameters().Length == 0 && TypesMatch(method.ReturnType, property.ReturnType, kotlinClass)); return(possible_methods.FirstOrDefault()); }
static MethodInfo?FindJavaPropertySetter(KotlinFile kotlinClass, KotlinProperty property, ClassFile klass) { var possible_methods = klass.Methods.Where(method => string.Compare(method.GetMethodNameWithoutSuffix(), $"set{property.Name}", StringComparison.OrdinalIgnoreCase) == 0 && property.ReturnType != null && method.GetParameters().Length == 1 && method.ReturnType.BinaryName == "V" && TypesMatch(method.GetParameters() [0].Type, property.ReturnType, kotlinClass)); return(possible_methods.FirstOrDefault()); }
public KotlinFile AsFileMetadata() { if (Kind != KotlinMetadataKind.File) { return(null); } var data = ParseStream <org.jetbrains.kotlin.metadata.jvm.Package> (); return(KotlinFile.FromProtobuf(data.Item1, data.Item2)); }
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); }
static bool ParametersMatch(KotlinFile kotlinClass, MethodInfo method, List <KotlinValueParameter> kotlinParameters) { var java_parameters = method.GetFilteredParameters(); if (java_parameters.Length == 0 && kotlinParameters.Count == 0) { return(true); } for (var i = 0; i < java_parameters.Length; i++) { var java_p = java_parameters [i]; var kotlin_p = kotlinParameters [i]; if (!TypesMatch(java_p.Type, kotlin_p.Type, kotlinClass)) { return(false); } } return(true); }