/// <summary> /// Adds a parameter value. /// </summary> private void AddParameterValue(JavaClass javaClass, object obj, JavaClassProperty prop, bool last) { var separator = last ? "" : ","; var rawValue = javaClass.Type.GetProperty(prop.Name).GetValue(obj); if (prop.IsCollection) { AddCollectionParameterValue(prop, rawValue, separator); } else if (prop.JavaClass != null) { GenerateConstructorInvocation(rawValue, prefix: $"/* {prop.Name}: */ ", suffix: separator); } else if (prop.JavaType == "String") { AddStringParameterValue(prop, rawValue, separator); } else if (prop.JavaType == "int") { AddPrimitiveParameterValue(prop, rawValue.ToString(), separator); } else if (prop.JavaType == "boolean") { AddPrimitiveParameterValue(prop, rawValue.Equals(true) ? "true" : "false", separator); } else { throw new InvalidOperationException("Unsupported property type."); } }
/// <summary> /// Adds a primitive parameter value. /// </summary> private void AddPrimitiveParameterValue( JavaClassProperty prop, string paramValue, string separator) { _builder.AddLine($"/* {prop.Name}: */ {paramValue}{separator}"); }
/// <summary> /// Adds a parameter value for a string. /// </summary> /// <param name="prop"></param> /// <param name="rawValue"></param> /// <param name="separator"></param> private void AddStringParameterValue(JavaClassProperty prop, object rawValue, string separator) { var lines = ((string)rawValue)?.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); if (lines != null && lines.Length > 1) { for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++) { var line = lines[lineIndex]; if (lineIndex == 0) { _builder.AddLine($"/* {prop.Name}: */ {EscapeLine(line, newLine: true)}"); } else if (lineIndex < lines.Length - 1) { _builder.AddLine($"\t+ {EscapeLine(line, newLine: true)}"); } else { _builder.AddLine($"\t+ {EscapeLine(line, newLine: false)}{separator}"); } } } else if (lines != null) { _builder.AddLine($"/* {prop.Name}: */ {EscapeLine((string)rawValue, newLine: false)}{separator}"); } else { _builder.AddLine($"/* {prop.Name}: */ null{separator}"); } }
/// <summary> /// Adds a parameter value for a collection property. /// </summary> private void AddCollectionParameterValue(JavaClassProperty prop, object rawValue, string collectionSeparator) { var items = ((IEnumerable)(rawValue ?? Enumerable.Empty <object>())).Cast <object>().ToList(); var lastItem = items.Count > 0 ? items[items.Count - 1] : null; _builder.AddLine($"/* {prop.Name}: */ Arrays.asList(new {prop.JavaClass.ClassName}[]"); _builder.BeginScope("{"); foreach (var item in items) { var itemSeparator = item == lastItem ? "" : ","; GenerateConstructorInvocation(item, prefix: string.Empty, suffix: itemSeparator); } _builder.EndScope($"}}){collectionSeparator}"); }
/// <summary> /// Returns definitions for each property accessor. /// </summary> private string GetAccessor(JavaClassProperty prop) { return($"public {prop.JavaType} get{prop.Name}() {{ return {prop.Name.ToCamelCase()}; }}"); }
/// <summary> /// Returns a field definition. /// </summary> private string GetField(JavaClassProperty prop) { return($"private {prop.JavaType} {prop.Name.ToCamelCase()};"); }