From 8cec83741a7b6298b950afc5899a3ac31d1659fb Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 29 May 2024 15:16:05 +0800 Subject: [PATCH] validateBuilder and validateClient --- .../ServiceClientBuilderTemplate.java | 34 ++++++++++++------- .../autorest/template/TemplateHelper.java | 2 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/javagen/src/main/java/com/azure/autorest/template/ServiceClientBuilderTemplate.java b/javagen/src/main/java/com/azure/autorest/template/ServiceClientBuilderTemplate.java index cd3f87cc86..4d3aa07458 100644 --- a/javagen/src/main/java/com/azure/autorest/template/ServiceClientBuilderTemplate.java +++ b/javagen/src/main/java/com/azure/autorest/template/ServiceClientBuilderTemplate.java @@ -250,17 +250,15 @@ public final void write(ClientBuilder clientBuilder, JavaFile javaFile) { JavaVisibility visibility = settings.isGenerateSyncAsyncClients() ? JavaVisibility.Private : JavaVisibility.Public; // build method - classBlock.javadocComment(comment -> - { + classBlock.javadocComment(comment -> { comment.description(String.format("Builds an instance of %1$s with the provided parameters", buildReturnType)); comment.methodReturns(String.format("an instance of %1$s", buildReturnType)); }); addGeneratedAnnotation(classBlock); - classBlock.method(visibility, null, String.format("%1$s %2$s()", buildReturnType, buildMethodName), function -> - { + classBlock.method(visibility, null, String.format("%1$s %2$s()", buildReturnType, buildMethodName), function -> { List allProperties = mergeClientPropertiesWithTraits( - clientProperties, - settings.isAzureOrFluent() ? null : clientBuilder.getBuilderTraits()); + clientProperties, + settings.isAzureOrFluent() ? null : clientBuilder.getBuilderTraits()); for (ServiceClientProperty serviceClientProperty : allProperties) { if (serviceClientProperty.getDefaultValueExpression() != null @@ -275,6 +273,10 @@ public final void write(ClientBuilder clientBuilder, JavaFile javaFile) { } } + if (!settings.isAzureOrFluent()) { + function.line("this.validateClient();"); + } + // additional service client properties in constructor arguments String constructorArgs = serviceClient.getProperties().stream() .filter(p -> !p.isReadOnly()) @@ -318,7 +320,8 @@ public final void write(ClientBuilder clientBuilder, JavaFile javaFile) { if (!settings.isAzureOrFluent()) { List allProperties = mergeClientPropertiesWithTraits(clientProperties, clientBuilder.getBuilderTraits()); - addValidateBuilderMethod(classBlock, allProperties); + addValidateClientMethod(classBlock, allProperties); + addValidatePipelineMethod(classBlock); addCreateHttpPipelineMethod(settings, classBlock, serviceClient.getDefaultCredentialScopes(), serviceClient.getSecurityInfo(), serviceClient.getPipelinePolicyDetails()); } @@ -529,14 +532,21 @@ protected void addCreateHttpPipelineMethod(JavaSettings settings, JavaClass clas }); } - private void addValidateBuilderMethod(JavaClass classBlock, List properties) { + private void addValidatePipelineMethod(JavaClass classBlock) { + addGeneratedAnnotation(classBlock); + classBlock.privateMethod("void validatePipeline()", methodBlock -> { + methodBlock.line("// This method is invoked from 'createHttpPipeline' method."); + methodBlock.line("// Developer can customize this method, to validate that the necessary conditions are met for the new HTTP pipeline."); + }); + } + + private void addValidateClientMethod(JavaClass classBlock, List properties) { addGeneratedAnnotation(classBlock); - classBlock.privateMethod("void validateBuilder()", methodBlock -> { - methodBlock.line("// This method is invoked from 'createHttpPipeline' when preparing the HTTP pipeline for the new client."); + classBlock.privateMethod("void validateClient()", methodBlock -> { + methodBlock.line("// This method is invoked from 'buildInnerClient'/'buildClient' method."); methodBlock.line("// Developer can customize this method, to validate that the necessary conditions are met for the new client."); for (ServiceClientProperty property : properties) { - // ServiceVersion is also provided with a defaultValueExpression - if (property.isRequired() && property.getDefaultValueExpression() == null) { + if (property.isRequired()) { methodBlock.line("Objects.requireNonNull(" + property.getName() + ", \"'" + property.getName() + "' cannot be null.\");"); } } diff --git a/javagen/src/main/java/com/azure/autorest/template/TemplateHelper.java b/javagen/src/main/java/com/azure/autorest/template/TemplateHelper.java index a6b7827ace..0dd4855b30 100644 --- a/javagen/src/main/java/com/azure/autorest/template/TemplateHelper.java +++ b/javagen/src/main/java/com/azure/autorest/template/TemplateHelper.java @@ -41,7 +41,7 @@ public static String getByteCloneExpression(String propertyName) { } public static void createHttpPipelineMethod(JavaSettings settings, String defaultCredentialScopes, SecurityInfo securityInfo, PipelinePolicyDetails pipelinePolicyDetails, JavaBlock function) { - function.line("this.validateBuilder();"); + function.line("this.validatePipeline();"); if (!settings.isBranded()) { createGenericHttpPipelineMethod(settings, defaultCredentialScopes, securityInfo, pipelinePolicyDetails, function); } else {