Skip to content

Commit 0ce9947

Browse files
committed
Generate anonymous classes in vanilla mode
1 parent 95ff028 commit 0ce9947

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

src/JavaBlock.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public void ReturnAnonymousClass(string anonymousClassDeclaration, Action<JavaCl
7171
contents.ReturnAnonymousClass(anonymousClassDeclaration, anonymousClassBlock);
7272
}
7373

74+
public void AnonymousClass(string anonymousClassDeclaration, Action<JavaClass> anonymousClassBlock)
75+
{
76+
contents.AnonymousClass(anonymousClassDeclaration, anonymousClassBlock);
77+
}
78+
7479
public JavaIfBlock If(string condition, Action<JavaBlock> ifAction)
7580
{
7681
contents.If(condition, ifAction);

src/JavaCodeGenerator.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4280,14 +4280,19 @@ restAPIMethodReturnBodyClientType is ListType restAPIMethodReturnBodyClientListT
42804280
}
42814281

42824282
IType returnValueTypeArgumentType = ((GenericType)restAPIMethod.ReturnType).TypeArguments.Single();
4283+
// TODO: generate anonymous classes for paging
4284+
IType mapperReturnType = ((GenericType)returnValueTypeArgumentType).TypeArguments.Last();
42834285

42844286
string restAPIMethodArgumentList = GetRestAPIMethodArgumentList(autoRestMethodOrderedRetrofitParameters, settings);
42854287

42864288
function.Line($"return service.{restAPIMethod.Name}({restAPIMethodArgumentList})");
42874289
function.Indent(() =>
42884290
{
42894291
function.Text(".map(");
4290-
function.Lambda(returnValueTypeArgumentType.ToString(), "res", "res.body()");
4292+
function.AnonymousClass($"new Function<{returnValueTypeArgumentType}, {mapperReturnType}>()", anonymousClass =>
4293+
{
4294+
anonymousClass.PublicMethod($"{mapperReturnType} apply(RestResponse<?, ?> res)", method => method.Return("res.body()"));
4295+
});
42914296
function.Line(");");
42924297
});
42934298
});
@@ -4342,12 +4347,18 @@ restAPIMethodReturnBodyClientType is ListType restAPIMethodReturnBodyClientListT
43424347
ConvertClientTypesToWireTypes(function, autoRestMethodRetrofitParameters, methodClientReference, settings);
43434348

43444349
IType returnValueTypeArgumentType = ((GenericType)restAPIMethod.ReturnType).TypeArguments.Single();
4350+
IType mapperReturnType = clientMethod.ReturnValue.Type;
4351+
43454352
string restAPIMethodArgumentList = GetRestAPIMethodArgumentList(autoRestMethodOrderedRetrofitParameters, settings);
43464353
function.Line($"return service.{clientMethod.RestAPIMethod.Name}({restAPIMethodArgumentList})");
43474354
function.Indent(() =>
43484355
{
43494356
function.Text(".map(");
4350-
function.Lambda(returnValueTypeArgumentType.ToString(), "res", "res.body()");
4357+
4358+
function.AnonymousClass($"new Function<{returnValueTypeArgumentType}, {mapperReturnType}>()", anonymousClass =>
4359+
{
4360+
anonymousClass.PublicMethod($"{mapperReturnType} apply(RestResponse<?, {mapperReturnType}> res)", method => method.Return("res.body()"));
4361+
});
43514362
function.Line(")");
43524363
function.Line(".toObservable();");
43534364
});
@@ -4551,16 +4562,27 @@ restAPIMethodReturnBodyClientType is ListType restAPIMethodReturnBodyClientListT
45514562
{
45524563
GenericType restAPIMethodClientReturnType = (GenericType)ConvertToClientType(restAPIMethod.ReturnType);
45534564
IType returnValueTypeArgumentClientType = restAPIMethodClientReturnType.TypeArguments.Single();
4565+
IType mapperReturnType = clientMethod.ReturnValue.Type;
45544566
if (restAPIMethodReturnBodyClientType != PrimitiveType.Void)
45554567
{
45564568
function.Text($".flatMapMaybe(");
4557-
function.Lambda(returnValueTypeArgumentClientType.ToString(), "res", "res.body() == null ? Maybe.empty() : Maybe.just(res.body())");
4569+
function.AnonymousClass($"new Function<{returnValueTypeArgumentClientType}, {mapperReturnType}>()", anonymousClassBlock =>
4570+
{
4571+
anonymousClassBlock.PublicMethod(
4572+
$"{mapperReturnType} apply({returnValueTypeArgumentClientType} res)",
4573+
method => method.Return($"res.body() == null ? Maybe.<{restAPIMethodReturnBodyClientType.AsNullable()}>empty() : Maybe.just(res.body())"));
4574+
});
45584575
function.Line(");");
45594576
}
45604577
else if (isFluentDelete)
45614578
{
45624579
function.Text($".flatMapMaybe(");
4563-
function.Lambda(returnValueTypeArgumentClientType.ToString(), "res", "Maybe.empty()");
4580+
function.AnonymousClass($"new Function<{returnValueTypeArgumentClientType}, {GenericType.Maybe(ClassType.Void)}>()", anonymousClassBlock =>
4581+
{
4582+
anonymousClassBlock.PublicMethod(
4583+
$"{GenericType.Maybe(ClassType.Void)} apply({returnValueTypeArgumentClientType} res)",
4584+
method => method.Return("Maybe.empty()"));
4585+
});
45644586
function.Line(");");
45654587
}
45664588
else
@@ -5393,7 +5415,7 @@ private static string GetRestAPIMethodArgumentList(IEnumerable<AutoRestParameter
53935415
});
53945416
return string.Join(", ", restAPIMethodArguments);
53955417
}
5396-
5418+
53975419
private static string AddClientTypePrefix(string clientType, JavaSettings settings)
53985420
=> string.IsNullOrEmpty(settings.ClientTypePrefix) ? clientType : settings.ClientTypePrefix + clientType;
53995421
}

src/JavaFileContents.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public void Import(IEnumerable<string> imports)
253253
Line();
254254
}
255255
}
256-
256+
257257
public void LineComment(string text)
258258
{
259259
LineComment(comment =>
@@ -319,11 +319,22 @@ public void ReturnAnonymousClass(string anonymousClassDeclaration, Action<JavaCl
319319
Indent(() =>
320320
{
321321
JavaClass javaClass = new JavaClass(this);
322-
anonymousClassBlock.Invoke(javaClass);
322+
anonymousClassBlock(javaClass);
323323
});
324324
Line("};");
325325
}
326326

327+
public void AnonymousClass(string anonymousClassDeclaration, Action<JavaClass> anonymousClassBlock)
328+
{
329+
Line($"{anonymousClassDeclaration} {{");
330+
Indent(() =>
331+
{
332+
JavaClass javaClass = new JavaClass(this);
333+
anonymousClassBlock(javaClass);
334+
});
335+
Line("}");
336+
}
337+
327338
public void Annotation(params string[] annotations)
328339
{
329340
Annotation((IEnumerable<string>)annotations);

src/Model/RestAPIMethod.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public void AddImportsTo(ISet<string> imports, bool includeImplementationImports
154154

155155
if (includeImplementationImports)
156156
{
157+
imports.Add("io.reactivex.functions.Function");
158+
157159
if (IsResumable)
158160
{
159161
imports.Add("com.microsoft.rest.v2.annotations.ResumeOperation");

0 commit comments

Comments
 (0)