diff --git a/Generator/Constants.cs b/Generator/Constants.cs index 65b433ec..cdc08358 100644 --- a/Generator/Constants.cs +++ b/Generator/Constants.cs @@ -6,11 +6,13 @@ public class Constants public const string GenerationFolderPath = "model"; public const string StandardImports = """ - import com.fasterxml.jackson.annotation.*; + import com.fasterxml.jackson.annotation.JsonTypeInfo; + import com.fasterxml.jackson.annotation.JsonSubTypes; + import com.fasterxml.jackson.annotation.JsonTypeName; + import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.annotation.Nullable; import java.io.IOException; import java.time.OffsetDateTime; - import java.time.LocalDateTime; import java.util.Objects; import java.util.Arrays; import java.util.UUID; diff --git a/Generator/Extensions/StringExtensions.cs b/Generator/Extensions/StringExtensions.cs index 2a743db5..7c6fb64e 100644 --- a/Generator/Extensions/StringExtensions.cs +++ b/Generator/Extensions/StringExtensions.cs @@ -20,6 +20,4 @@ public static string SingularIfPossible(this string value) } public static string RemoveNullable(this string value) => value.Length > 10 ? value[..10] is "@Nullable " ? value[10..] : value : value; - - public static string AsFieldName(this string? fieldName) => fieldName == "default" ? "_default" : fieldName ?? null!; } \ No newline at end of file diff --git a/Generator/Generator.csproj b/Generator/Generator.csproj index 036adff5..73d9d275 100644 --- a/Generator/Generator.csproj +++ b/Generator/Generator.csproj @@ -9,7 +9,7 @@ - + diff --git a/Generator/JavaMemberWriters/JavaCreatorMethodWriter.cs b/Generator/JavaMemberWriters/JavaCreatorMethodWriter.cs index 989a88ac..9a2ce956 100644 --- a/Generator/JavaMemberWriters/JavaCreatorMethodWriter.cs +++ b/Generator/JavaMemberWriters/JavaCreatorMethodWriter.cs @@ -159,7 +159,7 @@ private void WriteConstructor(IndentedTextWriter writer, Type returnType, string writer.WriteLine($"public static {typeName} create({ParameterList(parameters)})"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"return new {typeName}({string.Join(", ", parameters.Select(p => p.Name.AsFieldName()))});"); + writer.WriteLine($"return new {typeName}({string.Join(", ", parameters.Select(p => p.Name))});"); writer.Indent--; writer.WriteLine("}"); @@ -188,15 +188,15 @@ private void WriteCreatorMethodBody(IndentedTextWriter writer, string variable, if (property!.PropertyType.IsGenericType && property!.PropertyType.GetGenericTypeDefinition() == typeof(List<>) && (parameter.ParameterType.IsArray || (parameter == parameters.Last() && javaWriter.BetterTypedParameterTypeName(parameter.ParameterType, new NullabilityInfoContext().Create(parameter)).Contains("...")))) { - writer.WriteLine($"{variable}.{propertyName.AsFieldName()} = new ArrayList<>(Arrays.asList({parameter.Name.AsFieldName()}));"); + writer.WriteLine($"{variable}.{propertyName} = new ArrayList<>(Arrays.asList({parameter.Name}));"); } else if (property!.PropertyType.IsArray && parameter.ParameterType.IsGenericType && parameter.ParameterType.GetGenericTypeDefinition() == typeof(List<>)) { - writer.WriteLine($"{variable}.{propertyName.AsFieldName()} = {parameter.Name.AsFieldName()}.asArray();"); + writer.WriteLine($"{variable}.{propertyName} = {parameter.Name}.asArray();"); } else { - writer.WriteLine($"{variable}.{propertyName.AsFieldName()} = {parameter.Name.AsFieldName()};"); + writer.WriteLine($"{variable}.{propertyName} = {parameter.Name};"); } } } @@ -206,7 +206,7 @@ private void WriteCreatorMethodBody(IndentedTextWriter writer, string variable, string? propertyName = property?.Name.ToCamelCase(); if (propertyName is not null) { - writer.WriteLine($"{variable}.{propertyName.AsFieldName()}{DefaultValueSetter(parameter)};"); + writer.WriteLine($"{variable}.{propertyName}{DefaultValueSetter(parameter)};"); } } } @@ -251,7 +251,6 @@ private string LiteralValueExpression(object obj) int number => $"{number}", double number => $"{number.ToString(CultureInfo.InvariantCulture)}d", float number => $"{number.ToString(CultureInfo.InvariantCulture)}f", - long number => $"{number.ToString(CultureInfo.InvariantCulture)}L", string stringLiteral => $"\"{stringLiteral}\"", _ when obj.GetType().IsEnum => $"{javaWriter.TypeName(obj.GetType())}.{obj}", _ => System.Text.Json.JsonSerializer.Serialize(obj) @@ -262,7 +261,7 @@ private string ParameterList(ParameterInfo[] parameters) { return string.Join(", ", parameters.Select(parameter => - $"{(parameters[^1] == parameter ? javaWriter.BetterTypedParameterTypeName(parameter.ParameterType, new NullabilityInfoContext().Create(parameter)) : javaWriter.TypeName(parameter))} {parameter.Name.AsFieldName()}" + $"{(parameters[^1] == parameter ? javaWriter.BetterTypedParameterTypeName(parameter.ParameterType, new NullabilityInfoContext().Create(parameter)) : javaWriter.TypeName(parameter))} {parameter.Name}" ) ); } diff --git a/Generator/JavaMemberWriters/JavaFieldWriter.cs b/Generator/JavaMemberWriters/JavaFieldWriter.cs index 49d42dab..b98f4b63 100644 --- a/Generator/JavaMemberWriters/JavaFieldWriter.cs +++ b/Generator/JavaMemberWriters/JavaFieldWriter.cs @@ -22,7 +22,7 @@ public void Write(IndentedTextWriter writer, Type classType, (PropertyInfo type, propertyInfo.GetCustomAttribute(typeof(ObsoleteAttribute)) is ObsoleteAttribute { } obsolete ? $"@deprecated {obsolete.Message}" : null ); - writer.WriteLine($"public {propertyTypeName} {lowerCaseName.AsFieldName()};"); + writer.WriteLine($"public {propertyTypeName} {lowerCaseName};"); } } } \ No newline at end of file diff --git a/Generator/JavaMemberWriters/JavaPropertyGetterMethodsWriter.cs b/Generator/JavaMemberWriters/JavaPropertyGetterMethodsWriter.cs index 1542091f..e3f8f035 100644 --- a/Generator/JavaMemberWriters/JavaPropertyGetterMethodsWriter.cs +++ b/Generator/JavaMemberWriters/JavaPropertyGetterMethodsWriter.cs @@ -22,15 +22,10 @@ public void Write(IndentedTextWriter writer, Type classType, (PropertyInfo info, propertyInfo.GetCustomAttribute(typeof(ObsoleteAttribute)) is ObsoleteAttribute { } obsolete ? $"@deprecated {obsolete.Message}" : null ); - if (lowerCaseName == "default") - { - writer.WriteLine("@JsonProperty(\"default\")"); - } - writer.WriteLine($"public {propertyTypeName} get{propertyName}()"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"return this.{lowerCaseName.AsFieldName()};"); + writer.WriteLine($"return this.{lowerCaseName};"); writer.Indent--; writer.WriteLine("}"); } diff --git a/Generator/JavaMemberWriters/JavaPropertySetterMethodsWriter.cs b/Generator/JavaMemberWriters/JavaPropertySetterMethodsWriter.cs index b8596010..1bb9fab8 100644 --- a/Generator/JavaMemberWriters/JavaPropertySetterMethodsWriter.cs +++ b/Generator/JavaMemberWriters/JavaPropertySetterMethodsWriter.cs @@ -49,10 +49,10 @@ public void Write(IndentedTextWriter writer, Type returnType, string returnTypeN { writer.WriteLine("@Override"); } - writer.WriteLine($"public {returnTypeName} set{propertyName}({parameterType} {lowerCaseName.AsFieldName()})"); + writer.WriteLine($"public {returnTypeName} set{propertyName}({parameterType} {lowerCaseName})"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"this.{javaWriter.ValueSetter(propertyType, lowerCaseName.AsFieldName(), propertyType is { IsGenericType: true } genericType && genericType.GetGenericTypeDefinition() == typeof(List<>) ? (genericType.GenericTypeArguments[0]).MakeArrayType() : propertyType, lowerCaseName.AsFieldName())};"); + writer.WriteLine($"this.{javaWriter.ValueSetter(propertyType, lowerCaseName, propertyType is { IsGenericType: true } genericType && genericType.GetGenericTypeDefinition() == typeof(List<>) ? (genericType.GenericTypeArguments[0]).MakeArrayType() : propertyType, lowerCaseName)};"); writer.WriteLine("return this;"); writer.Indent--; writer.WriteLine("}"); @@ -64,21 +64,21 @@ public void Write(IndentedTextWriter writer, Type returnType, string returnTypeN deprecationComment ); var elementType = propertyType.GetElementType()!; - writer.WriteLine($"public {returnTypeName} addTo{propertyName}({javaWriter.TypeName(elementType)} {lowerCaseName.SingularIfPossible().AsFieldName()})"); + writer.WriteLine($"public {returnTypeName} addTo{propertyName}({javaWriter.TypeName(elementType)} {lowerCaseName.SingularIfPossible()})"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"if (this.{lowerCaseName.AsFieldName()} == null)"); + writer.WriteLine($"if (this.{lowerCaseName} == null)"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"this.{lowerCaseName.AsFieldName()} = new {javaWriter.TypeName(elementType)}[] {{ {lowerCaseName.SingularIfPossible().AsFieldName()} }};"); + writer.WriteLine($"this.{lowerCaseName} = new {javaWriter.TypeName(elementType)}[] {{ {lowerCaseName.SingularIfPossible()} }};"); writer.Indent--; writer.WriteLine("}"); writer.WriteLine("else"); writer.WriteLine("{"); writer.Indent++; writer.WriteLine($"ArrayList<{javaWriter.TypeName(elementType)}> existingList = new ArrayList<>(Arrays.asList(this.{lowerCaseName}));"); - writer.WriteLine($"existingList.add({lowerCaseName.SingularIfPossible().AsFieldName()});"); - writer.WriteLine($"this.{lowerCaseName.AsFieldName()} = existingList.toArray({NewUpper(javaWriter.TypeName(elementType))}[0]);"); + writer.WriteLine($"existingList.add({lowerCaseName.SingularIfPossible()});"); + writer.WriteLine($"this.{lowerCaseName} = existingList.toArray({NewUpper(javaWriter.TypeName(elementType))}[0]);"); writer.Indent--; writer.WriteLine("}"); writer.WriteLine("return this;"); @@ -92,16 +92,16 @@ public void Write(IndentedTextWriter writer, Type returnType, string returnTypeN javaWriter.XmlDocumentation.GetSummary(returnType, propertyName), deprecationComment ); - writer.WriteLine($"public {returnTypeName} addTo{propertyName}({javaWriter.TypeName(propertyType.GenericTypeArguments[0])} {lowerCaseName.AsFieldName()})"); + writer.WriteLine($"public {returnTypeName} addTo{propertyName}({javaWriter.TypeName(propertyType.GenericTypeArguments[0])} {lowerCaseName})"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"if (this.{lowerCaseName.AsFieldName()} == null)"); + writer.WriteLine($"if (this.{lowerCaseName} == null)"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"this.{lowerCaseName.AsFieldName()} = new ArrayList<>();"); + writer.WriteLine($"this.{lowerCaseName} = new ArrayList<>();"); writer.Indent--; writer.WriteLine("}"); - writer.WriteLine($"this.{lowerCaseName.AsFieldName()}.add({lowerCaseName.AsFieldName()});"); + writer.WriteLine($"this.{lowerCaseName}.add({lowerCaseName});"); writer.WriteLine("return this;"); writer.Indent--; diff --git a/Generator/JavaTypeResolver.cs b/Generator/JavaTypeResolver.cs index db940133..c8e5e5c3 100644 --- a/Generator/JavaTypeResolver.cs +++ b/Generator/JavaTypeResolver.cs @@ -97,9 +97,12 @@ private string AddCollectionTypeDefinition(Type type) private string GetGenericTypeDefinition(Type type) { // We use `RemoveNullable` in the following section because Java does not support to annotate generic type arguments. - if (type.GenericTypeArguments.Length >= 1) + switch (type.GenericTypeArguments.Length) { - return string.Join("", type.GenericTypeArguments.Select(t => ResolveType(t).RemoveNullable())) + GetOrAddTypeDefinition(type); + case 1: + return $"{ResolveType(type.GenericTypeArguments.Single()).RemoveNullable()}{GetOrAddTypeDefinition(type)}"; + case 2: + return $"{ResolveType(type.GenericTypeArguments.First()).RemoveNullable()}{ResolveType(type.GenericTypeArguments.Last()).RemoveNullable()}{GetOrAddTypeDefinition(type)}"; } if (type.GetGenericArguments() is not [var genericTypeArgumentDefinition])