Skip to content

Commit

Permalink
Revert "Last adjustments to support generic types with more than 2 ty…
Browse files Browse the repository at this point in the history
…pes parameters and to support the use of a property called "default"."

This reverts commit 8969f64.
  • Loading branch information
KSGRelewise committed Aug 12, 2024
1 parent 8969f64 commit fcb3898
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 32 deletions.
6 changes: 4 additions & 2 deletions Generator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions Generator/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
}
2 changes: 1 addition & 1 deletion Generator/Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="AngleSharp.Xml" Version="1.0.0" />
<PackageReference Include="Relewise.Client" Version="1.162.0" />
<PackageReference Include="Relewise.Client" Version="1.61.0" />
</ItemGroup>

</Project>
13 changes: 6 additions & 7 deletions Generator/JavaMemberWriters/JavaCreatorMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("}");

Expand Down Expand Up @@ -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};");
}
}
}
Expand All @@ -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)};");
}
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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}"
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion Generator/JavaMemberWriters/JavaFieldWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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};");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("}");
}
Expand Down
22 changes: 11 additions & 11 deletions Generator/JavaMemberWriters/JavaPropertySetterMethodsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("}");
Expand All @@ -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;");
Expand All @@ -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--;
Expand Down
7 changes: 5 additions & 2 deletions Generator/JavaTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit fcb3898

Please sign in to comment.