Skip to content

Commit acda137

Browse files
authored
Adding @deprecated annotation when API is marked as deprecated (#2991)
1 parent 4fdfcb2 commit acda137

File tree

10 files changed

+67
-5
lines changed

10 files changed

+67
-5
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/AddOperations.java

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public Map<String, OperationModel> constructOperations() {
155155

156156
operationModel.setOperationName(operationName);
157157
operationModel.setDeprecated(op.isDeprecated());
158+
operationModel.setDeprecatedMessage(op.getDeprecatedMessage());
158159
operationModel.setDocumentation(op.getDocumentation());
159160
operationModel.setIsAuthenticated(isAuthenticated(op));
160161
operationModel.setAuthType(op.getAuthtype());

codegen/src/main/java/software/amazon/awssdk/codegen/model/intermediate/OperationModel.java

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class OperationModel extends DocumentationModel {
3232

3333
private boolean deprecated;
3434

35+
private String deprecatedMessage;
36+
3537
private VariableModel input;
3638

3739
private ReturnTypeModel returnType;
@@ -84,6 +86,14 @@ public void setDeprecated(boolean deprecated) {
8486
this.deprecated = deprecated;
8587
}
8688

89+
public String getDeprecatedMessage() {
90+
return deprecatedMessage;
91+
}
92+
93+
public void setDeprecatedMessage(String deprecatedMessage) {
94+
this.deprecatedMessage = deprecatedMessage;
95+
}
96+
8797
public String getDocs(IntermediateModel model,
8898
ClientType clientType) {
8999
return OperationDocs.getDocs(model, this, clientType);

codegen/src/main/java/software/amazon/awssdk/codegen/model/service/Operation.java

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Operation {
2424

2525
private boolean deprecated;
2626

27+
private String deprecatedMessage;
28+
2729
private Http http;
2830

2931
private Input input;
@@ -67,6 +69,14 @@ public void setDeprecated(boolean deprecated) {
6769
this.deprecated = deprecated;
6870
}
6971

72+
public String getDeprecatedMessage() {
73+
return deprecatedMessage;
74+
}
75+
76+
public void setDeprecatedMessage(String deprecatedMessage) {
77+
this.deprecatedMessage = deprecatedMessage;
78+
}
79+
7080
public Http getHttp() {
7181
return http;
7282
}

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/AsyncClientInterface.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import software.amazon.awssdk.codegen.poet.PoetExtensions;
4747
import software.amazon.awssdk.codegen.poet.PoetUtils;
4848
import software.amazon.awssdk.codegen.poet.eventstream.EventStreamUtils;
49+
import software.amazon.awssdk.codegen.poet.model.DeprecationUtils;
4950
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
5051
import software.amazon.awssdk.core.SdkClient;
5152
import software.amazon.awssdk.core.async.AsyncRequestBody;
@@ -179,7 +180,9 @@ private Stream<MethodSpec> operationsAndSimpleMethods(OperationModel operationMo
179180
methods.addAll(traditionalMethods(operationModel));
180181
methods.addAll(overloadMethods(operationModel));
181182
methods.addAll(paginatedMethods(operationModel));
182-
return methods.stream();
183+
return methods.stream()
184+
// Add Deprecated annotation if needed to all overloads
185+
.map(m -> DeprecationUtils.checkDeprecated(operationModel, m));
183186
}
184187

185188
private List<MethodSpec> paginatedMethods(OperationModel opModel) {
@@ -406,7 +409,7 @@ private MethodSpec.Builder methodSignatureWithReturnType(OperationModel opModel)
406409
*/
407410
private MethodSpec.Builder interfaceMethodSignature(OperationModel opModel) {
408411
return methodSignatureWithReturnType(opModel)
409-
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT);
412+
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT);
410413
}
411414

412415
/**

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/ClientClassUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static MethodSpec consumerBuilderVariant(MethodSpec spec, String javadoc) {
6464
MethodSpec.Builder result = MethodSpec.methodBuilder(spec.name)
6565
.returns(spec.returnType)
6666
.addExceptions(spec.exceptions)
67+
.addAnnotations(spec.annotations)
6768
.addJavadoc(javadoc)
6869
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
6970
.addTypeVariables(spec.typeVariables)

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/SyncClientInterface.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import software.amazon.awssdk.codegen.poet.ClassSpec;
4949
import software.amazon.awssdk.codegen.poet.PoetExtensions;
5050
import software.amazon.awssdk.codegen.poet.PoetUtils;
51+
import software.amazon.awssdk.codegen.poet.model.DeprecationUtils;
5152
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
5253
import software.amazon.awssdk.core.ResponseBytes;
5354
import software.amazon.awssdk.core.ResponseInputStream;
@@ -176,7 +177,10 @@ private List<MethodSpec> operationMethodSpec(OperationModel opModel) {
176177
methods.addAll(streamingSimpleMethods(opModel));
177178
methods.addAll(paginatedMethods(opModel));
178179

179-
return methods;
180+
return methods.stream()
181+
// Add Deprecated annotation if needed to all overloads
182+
.map(m -> DeprecationUtils.checkDeprecated(opModel, m))
183+
.collect(toList());
180184
}
181185

182186
private MethodSpec simpleMethod(OperationModel opModel) {

codegen/src/main/java/software/amazon/awssdk/codegen/poet/model/DeprecationUtils.java

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.squareup.javapoet.MethodSpec;
2323
import java.util.List;
2424
import software.amazon.awssdk.codegen.model.intermediate.MemberModel;
25+
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
2526
import software.amazon.awssdk.utils.StringUtils;
2627

2728
public final class DeprecationUtils {
@@ -49,6 +50,24 @@ public static MethodSpec checkDeprecated(MemberModel member, MethodSpec method)
4950
return builder.build();
5051
}
5152

53+
/**
54+
* If a given operation is modeled as deprecated, add the {@link Deprecated} annotation to the method and, if the method
55+
* already has existing Javadoc, append a section with the {@code @deprecated} tag.
56+
*/
57+
public static MethodSpec checkDeprecated(OperationModel operation, MethodSpec method) {
58+
if (!operation.isDeprecated() || method.annotations.contains(DEPRECATED)) {
59+
return method;
60+
}
61+
MethodSpec.Builder builder = method.toBuilder().addAnnotation(DEPRECATED);
62+
if (!method.javadoc.isEmpty()) {
63+
builder.addJavadoc(LF + "@deprecated");
64+
if (StringUtils.isNotBlank(operation.getDeprecatedMessage())) {
65+
builder.addJavadoc(" $L", operation.getDeprecatedMessage());
66+
}
67+
}
68+
return builder.build();
69+
}
70+
5271
public static List<MethodSpec> checkDeprecated(MemberModel member, List<MethodSpec> methods) {
5372
return methods.stream().map(methodSpec -> checkDeprecated(member, methodSpec)).collect(toList());
5473
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/rest-json/service-2.json

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"shape": "InvalidInputException"
4040
}
4141
],
42+
"deprecated": true,
43+
"deprecatedMessage": "This API is deprecated, use something else",
4244
"documentation": "<p>Performs a post operation to the query service and has no output</p>"
4345
},
4446
"GetWithoutRequiredMembers": {

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-async-client-interface.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ static JsonAsyncClientBuilder builder() {
9595
* @sample JsonAsyncClient.APostOperation
9696
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/APostOperation" target="_top">AWS
9797
* API Documentation</a>
98+
*
99+
* @deprecated This API is deprecated, use something else
98100
*/
101+
@Deprecated
99102
default CompletableFuture<APostOperationResponse> aPostOperation(APostOperationRequest aPostOperationRequest) {
100103
throw new UnsupportedOperationException();
101104
}
@@ -128,7 +131,10 @@ default CompletableFuture<APostOperationResponse> aPostOperation(APostOperationR
128131
* @sample JsonAsyncClient.APostOperation
129132
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/APostOperation" target="_top">AWS
130133
* API Documentation</a>
134+
*
135+
* @deprecated This API is deprecated, use something else
131136
*/
137+
@Deprecated
132138
default CompletableFuture<APostOperationResponse> aPostOperation(Consumer<APostOperationRequest.Builder> aPostOperationRequest) {
133139
return aPostOperation(APostOperationRequest.builder().applyMutation(aPostOperationRequest).build());
134140
}
@@ -1449,4 +1455,4 @@ default CompletableFuture<StreamingOutputOperationResponse> streamingOutputOpera
14491455
default JsonUtilities utilities() {
14501456
throw new UnsupportedOperationException();
14511457
}
1452-
}
1458+
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-client-interface.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ static JsonClientBuilder builder() {
8888
* @sample JsonClient.APostOperation
8989
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/APostOperation" target="_top">AWS
9090
* API Documentation</a>
91+
*
92+
* @deprecated This API is deprecated, use something else
9193
*/
94+
@Deprecated
9295
default APostOperationResponse aPostOperation(APostOperationRequest aPostOperationRequest) throws InvalidInputException,
9396
AwsServiceException, SdkClientException, JsonException {
9497
throw new UnsupportedOperationException();
@@ -119,7 +122,10 @@ default APostOperationResponse aPostOperation(APostOperationRequest aPostOperati
119122
* @sample JsonClient.APostOperation
120123
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/APostOperation" target="_top">AWS
121124
* API Documentation</a>
125+
*
126+
* @deprecated This API is deprecated, use something else
122127
*/
128+
@Deprecated
123129
default APostOperationResponse aPostOperation(Consumer<APostOperationRequest.Builder> aPostOperationRequest)
124130
throws InvalidInputException, AwsServiceException, SdkClientException, JsonException {
125131
return aPostOperation(APostOperationRequest.builder().applyMutation(aPostOperationRequest).build());
@@ -1377,4 +1383,4 @@ static ServiceMetadata serviceMetadata() {
13771383
default JsonUtilities utilities() {
13781384
throw new UnsupportedOperationException();
13791385
}
1380-
}
1386+
}

0 commit comments

Comments
 (0)