|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.codegen.poet.model; |
| 17 | + |
| 18 | +import static java.util.stream.Collectors.toList; |
| 19 | +import static software.amazon.awssdk.codegen.internal.Constant.LF; |
| 20 | + |
| 21 | +import com.squareup.javapoet.AnnotationSpec; |
| 22 | +import com.squareup.javapoet.MethodSpec; |
| 23 | +import java.util.List; |
| 24 | +import software.amazon.awssdk.codegen.model.intermediate.MemberModel; |
| 25 | +import software.amazon.awssdk.utils.StringUtils; |
| 26 | + |
| 27 | +public final class DeprecationUtils { |
| 28 | + |
| 29 | + private static final AnnotationSpec DEPRECATED = AnnotationSpec.builder(Deprecated.class).build(); |
| 30 | + |
| 31 | + private DeprecationUtils() { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * If a given member is modeled as deprecated, add the {@link Deprecated} annotation to the method and, if the method |
| 36 | + * already has existing Javadoc, append a section with the {@code @deprecated} tag. |
| 37 | + */ |
| 38 | + public static MethodSpec checkDeprecated(MemberModel member, MethodSpec method) { |
| 39 | + if (!member.isDeprecated() || method.annotations.contains(DEPRECATED)) { |
| 40 | + return method; |
| 41 | + } |
| 42 | + MethodSpec.Builder builder = method.toBuilder().addAnnotation(DEPRECATED); |
| 43 | + if (!method.javadoc.isEmpty()) { |
| 44 | + builder.addJavadoc(LF + "@deprecated"); |
| 45 | + if (StringUtils.isNotBlank(member.getDeprecatedMessage())) { |
| 46 | + builder.addJavadoc(" $L", member.getDeprecatedMessage()); |
| 47 | + } |
| 48 | + } |
| 49 | + return builder.build(); |
| 50 | + } |
| 51 | + |
| 52 | + public static List<MethodSpec> checkDeprecated(MemberModel member, List<MethodSpec> methods) { |
| 53 | + return methods.stream().map(methodSpec -> checkDeprecated(member, methodSpec)).collect(toList()); |
| 54 | + } |
| 55 | +} |
0 commit comments