|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 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.transform; |
| 17 | + |
| 18 | +import com.squareup.javapoet.ClassName; |
| 19 | +import com.squareup.javapoet.CodeBlock; |
| 20 | +import com.squareup.javapoet.FieldSpec; |
| 21 | +import com.squareup.javapoet.MethodSpec; |
| 22 | +import com.squareup.javapoet.ParameterizedTypeName; |
| 23 | +import com.squareup.javapoet.TypeName; |
| 24 | +import com.squareup.javapoet.TypeSpec; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Optional; |
| 29 | +import javax.lang.model.element.Modifier; |
| 30 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 31 | +import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; |
| 32 | +import software.amazon.awssdk.codegen.model.intermediate.MemberModel; |
| 33 | +import software.amazon.awssdk.codegen.model.intermediate.ShapeModel; |
| 34 | +import software.amazon.awssdk.codegen.poet.ClassSpec; |
| 35 | +import software.amazon.awssdk.codegen.poet.PoetExtensions; |
| 36 | +import software.amazon.awssdk.codegen.poet.PoetUtils; |
| 37 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 38 | +import software.amazon.awssdk.core.protocol.MarshallLocation; |
| 39 | +import software.amazon.awssdk.core.protocol.MarshallingInfo; |
| 40 | +import software.amazon.awssdk.core.protocol.MarshallingType; |
| 41 | +import software.amazon.awssdk.core.protocol.ProtocolMarshaller; |
| 42 | +import software.amazon.awssdk.core.protocol.StructuredPojo; |
| 43 | +import software.amazon.awssdk.core.util.IdempotentUtils; |
| 44 | +import software.amazon.awssdk.utils.Validate; |
| 45 | + |
| 46 | +/** |
| 47 | + * Create ModelMarshaller for Json protocol |
| 48 | + */ |
| 49 | +public class JsonModelMarshallerSpec implements ClassSpec { |
| 50 | + |
| 51 | + private final IntermediateModel intermediateModel; |
| 52 | + private final ShapeModel shapeModel; |
| 53 | + private final ClassName className; |
| 54 | + private final ClassName requestClassName; |
| 55 | + |
| 56 | + public JsonModelMarshallerSpec(IntermediateModel intermediateModel, ShapeModel shapeModel, String className) { |
| 57 | + PoetExtensions poetExtensions = new PoetExtensions(intermediateModel); |
| 58 | + String modelPackage = intermediateModel.getMetadata().getFullModelPackageName(); |
| 59 | + this.intermediateModel = intermediateModel; |
| 60 | + this.shapeModel = shapeModel; |
| 61 | + this.className = poetExtensions.getTransformClass(shapeModel.getShapeName() + className); |
| 62 | + this.requestClassName = ClassName.get(modelPackage, shapeModel.getShapeName()); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public TypeSpec poetSpec() { |
| 67 | + return TypeSpec.classBuilder(className) |
| 68 | + .addModifiers(Modifier.PUBLIC) |
| 69 | + .addAnnotation(PoetUtils.GENERATED) |
| 70 | + .addAnnotation(SdkInternalApi.class) |
| 71 | + .addJavadoc("{@link $T} Marshaller", requestClassName) |
| 72 | + .addFields(memberVariables()) |
| 73 | + .addMethods(methods()) |
| 74 | + .build(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ClassName className() { |
| 79 | + return className; |
| 80 | + } |
| 81 | + |
| 82 | + private List<MethodSpec> methods() { |
| 83 | + List<MethodSpec> methodSpecs = new ArrayList<>(); |
| 84 | + |
| 85 | + methodSpecs.add(constructor()); |
| 86 | + methodSpecs.add(getInstanceMethod()); |
| 87 | + methodSpecs.add(marshallMethod()); |
| 88 | + return methodSpecs; |
| 89 | + } |
| 90 | + |
| 91 | + private List<FieldSpec> memberVariables() { |
| 92 | + List<FieldSpec> fields = new ArrayList<>(); |
| 93 | + |
| 94 | + FieldSpec instance = FieldSpec.builder(className, "INSTANCE") |
| 95 | + .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC) |
| 96 | + .initializer("new $T()", className) |
| 97 | + .build(); |
| 98 | + |
| 99 | + for (MemberModel memberModel : shapeModel.getNonStreamingMembers()) { |
| 100 | + TypeName typeName = ParameterizedTypeName.get(ClassName.get(MarshallingInfo.class), |
| 101 | + marshallingTargetClass(memberModel.getMarshallingTargetClass(), |
| 102 | + memberModel.getVariable().getVariableType())); |
| 103 | + |
| 104 | + CodeBlock.Builder initializationCodeBlockBuilder = CodeBlock.builder() |
| 105 | + .add("$T.builder($T.$L).marshallLocation($T.$L)", |
| 106 | + MarshallingInfo.class, |
| 107 | + MarshallingType.class, |
| 108 | + memberModel.getMarshallingType(), |
| 109 | + ClassName.get(MarshallLocation.class), |
| 110 | + memberModel.getHttp().getMarshallLocation()); |
| 111 | + |
| 112 | + if (memberModel.getHttp().getIsPayload()) { |
| 113 | + initializationCodeBlockBuilder.add(".isExplicitPayloadMember($L)", memberModel.getHttp().getIsPayload()); |
| 114 | + } else { |
| 115 | + initializationCodeBlockBuilder.add(".marshallLocationName($S)", memberModel.getHttp().getMarshallLocationName()); |
| 116 | + } |
| 117 | + |
| 118 | + initializationCodeBlockBuilder.add(".isBinary($L)", memberModel.getIsBinary()); |
| 119 | + |
| 120 | + Optional.ofNullable(intermediateModel.getCustomizationConfig().getModelMarshallerDefaultValueSupplier()) |
| 121 | + .map(defaultValueSupplier -> defaultValueSupplier.get(memberModel.getName())) |
| 122 | + .ifPresent(value -> initializationCodeBlockBuilder.add(".defaultValueSupplier($L)", value)); |
| 123 | + |
| 124 | + if (memberModel.isIdempotencyToken()) { |
| 125 | + initializationCodeBlockBuilder.add(".defaultValueSupplier($T.getGenerator())", ClassName.get(IdempotentUtils |
| 126 | + .class)); |
| 127 | + } |
| 128 | + |
| 129 | + CodeBlock codeBlock = initializationCodeBlockBuilder.add(".build()").build(); |
| 130 | + |
| 131 | + FieldSpec fieldSpec = FieldSpec.builder(typeName, memberModel.getMarshallerBindingFieldName()) |
| 132 | + .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC) |
| 133 | + .initializer(codeBlock) |
| 134 | + .build(); |
| 135 | + fields.add(fieldSpec); |
| 136 | + } |
| 137 | + |
| 138 | + fields.add(instance); |
| 139 | + return fields; |
| 140 | + } |
| 141 | + |
| 142 | + private MethodSpec constructor() { |
| 143 | + return MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build(); |
| 144 | + } |
| 145 | + |
| 146 | + private MethodSpec getInstanceMethod() { |
| 147 | + return MethodSpec.methodBuilder("getInstance").addModifiers(Modifier.STATIC, Modifier.PUBLIC) |
| 148 | + .addStatement("return INSTANCE") |
| 149 | + .returns(className) |
| 150 | + .build(); |
| 151 | + } |
| 152 | + |
| 153 | + private MethodSpec marshallMethod() { |
| 154 | + String variableName = shapeModel.getVariable().getVariableName(); |
| 155 | + MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder("marshall") |
| 156 | + .addJavadoc("Marshall the given parameter object") |
| 157 | + .addModifiers(Modifier.PUBLIC) |
| 158 | + .addParameter(requestClassName, variableName) |
| 159 | + .addParameter(ProtocolMarshaller.class, "protocolMarshaller"); |
| 160 | + |
| 161 | + if (shapeModel.getNonStreamingMembers().isEmpty()) { |
| 162 | + return methodSpecBuilder.build(); |
| 163 | + } |
| 164 | + |
| 165 | + methodSpecBuilder.addStatement("$T.paramNotNull($L, $S)", ClassName.get(Validate.class), variableName, variableName); |
| 166 | + methodSpecBuilder.addStatement("$T.paramNotNull($L, $S)", ClassName.get(Validate.class), "protocolMarshaller", |
| 167 | + "protocolMarshaller"); |
| 168 | + |
| 169 | + methodSpecBuilder.beginControlFlow("try"); |
| 170 | + shapeModel.getNonStreamingMembers().forEach( |
| 171 | + memberModel -> methodSpecBuilder.addStatement("protocolMarshaller.marshall($L.$L(), $L)", |
| 172 | + variableName, |
| 173 | + memberModel.getFluentGetterMethodName(), |
| 174 | + memberModel.getMarshallerBindingFieldName())); |
| 175 | + |
| 176 | + methodSpecBuilder.endControlFlow(); |
| 177 | + methodSpecBuilder.beginControlFlow("catch (Exception e)"); |
| 178 | + methodSpecBuilder.addStatement("throw new $T(\"Unable to marshall request to JSON: \" + e.getMessage(), e)", ClassName |
| 179 | + .get(SdkClientException.class)); |
| 180 | + methodSpecBuilder.endControlFlow(); |
| 181 | + return methodSpecBuilder.build(); |
| 182 | + } |
| 183 | + |
| 184 | + private ClassName marshallingTargetClass(String marshallerTargetClass, String variableType) { |
| 185 | + if ("List".equals(marshallerTargetClass)) { |
| 186 | + return ClassName.get(List.class); |
| 187 | + } else if ("String".equals(marshallerTargetClass)) { |
| 188 | + return ClassName.get(String.class); |
| 189 | + } else if ("Map".equals(marshallerTargetClass)) { |
| 190 | + return ClassName.get(Map.class); |
| 191 | + } else if ("StructuredPojo".equals(marshallerTargetClass)) { |
| 192 | + return ClassName.get(StructuredPojo.class); |
| 193 | + } else { |
| 194 | + return ClassName.bestGuess(variableType); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments