1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+
14import com .azure .autorest .customization .ClassCustomization ;
2- import com .azure .autorest .customization .ConstructorCustomization ;
35import com .azure .autorest .customization .Customization ;
6+ import com .azure .autorest .customization .Editor ;
47import com .azure .autorest .customization .LibraryCustomization ;
58import com .azure .autorest .customization .PackageCustomization ;
6- import com .github .javaparser .ast .body .ClassOrInterfaceDeclaration ;
7- import com .github .javaparser .ast .body .FieldDeclaration ;
89import com .github .javaparser .StaticJavaParser ;
910import com .github .javaparser .ast .Modifier ;
10- import com .github .javaparser .ast .NodeList ;
1111import com .github .javaparser .ast .body .Parameter ;
12- import com .github .javaparser .ast .stmt .BlockStmt ;
12+ import com .github .javaparser .ast .nodeTypes .NodeWithJavadoc ;
13+ import com .github .javaparser .ast .nodeTypes .NodeWithModifiers ;
14+ import com .github .javaparser .javadoc .Javadoc ;
1315import org .slf4j .Logger ;
14- import java .util .Arrays ;
16+
17+ import java .util .List ;
18+
19+ import static com .github .javaparser .javadoc .description .JavadocDescription .parseText ;
1520
1621/**
1722 * This class contains the customization code to customize the AutoRest generated code for OpenAI.
1823 */
1924public class AppConfigCustomizations extends Customization {
25+ private static final String ROOT_FILE_PATH = "src/main/java/com/azure/v2/data/appconfiguration/" ;
2026
2127 @ Override
2228 public void customize (LibraryCustomization customization , Logger logger ) {
2329 PackageCustomization models = customization .getPackage ("com.azure.v2.data.appconfiguration.models" );
2430 PackageCustomization appConfigPackages = customization .getPackage ("com.azure.v2.data.appconfiguration" );
2531 hideClient (appConfigPackages );
2632 hideModels (models );
27- renameServiceVersionClassName (appConfigPackages );
33+ renameServiceVersionClassName (customization . getRawEditor () );
2834
2935// customizeKeyValueFilter(models.getClass("ConfigurationSettingsFilter"));
3036// customizeKeyValueFields(models.getClass("SettingFields"));
@@ -33,101 +39,98 @@ public void customize(LibraryCustomization customization, Logger logger) {
3339
3440 private void hideModels (PackageCustomization models ) {
3541 // TODO: move both 'Key' and 'KeyValue' class to implementation package
36- models
37- .getClass ("Key" )
38- .setModifier (0 );
39- models
40- .getClass ("KeyValue" )
41- .setModifier (0 );
42+ makeClassPackagePrivate (models , "Key" );
43+ makeClassPackagePrivate (models , "KeyValue" );
4244 }
4345
4446 private void hideClient (PackageCustomization appConfigPackages ) {
45- // TODO: move both 'AzureAppConfigurationClient' and 'AzureAppConfigurationClientBuilder' class to implementation package
46- appConfigPackages
47- .getClass ("AzureAppConfigurationClient" )
48- .setModifier (0 );
49- appConfigPackages
50- .getClass ("AzureAppConfigurationClientBuilder" )
51- .setModifier (0 );
47+ // TODO: move both 'AzureAppConfigurationClient' and 'AzureAppConfigurationClientBuilder' class to
48+ // implementation package
49+ makeClassPackagePrivate (appConfigPackages , "AzureAppConfigurationClient" );
50+ makeClassPackagePrivate (appConfigPackages , "AzureAppConfigurationClientBuilder" );
5251 }
5352
54- private void renameServiceVersionClassName (PackageCustomization appConfigPackages ) {
55- appConfigPackages
56- .getClass ("AzureAppConfigurationServiceVersion" )
57- .rename ("ConfigurationServiceVersion" );
53+ private static void makeClassPackagePrivate (PackageCustomization customization , String className ) {
54+ customization .getClass (className ).customizeAst (ast -> ast .getClassByName (className )
55+ .ifPresent (NodeWithModifiers ::setModifiers ));
56+ }
57+
58+ private void renameServiceVersionClassName (Editor editor ) {
59+ String serviceVersion = editor .getFileContent (ROOT_FILE_PATH + "AzureAppConfigurationServiceVersion.java" )
60+ .replace ("AzureAppConfigurationServiceVersion" , "ConfigurationServiceVersion" );
61+
62+ editor .addFile (ROOT_FILE_PATH + "ConfigurationServiceVersion.java" , serviceVersion );
63+ editor .removeFile (ROOT_FILE_PATH + "AzureAppConfigurationServiceVersion.java" );
64+
65+ for (String path : List .of ("AzureAppConfigurationClientBuilder" , "implementation/AzureAppConfigurationClientImpl" )) {
66+ String fileName = ROOT_FILE_PATH + path + ".java" ;
67+ String fileContent = editor .getFileContent (fileName );
68+ fileContent = fileContent .replace ("AzureAppConfigurationServiceVersion" , "ConfigurationServiceVersion" );
69+ editor .replaceFile (fileName , fileContent );
70+ }
5871 }
5972
6073 // TODO: LRO is not support yet in codegen-v2, wait for codegen-v2 to support LRO
6174 // KeyValueFilter is used in LRO and no other place use it, so it is not generate setters.
62- private void customizeKeyValueFilter (ClassCustomization classCustomization ) {
63- // Edit javadoc of `setLabel` method
64- classCustomization .getMethod ("setLabel" )
65- .getJavadoc ()
66- .setDescription ("Set the label property: Filters {@link ConfigurationSetting} by their label field." );
67- // Edit javadoc of `getKey` method
68- classCustomization .getMethod ("getKey" )
69- .getJavadoc ()
70- .setDescription ("Get the key property: Filters {@link ConfigurationSetting} by their key field." );
71- // Edit javadoc of `getLabel` method
72- classCustomization .getMethod ("getLabel" )
73- .getJavadoc ()
74- .setDescription ("Get the label property: Filters {@link ConfigurationSetting} by their label field." );
75+ private void customizeKeyValueFilter (ClassCustomization customization ) {
76+ customization .customizeAst (ast -> ast .getClassByName (customization .getClassName ()).ifPresent (clazz -> {
77+ // Edit javadoc of `setLabel` method
78+ clazz .getMethodsByName ("setLabel" ).forEach (method -> replaceDescription (method ,
79+ "Set the label property: Filters {@link ConfigurationSetting} by their label field." ));
80+ // Edit javadoc of `getKey` method
81+ clazz .getMethodsByName ("getKey" ).forEach (method -> replaceDescription (method ,
82+ "Get the key property: Filters {@link ConfigurationSetting} by their key field." ));
83+ // Edit javadoc of `getLabel` method
84+ clazz .getMethodsByName ("getLabel" ).forEach (method -> replaceDescription (method ,
85+ "Get the label property: Filters {@link ConfigurationSetting} by their label field." ));
86+ }));
7587 }
7688
77- private void customizeSnapshot (ClassCustomization classCustomization ) {
78- classCustomization .customizeAst (ast -> {
79- ast .addImport ("java.time.Duration" );
89+ private static void replaceDescription (NodeWithJavadoc <?> node , String newDescription ) {
90+ node .getJavadoc ().ifPresent (javadoc -> {
91+ javadoc .getDescription ().getElements ().clear ();
92+ javadoc .getDescription ().getElements ().addAll (parseText (newDescription ).getElements ());
93+ node .setJavadocComment (javadoc );
94+ });
95+ }
8096
81- ast .getClassByName (classCustomization .getClassName ()).ifPresent (clazz -> {
97+ private void customizeSnapshot (ClassCustomization customization ) {
98+ customization .customizeAst (ast -> ast .addImport ("java.time.Duration" )
99+ .getClassByName (customization .getClassName ()).ifPresent (clazz -> {
82100 // Transfer Long to Duration internally
83- clazz .getMethodsByName ("getRetentionPeriod" ).get (0 )
84- .setType ("Duration" )
85- .setBody (StaticJavaParser .parseBlock ("{ return this.retentionPeriod == null ? null : Duration.ofSeconds(this.retentionPeriod); }" ));
101+ clazz .getMethodsByName ("getRetentionPeriod" ).forEach (method -> method .setType ("Duration" )
102+ .setBody (StaticJavaParser .parseBlock ("{ return this.retentionPeriod == null ? null : Duration.ofSeconds(this.retentionPeriod); }" )));
86103
87- clazz .getMethodsByName ("setRetentionPeriod" ).get ( 0 )
104+ clazz .getMethodsByName ("setRetentionPeriod" ).forEach ( method -> method
88105 .setParameter (0 , new Parameter ().setType ("Duration" ).setName ("retentionPeriod" ))
89- .setBody (StaticJavaParser .parseBlock ("{ this.retentionPeriod = retentionPeriod == null ? null : retentionPeriod.getSeconds(); return this; }" ));
90- });
91- });
92- }
93-
94- private void customizeKeyValueFields (ClassCustomization classCustomization ) {
95- classCustomization .customizeAst (ast -> {
96- // Add imports required by class changes.
97- ast .addImport ("java.util.Locale" );
98-
99- ClassOrInterfaceDeclaration clazz = ast .getClassByName (classCustomization .getClassName ()).get ();
100-
101- // Modify fromString() method
102-
103- clazz .getMethodsByName ("fromString" ).get (0 )
104- .setJavadocComment (StaticJavaParser .parseJavadoc (joinWithNewline (
105- "Creates or finds a {@link SettingFields} from its string representation." ,
106- "@param name a name to look for." ,
107- "@return the corresponding {@link SettingFields}"
108- )));
109-
110- // Add class-level javadoc
111- clazz .setJavadocComment (StaticJavaParser .parseJavadoc (joinWithNewline (
112- "Fields in {@link ConfigurationSetting} that can be returned from GET queries." ,
113- "@see SettingSelector"
114- )));
115-
116- // Add toStringMapper static new method to SettingFields
117- clazz .addMethod ("toStringMapper" , Modifier .Keyword .PUBLIC , Modifier .Keyword .STATIC ).setType ("String" )
118- .addParameter ("SettingFields" , "field" )
119- .setBody (new BlockStmt (new NodeList <>(StaticJavaParser .parseStatement ("return field.toString().toLowerCase(Locale.US);" ))))
120- .addAnnotation (Deprecated .class )
121- .setJavadocComment (StaticJavaParser .parseJavadoc (joinWithNewline (
122- " * Converts the SettingFields to a string that is usable for HTTP requests and logging." ,
123- " * @param field SettingFields to map." ,
124- " * @return SettingFields as a lowercase string in the US locale." ,
125- " * @deprecated This method is no longer needed. SettingFields is using lower case enum value for the HTTP requests."
126- )));
127- });
106+ .setBody (StaticJavaParser .parseBlock ("{ this.retentionPeriod = retentionPeriod == null ? null : retentionPeriod.getSeconds(); return this; }" )));
107+ }));
128108 }
129109
130- private static String joinWithNewline (String ... lines ) {
131- return String .join ("\n " , lines );
110+ private void customizeKeyValueFields (ClassCustomization customization ) {
111+ customization .customizeAst (ast -> ast .addImport ("java.util.Locale" )
112+ .getClassByName (customization .getClassName ()).ifPresent (clazz -> {
113+ // Modify fromString() method
114+ clazz .getMethodsByName ("fromString" ).forEach (method -> method .setJavadocComment (new Javadoc (
115+ parseText ("Creates or finds a {@link SettingFields} from its string representation." ))
116+ .addBlockTag ("param" , "name" , "a name to look for." )
117+ .addBlockTag ("return" , "the corresponding {@link SettingFields}" )));
118+
119+ // Add class-level javadoc
120+ clazz .setJavadocComment (new Javadoc (
121+ parseText ("Fields in {@link ConfigurationSetting} that can be returned from GET queries." ))
122+ .addBlockTag ("see" , "SettingSelector" ));
123+
124+ // Add toStringMapper static new method to SettingFields
125+ clazz .addMethod ("toStringMapper" , Modifier .Keyword .PUBLIC , Modifier .Keyword .STATIC ).setType ("String" )
126+ .addParameter ("SettingFields" , "field" )
127+ .setBody (StaticJavaParser .parseBlock ("{ return field.toString().toLowerCase(Locale.US); }" ))
128+ .addMarkerAnnotation (Deprecated .class )
129+ .setJavadocComment (new Javadoc (parseText (
130+ "Converts the SettingFields to a string that is usable for HTTP requests and logging." ))
131+ .addBlockTag ("param" , "field" , "SettingFields to map." )
132+ .addBlockTag ("return" , "SettingFields as a lowercase string in the US locale." )
133+ .addBlockTag ("deprecated" , "This method is no longer needed. SettingFields is using lower case enum value for the HTTP requests." ));
134+ }));
132135 }
133136}
0 commit comments