Skip to content

Commit 760c502

Browse files
authored
Merge pull request #171 from Azure/daschult/RemoveJoda
Replace Joda with standard Java 8 types
2 parents c906db9 + 3aec111 commit 760c502

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1652
-1907
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ This project enables Java code generation in [AutoRest](https://github.com/Azure
4444
- With Argument (`true`): `int Plus(@NonNull Integer val1, @NonNull Integer val2)`
4545
- *--java.required-parameter-client-methods*: Whether or not Service and Method Group client method overloads that omit optional parameters will be created.
4646
- Default: `true`
47-
- *--java.string-dates*: Whether or not DateTime types should be represented as Strings (generally for better/different precision).
48-
- Default: `false`
49-
- Without Argument: `public DateTime duration(DateTime startTime, DateTime endTime)`
50-
- With Argument (`true`): `public String duration(String startTime, String endTime)`
5147

5248
# Contributing
5349

src/JavaCodeGenerator.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class JavaCodeGenerator : CodeGenerator
9191
"byte[]", "Byte[]",
9292
"String",
9393
"LocalDate",
94-
"DateTime",
94+
"OffsetDateTime",
9595
"DateTimeRfc1123",
9696
"Duration",
9797
"Period",
@@ -154,7 +154,6 @@ public override Task Generate(AutoRestCodeModel codeModel)
154154
package: codeModel.Namespace.ToLowerInvariant(),
155155
shouldGenerateXmlSerialization: codeModel.ShouldGenerateXmlSerialization,
156156
nonNullAnnotations: GetBoolSetting(autoRestSettings, "non-null-annotations", true),
157-
stringDates: GetBoolSetting(autoRestSettings, "string-dates"),
158157
clientTypePrefix: GetStringSetting(autoRestSettings, "client-type-prefix"),
159158
generateClientInterfaces: GetBoolSetting(autoRestSettings, "generate-client-interfaces", true),
160159
implementationSubpackage: GetStringSetting(autoRestSettings, "implementation-subpackage", "implementation"),
@@ -1428,13 +1427,13 @@ private static IType ParseType(AutoRestIModelType autoRestIModelType, JavaSettin
14281427
result = ArrayType.ByteArray;
14291428
break;
14301429
case AutoRestKnownPrimaryType.Date:
1431-
result = settings.StringDates ? ClassType.String : ClassType.JodaLocalDate;
1430+
result = ClassType.LocalDate;
14321431
break;
14331432
case AutoRestKnownPrimaryType.DateTime:
1434-
result = settings.StringDates ? ClassType.String : ClassType.JodaDateTime;
1433+
result = ClassType.DateTime;
14351434
break;
14361435
case AutoRestKnownPrimaryType.DateTimeRfc1123:
1437-
result = settings.StringDates ? ClassType.String : ClassType.DateTimeRfc1123;
1436+
result = ClassType.DateTimeRfc1123;
14381437
break;
14391438
case AutoRestKnownPrimaryType.Double:
14401439
result = PrimitiveType.Double;
@@ -1462,10 +1461,10 @@ private static IType ParseType(AutoRestIModelType autoRestIModelType, JavaSettin
14621461
}
14631462
break;
14641463
case AutoRestKnownPrimaryType.TimeSpan:
1465-
result = ClassType.JodaPeriod;
1464+
result = ClassType.Duration;
14661465
break;
14671466
case AutoRestKnownPrimaryType.UnixTime:
1468-
result = settings.StringDates ? (IType)ClassType.String : PrimitiveType.UnixTimeLong;
1467+
result = PrimitiveType.UnixTimeLong;
14691468
break;
14701469
case AutoRestKnownPrimaryType.Uuid:
14711470
result = ClassType.UUID;
@@ -2684,7 +2683,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
26842683
string propertyConversion = null;
26852684
switch (sourceTypeName.ToLower())
26862685
{
2687-
case "datetime":
2686+
case "offsetdatetime":
26882687
switch (targetTypeName.ToLower())
26892688
{
26902689
case "datetimerfc1123":
@@ -2696,7 +2695,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
26962695
case "datetimerfc1123":
26972696
switch (targetTypeName.ToLower())
26982697
{
2699-
case "datetime":
2698+
case "offsetdatetime":
27002699
propertyConversion = $"{expression}.dateTime()";
27012700
break;
27022701
}
@@ -2742,7 +2741,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
27422741
{
27432742
switch (sourceTypeName.ToLower())
27442743
{
2745-
case "datetime":
2744+
case "offsetdatetime":
27462745
switch (targetTypeName.ToLower())
27472746
{
27482747
case "datetimerfc1123":
@@ -2754,7 +2753,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
27542753
case "datetimerfc1123":
27552754
switch (targetTypeName.ToLower())
27562755
{
2757-
case "datetime":
2756+
case "offsetdatetime":
27582757
propertyConversion = $"{expression}.dateTime()";
27592758
break;
27602759
}
@@ -3032,7 +3031,7 @@ private static IType ConvertToClientType(IType modelType)
30323031
}
30333032
else if (modelType == ClassType.DateTimeRfc1123)
30343033
{
3035-
clientType = ClassType.JodaDateTime;
3034+
clientType = ClassType.DateTime;
30363035
}
30373036
else if (modelType == PrimitiveType.UnixTimeLong)
30383037
{
@@ -3180,7 +3179,7 @@ private static string AutoRestIModelTypeName(AutoRestIModelType autoRestModelTyp
31803179
result = "LocalDate";
31813180
break;
31823181
case AutoRestKnownPrimaryType.DateTime:
3183-
result = "DateTime";
3182+
result = "OffsetDateTime";
31843183
break;
31853184
case AutoRestKnownPrimaryType.DateTimeRfc1123:
31863185
result = "DateTimeRfc1123";
@@ -3303,14 +3302,14 @@ private static void ParameterConvertClientTypeToWireType(JavaBlock block, JavaSe
33033302
{
33043303
if (parameterIsRequired)
33053304
{
3306-
block.Line($"Long {target} = {source}.toDateTime(DateTimeZone.UTC).getMillis() / 1000;");
3305+
block.Line($"Long {target} = {source}.toInstant().getEpochSecond();");
33073306
}
33083307
else
33093308
{
33103309
block.Line($"Long {target} = null;");
33113310
block.If($"{source} != null", ifBlock =>
33123311
{
3313-
ifBlock.Line($"{target} = {source}.toDateTime(DateTimeZone.UTC).getMillis() / 1000;");
3312+
ifBlock.Line($"{target} = {source}.toInstant().getEpochSecond();");
33143313
});
33153314
}
33163315
}
@@ -4597,10 +4596,10 @@ private static IEnumerable<string> GetExpressionsToValidate(RestAPIMethod restAP
45974596
parameterType != ClassType.Double &&
45984597
parameterType != ClassType.BigDecimal &&
45994598
parameterType != ClassType.String &&
4600-
parameterType != ClassType.JodaDateTime &&
4601-
parameterType != ClassType.JodaLocalDate &&
4599+
parameterType != ClassType.DateTime &&
4600+
parameterType != ClassType.LocalDate &&
46024601
parameterType != ClassType.DateTimeRfc1123 &&
4603-
parameterType != ClassType.JodaPeriod &&
4602+
parameterType != ClassType.Duration &&
46044603
parameterType != ClassType.Boolean &&
46054604
parameterType != ClassType.ServiceClientCredentials &&
46064605
parameterType != ClassType.AzureTokenCredentials &&

src/JavaSettings.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public JavaSettings(
4444
string package,
4545
bool shouldGenerateXmlSerialization,
4646
bool nonNullAnnotations,
47-
bool stringDates,
4847
string clientTypePrefix,
4948
bool generateClientInterfaces,
5049
string implementationSubpackage,
@@ -62,7 +61,6 @@ public JavaSettings(
6261
Package = package;
6362
ShouldGenerateXmlSerialization = shouldGenerateXmlSerialization;
6463
NonNullAnnotations = nonNullAnnotations;
65-
StringDates = stringDates;
6664
ClientTypePrefix = clientTypePrefix;
6765
GenerateClientInterfaces = generateClientInterfaces;
6866
ImplementationSubpackage = implementationSubpackage;
@@ -100,11 +98,6 @@ public bool AddCredentials
10098
/// </summary>
10199
public bool NonNullAnnotations { get; }
102100

103-
/// <summary>
104-
/// Whether or not DateTime types should be represented as Strings (generally for better/different precision).
105-
/// </summary>
106-
public bool StringDates { get; }
107-
108101
/// <summary>
109102
/// The prefix that will be added to each generated client type.
110103
/// </summary>

src/Model/ClassType.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public class ClassType : IType
1717
public static readonly ClassType Double = new ClassType("java.lang", "Double", defaultValueExpressionConverter: (string defaultValueExpression) => double.Parse(defaultValueExpression).ToString());
1818
public static readonly ClassType String = new ClassType("java.lang", "String", defaultValueExpressionConverter: (string defaultValueExpression) => CodeNamer.Instance.QuoteValue(defaultValueExpression));
1919
public static readonly ClassType Base64Url = new ClassType("com.microsoft.rest.v2", "Base64Url");
20-
public static readonly ClassType JodaLocalDate = new ClassType("org.joda.time", "LocalDate", defaultValueExpressionConverter: (string defaultValueExpression) => $"LocalDate.parse(\"{defaultValueExpression}\")");
21-
public static readonly ClassType JodaDateTime = new ClassType("org.joda.time", "DateTime", defaultValueExpressionConverter: (string defaultValueExpression) => $"DateTime.parse(\"{defaultValueExpression}\")");
22-
public static readonly ClassType JodaPeriod = new ClassType("org.joda.time", "Period", defaultValueExpressionConverter: (string defaultValueExpression) => $"Period.parse(\"{defaultValueExpression}\")");
23-
public static readonly ClassType DateTimeRfc1123 = new ClassType("com.microsoft.rest.v2", "DateTimeRfc1123", defaultValueExpressionConverter: (string defaultValueExpression) => $"DateTime.parse(\"{defaultValueExpression}\")");
20+
public static readonly ClassType LocalDate = new ClassType("java.time", "LocalDate", defaultValueExpressionConverter: (string defaultValueExpression) => $"LocalDate.parse(\"{defaultValueExpression}\")");
21+
public static readonly ClassType DateTime = new ClassType("java.time", "OffsetDateTime", defaultValueExpressionConverter: (string defaultValueExpression) => $"OffsetDateTime.parse(\"{defaultValueExpression}\")");
22+
public static readonly ClassType Duration = new ClassType("java.time", "Duration", defaultValueExpressionConverter: (string defaultValueExpression) => $"Duration.parse(\"{defaultValueExpression}\")");
23+
public static readonly ClassType DateTimeRfc1123 = new ClassType("com.microsoft.rest.v2", "DateTimeRfc1123", defaultValueExpressionConverter: (string defaultValueExpression) => $"new DateTimeRfc1123(\"{defaultValueExpression}\")");
2424
public static readonly ClassType BigDecimal = new ClassType("java.math", "BigDecimal");
2525
public static readonly ClassType UUID = new ClassType("java.util", "UUID");
2626
public static readonly ClassType Object = new ClassType("java.lang", "Object");
@@ -29,7 +29,7 @@ public class ClassType : IType
2929
public static readonly ClassType CloudException = new ClassType("com.microsoft.azure.v2", "CloudException");
3030
public static readonly ClassType RestException = new ClassType("com.microsoft.azure.v2", "RestException");
3131
public static readonly ClassType UnixTime = new ClassType("com.microsoft.rest.v2", "UnixTime");
32-
public static readonly ClassType UnixTimeDateTime = new ClassType("org.joda.time", "DateTime", new[] { "org.joda.time.DateTimeZone" });
32+
public static readonly ClassType UnixTimeDateTime = new ClassType("java.time", "OffsetDateTime");
3333
public static readonly ClassType UnixTimeLong = new ClassType("java.lang", "Long");
3434
public static readonly ClassType AzureEnvironment = new ClassType("com.microsoft.azure.v2", "AzureEnvironment");
3535
public static readonly ClassType HttpPipeline = new ClassType("com.microsoft.rest.v2.http", "HttpPipeline");

0 commit comments

Comments
 (0)