diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClientAccessorMethod.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClientAccessorMethod.java index 2efeb6d7c6..f41b860768 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClientAccessorMethod.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClientAccessorMethod.java @@ -21,7 +21,7 @@ public class ClientAccessorMethod { private ServiceClient serviceClient; /** - * Initializes the ClientAccessorMethod + * Initializes the ClientAccessorMethod. * * @param subClient the ServiceClient of the sub client */ @@ -30,7 +30,7 @@ public ClientAccessorMethod(ServiceClient subClient) { } /** - * Sets the ServiceClient + * Sets the ServiceClient containing this accessor method. * * @param serviceClient the ServiceClient containing this accessor method */ @@ -38,6 +38,24 @@ public void setServiceClient(ServiceClient serviceClient) { this.serviceClient = serviceClient; } + /** + * Gets the ServiceClient containing this accessor method. + * + * @return the ServiceClient containing this accessor method + */ + public ServiceClient getServiceClient() { + return serviceClient; + } + + /** + * Gets the sub ServiceClient. + * + * @return the sub ServiceClient + */ + public ServiceClient getSubClient() { + return subClient; + } + public List getAccessorProperties() { List additionalProperties = new ArrayList<>(); for (ServiceClientProperty property : subClient.getProperties()) { @@ -67,8 +85,17 @@ public void addImportsTo(Set imports, boolean includeImplementationImpor } } + public String getName() { + return "get" + subClient.getClientBaseName(); + } + public String getDeclaration() { - return null; + String subClientClassName = subClient.getClassName(); + List additionalProperties = this.getAccessorProperties(); + + return subClientClassName + " " + getName() + "(" + + additionalProperties.stream().map(p -> p.getType() + " " + p.getName()).collect(Collectors.joining(", ")) + + ")"; } public String getAsyncSyncClientName(boolean isAsync) { diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceAsyncClientTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceAsyncClientTemplate.java index 011a47fb99..9b159c5a13 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceAsyncClientTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceAsyncClientTemplate.java @@ -221,16 +221,20 @@ static void writeSubClientAccessors(ServiceClient serviceClient, JavaClass class List accessorProperties = clientAccessorMethod.getAccessorProperties(); String subClientClassName = clientAccessorMethod.getAsyncSyncClientName(isAsync); + String serviceClientMethodCall = "serviceClient." + clientAccessorMethod.getName() + "(" + + accessorProperties.stream().map(ServiceClientProperty::getName).collect(Collectors.joining(", ")) + + ")"; + // expect all properties are required, so no overload classBlock.javadocComment(comment -> { - comment.description("Gets the " + subClientClassName + "."); + comment.description("Gets an instance of " + subClientClassName + " class."); for (ServiceClientProperty property : accessorProperties) { comment.param(property.getName(), property.getDescription()); } - comment.methodReturns("the " + subClientClassName); + comment.methodReturns("an instance of " + subClientClassName + "class"); }); classBlock.publicMethod(clientAccessorMethod.getAsyncSyncClientDeclaration(isAsync), method -> { - method.methodReturn("null"); + method.methodReturn("new " + subClientClassName + "(" + serviceClientMethodCall + ")"); }); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java index aef45f4e41..11fa8822b5 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java @@ -6,6 +6,7 @@ import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Annotation; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientAccessorMethod; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodParameter; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Constructor; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient; @@ -319,6 +320,8 @@ public final void write(ServiceClient serviceClient, JavaFile javaFile) { this.writeAdditionalClassBlock(classBlock); + writeClientAccessorMethods(classBlock, serviceClient.getClientAccessorMethods()); + if (settings.isUseClientLogger()) { TemplateUtil.addClientLogger(classBlock, serviceClient.getClassName(), javaFile.getContents()); } @@ -367,4 +370,43 @@ protected void addServiceClientAnnotationImport(Set imports) { */ protected void writeAdditionalClassBlock(JavaClass classBlock) { } + + private static void writeClientAccessorMethods(JavaClass classBlock, + List clientAccessorMethods) { + for (ClientAccessorMethod clientAccessorMethod : clientAccessorMethods) { + final String subClientName = clientAccessorMethod.getSubClient().getClassName(); + final List arguments = new ArrayList<>(); + + // pre-defined properties like "httpPipeline" + List parentConstructors = clientAccessorMethod.getServiceClient().getConstructors(); + // take the last, which is the maximum overload + Constructor parentConstructor + = clientAccessorMethod.getServiceClient().getConstructors().get(parentConstructors.size() - 1); + for (ClientMethodParameter parameter : parentConstructor.getParameters()) { + arguments.add("this." + parameter.getName()); + } + // other properties from parent client + for (ServiceClientProperty property : clientAccessorMethod.getServiceClient().getProperties()) { + if (!property.isReadOnly()) { + arguments.add("this." + property.getName()); + } + } + // properties from method + for (ServiceClientProperty property : clientAccessorMethod.getAccessorProperties()) { + arguments.add(property.getName()); + } + + classBlock.javadocComment(comment -> { + comment.description("Gets an instance of " + subClientName + " class."); + for (ServiceClientProperty property : clientAccessorMethod.getAccessorProperties()) { + comment.param(property.getName(), property.getDescription()); + } + comment.methodReturns("an instance of " + subClientName + "class"); + }); + classBlock.publicMethod(clientAccessorMethod.getDeclaration(), method -> { + method.methodReturn( + "new " + subClientName + "(" + arguments.stream().collect(Collectors.joining(", ")) + ")"); + }); + } + } }