Skip to content

Commit cdb4766

Browse files
authored
add customization doc (#3217)
1 parent b9ec00a commit cdb4766

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

customization-base/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ customizations are supported:
2222
- [Change class modifier](#change-class-modifier)
2323
- [Change method modifier](#change-method-modifier)
2424
- [Change method return type](#change-method-return-type)
25+
- [Change class super type](#change-class-super-type)
2526
- [Add an annotation to a class](#add-an-annotation-to-a-class)
2627
- [Add an annotation to a method](#add-an-annotation-to-a-method)
28+
- [Add a field default value](#add-a-field-default-value)
2729
- [Remove an annotation from a class](#remove-an-annotation-from-a-class)
2830
- [Refactor: Generate the getter and setter methods for a property](#refactor-generate-the-getter-and-setter-methods-for-a-property)
2931
- [Refactor: Rename an enum member name](#refactor-rename-an-enum-member-name)
@@ -149,6 +151,36 @@ public class Foo {
149151

150152
The `UUID` class will be automatically imported.
151153

154+
## Change class super type
155+
156+
A class `Foo` extends `Bar`
157+
```java readme-sample-change-class-base-type-initial
158+
public class Bar {
159+
}
160+
public class Foo extends Bar {
161+
}
162+
```
163+
164+
with customization
165+
```java readme-sample-change-class-base-type-customization
166+
@Override
167+
public void customize(LibraryCustomization customization, Logger logger) {
168+
customization.getClass("com.azure.myservice.models", "Foo")
169+
.customizeAst(ast -> ast.getClassByName("foo").ifPresent(clazz -> {
170+
String newTypeFullName = "com.azure.myservice.models.Bar1";
171+
ast.addImport(newTypeFullName);
172+
clazz.getExtendedTypes().clear();
173+
clazz.addExtendedType(new ClassOrInterfaceType(null, "Bar1"));
174+
}));
175+
}
176+
```
177+
178+
will generate
179+
```java readme-sample-change-class-base-type-result
180+
public class Foo extends Bar1 {
181+
}
182+
```
183+
152184
## Add an annotation to a class
153185

154186
A class `Foo`
@@ -218,6 +250,40 @@ public class Foo {
218250

219251
The `Deprecated` class will be automatically imported.
220252

253+
## Add a field default value
254+
255+
A class `Foo`
256+
257+
```java readme-sample-add-a-field-default-value-initial
258+
public class Foo {
259+
private String bar;
260+
}
261+
```
262+
263+
with customization
264+
265+
```java readme-sample-add-a-field-default-value-customization
266+
@Override
267+
public void customize(LibraryCustomization customization, Logger logger) {
268+
customization.getClass("com.azure.myservice.models", "Foo")
269+
.customizeAst(ast -> ast.getClassByName("Foo")
270+
.flatMap(clazz -> clazz.getFieldByName("bar"))
271+
.ifPresent(barField ->
272+
barField.getVariables().forEach(var -> {
273+
if (var.getNameAsString().equals("bar")) {
274+
var.setInitializer("\"bar\"");
275+
}
276+
})));
277+
}
278+
```
279+
280+
will generate
281+
```java readme-sample-add-a-field-default-value-result
282+
public class Foo {
283+
private String bar = "bar";
284+
}
285+
```
286+
221287
## Remove an annotation from a class
222288

223289
A class `Foo`

customization-base/src/samples/java/com/azure/autorest/customization/ReadmeSamples.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.github.javaparser.ast.Modifier;
88
import com.github.javaparser.ast.Node;
99
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
10+
import com.github.javaparser.ast.type.ClassOrInterfaceType;
1011
import org.slf4j.Logger;
1112

1213
import java.util.UUID;
@@ -116,6 +117,39 @@ public UUID getId() {
116117
}
117118
}
118119

120+
static class ChangeClassBaseType extends Customization {
121+
static class Initial {
122+
// BEGIN: readme-sample-change-class-base-type-initial
123+
public class Bar {
124+
}
125+
public class Foo extends Bar {
126+
}
127+
// END: readme-sample-change-class-base-type-initial
128+
}
129+
130+
// BEGIN: readme-sample-change-class-base-type-customization
131+
@Override
132+
public void customize(LibraryCustomization customization, Logger logger) {
133+
customization.getClass("com.azure.myservice.models", "Foo")
134+
.customizeAst(ast -> ast.getClassByName("foo").ifPresent(clazz -> {
135+
String newTypeFullName = "com.azure.myservice.models.Bar1";
136+
ast.addImport(newTypeFullName);
137+
clazz.getExtendedTypes().clear();
138+
clazz.addExtendedType(new ClassOrInterfaceType(null, "Bar1"));
139+
}));
140+
}
141+
// END: readme-sample-change-class-base-type-customization
142+
143+
static class Result {
144+
public class Bar1 {
145+
}
146+
// BEGIN: readme-sample-change-class-base-type-result
147+
public class Foo extends Bar1 {
148+
}
149+
// END: readme-sample-change-class-base-type-result
150+
}
151+
}
152+
119153
static class AddClassAnnotation extends Customization {
120154
static class Initial {
121155
// BEGIN: readme-sample-add-class-annotation-initial
@@ -177,6 +211,39 @@ public Bar getBar() {
177211
}
178212
}
179213

214+
static class AddFieldDefaultValue extends Customization {
215+
static class Initial {
216+
// BEGIN: readme-sample-add-a-field-default-value-initial
217+
public class Foo {
218+
private String bar;
219+
}
220+
// END: readme-sample-add-a-field-default-value-initial
221+
}
222+
223+
// BEGIN: readme-sample-add-a-field-default-value-customization
224+
@Override
225+
public void customize(LibraryCustomization customization, Logger logger) {
226+
customization.getClass("com.azure.myservice.models", "Foo")
227+
.customizeAst(ast -> ast.getClassByName("Foo")
228+
.flatMap(clazz -> clazz.getFieldByName("bar"))
229+
.ifPresent(barField ->
230+
barField.getVariables().forEach(var -> {
231+
if (var.getNameAsString().equals("bar")) {
232+
var.setInitializer("\"bar\"");
233+
}
234+
})));
235+
}
236+
// END: readme-sample-add-a-field-default-value-customization
237+
238+
static class Result {
239+
// BEGIN: readme-sample-add-a-field-default-value-result
240+
public class Foo {
241+
private String bar = "bar";
242+
}
243+
// END: readme-sample-add-a-field-default-value-result
244+
}
245+
}
246+
180247
static class RemoveClassAnnotation extends Customization {
181248
static class Initial {
182249
// BEGIN: readme-sample-remove-class-annotation-initial

0 commit comments

Comments
 (0)