diff --git a/.changes/next-release/feature-AWSSDKforJavav2-382b343.json b/.changes/next-release/feature-AWSSDKforJavav2-382b343.json new file mode 100644 index 000000000000..dbcb0e9dc42f --- /dev/null +++ b/.changes/next-release/feature-AWSSDKforJavav2-382b343.json @@ -0,0 +1,6 @@ +{ + "type": "feature", + "category": "AWS SDK for Java v2", + "contributor": "", + "description": "Adding constant to each SDK module to represent its version" +} diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/emitters/tasks/CommonInternalGeneratorTasks.java b/codegen/src/main/java/software/amazon/awssdk/codegen/emitters/tasks/CommonInternalGeneratorTasks.java index 1bafba5eed5d..c02571f181c0 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/emitters/tasks/CommonInternalGeneratorTasks.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/emitters/tasks/CommonInternalGeneratorTasks.java @@ -22,6 +22,7 @@ import software.amazon.awssdk.codegen.emitters.PoetGeneratorTask; import software.amazon.awssdk.codegen.poet.client.EnvironmentTokenSystemSettingsClass; import software.amazon.awssdk.codegen.poet.client.SdkClientOptions; +import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec; import software.amazon.awssdk.codegen.poet.common.UserAgentUtilsSpec; public class CommonInternalGeneratorTasks extends BaseGeneratorTasks { @@ -40,6 +41,7 @@ protected List createTasks() throws Exception { if (params.getModel().getCustomizationConfig().isEnableEnvironmentBearerToken()) { tasks.add(createEnvironmentTokenSystemSettingTask()); } + tasks.add(createServiceVersionInfoTask()); return tasks; } @@ -58,6 +60,11 @@ private GeneratorTask createEnvironmentTokenSystemSettingTask() { new EnvironmentTokenSystemSettingsClass(params.getModel())); } + private GeneratorTask createServiceVersionInfoTask() { + return new PoetGeneratorTask(clientOptionsDir(), params.getModel().getFileHeader(), + new ServiceVersionInfoSpec(params.getModel())); + } + private String clientOptionsDir() { return params.getPathProvider().getClientInternalDirectory(); } diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/PoetExtension.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/PoetExtension.java index 4112188c6451..f119507809c2 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/PoetExtension.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/PoetExtension.java @@ -79,6 +79,10 @@ public ClassName getUserAgentClass() { return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "UserAgentUtils"); } + public ClassName getServiceVersionInfoClass() { + return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "ServiceVersionInfo"); + } + public ClassName getEnvironmentTokenSystemSettingsClass() { return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "EnvironmentTokenSystemSettings"); } diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java new file mode 100644 index 000000000000..00963197bdee --- /dev/null +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java @@ -0,0 +1,63 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.codegen.poet.client.specs; + +import static software.amazon.awssdk.core.util.VersionInfo.SDK_VERSION; + +import com.squareup.javapoet.ClassName; +import com.squareup.javapoet.FieldSpec; +import com.squareup.javapoet.MethodSpec; +import com.squareup.javapoet.TypeSpec; +import javax.lang.model.element.Modifier; +import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; +import software.amazon.awssdk.codegen.poet.ClassSpec; +import software.amazon.awssdk.codegen.poet.PoetExtension; +import software.amazon.awssdk.codegen.poet.PoetUtils; + +public class ServiceVersionInfoSpec implements ClassSpec { + private final PoetExtension poetExtension; + + public ServiceVersionInfoSpec(IntermediateModel model) { + this.poetExtension = new PoetExtension(model); + } + + @Override + public TypeSpec poetSpec() { + TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionInfo") + .addModifiers(Modifier.PUBLIC, Modifier.FINAL) + .addAnnotation(PoetUtils.generatedAnnotation()) + .addField(FieldSpec.builder( + String.class, "VERSION", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) + .initializer("$S", SDK_VERSION) + .addJavadoc("Returns the current version for the AWS SDK in which" + + " this class is running.") + .build()) + .addMethod(privateConstructor()); + + return builder.build(); + } + + protected MethodSpec privateConstructor() { + return MethodSpec.constructorBuilder() + .addModifiers(Modifier.PRIVATE) + .build(); + } + + @Override + public ClassName className() { + return poetExtension.getServiceVersionInfoClass(); + } +} diff --git a/codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java b/codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java new file mode 100644 index 000000000000..496a35914030 --- /dev/null +++ b/codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java @@ -0,0 +1,59 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.codegen.poet.client; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace; + +import com.squareup.javapoet.JavaFile; +import com.squareup.javapoet.TypeSpec; +import java.io.InputStream; +import java.util.Scanner; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.codegen.poet.ClassSpec; +import software.amazon.awssdk.codegen.poet.ClientTestModels; +import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec; +import software.amazon.awssdk.core.util.VersionInfo; + +public class ServiceVersionInfoSpecTest { + + // Fixture test that compares generated ServiceVersionInfo class against expected output. + // The fixture file uses {{VERSION}} as a placeholder which gets replaced with the current + // SDK version at test time, since the generated code injects the actual version at build time. + @Test + void testServiceVersionInfoClass() { + String currVersion = VersionInfo.SDK_VERSION; + ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels()); + + String expectedContent = loadFixtureFile("test-service-version-info-class.java"); + expectedContent = expectedContent.replace("{{VERSION}}", currVersion); + + String actualContent = generateContent(serviceVersionInfoSpec); + + assertThat(actualContent).isEqualToIgnoringWhitespace(expectedContent); + } + + private String loadFixtureFile(String filename) { + InputStream is = getClass().getResourceAsStream("specs/" + filename); + return new Scanner(is).useDelimiter("\\A").next(); + } + + private String generateContent(ClassSpec spec) { + TypeSpec typeSpec = spec.poetSpec(); + JavaFile javaFile = JavaFile.builder(spec.className().packageName(), typeSpec).build(); + return javaFile.toString(); + } +} diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java new file mode 100644 index 000000000000..62d60b103745 --- /dev/null +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java @@ -0,0 +1,15 @@ +package software.amazon.awssdk.services.json.internal; + +import java.lang.String; +import software.amazon.awssdk.annotations.Generated; + +@Generated("software.amazon.awssdk:codegen") +public final class ServiceVersionInfo { + /** + * Returns the current version for the AWS SDK in which this class is running. + */ + public static final String VERSION = "{{VERSION}}"; + + private ServiceVersionInfo() { + } +}