Skip to content

Commit 89e0e75

Browse files
authored
Stop creating anonymous HashMap in models (#5748)
This commit updates the generation of the service models classes so that we don't create anonymous HashMaps for the `SDK_NAME_TO_FIELD` field. Rather, we move the initialization to a static method that populates a normal HashMap returns it. This lowers the size of the JARs because the compiler doesn't need to create separate .class files for each of these anonymous implementations of HashMap.
1 parent 49ba682 commit 89e0e75

File tree

52 files changed

+3111
-2965
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3111
-2965
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public TypeSpec poetSpec() {
113113
.addMethod(addModifier(sdkFieldNameToFieldMethod(), FINAL))
114114
.addTypes(nestedModelClassTypes());
115115

116+
shapeModelSpec.additionalMethods().forEach(specBuilder::addMethod);
117+
116118
if (shapeModel.isUnion()) {
117119
specBuilder.addField(unionTypeField());
118120
}

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.squareup.javapoet.ClassName;
1919
import com.squareup.javapoet.CodeBlock;
2020
import com.squareup.javapoet.FieldSpec;
21+
import com.squareup.javapoet.MethodSpec;
2122
import com.squareup.javapoet.ParameterizedTypeName;
2223
import com.squareup.javapoet.WildcardTypeName;
2324
import java.util.ArrayList;
@@ -128,7 +129,7 @@ public Iterable<FieldSpec> staticFields(Modifier... modifiers) {
128129
sdkFieldType),
129130
"SDK_NAME_TO_FIELD",
130131
Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
131-
.initializer(memberNameToFieldInitializer(nameToField))
132+
.initializer(memberNameToFieldInitializer())
132133
.build());
133134
return fields;
134135
}
@@ -150,6 +151,22 @@ private CodeBlock sdkFieldInitializer(MemberModel m) {
150151
.build();
151152
}
152153

154+
public List<MethodSpec> additionalMethods() {
155+
return Collections.singletonList(memberNameToFieldInitializerMethod(nameToField()));
156+
}
157+
158+
public Map<String, String> nameToField() {
159+
Map<String, String> nameToField = new LinkedHashMap<>();
160+
shapeModel.getNonStreamingMembers().stream()
161+
.filter(m -> m.getShape() == null || m.getShape().getShapeType() != ShapeType.Exception)
162+
.filter(m -> !m.isSynthetic())
163+
.forEach(m -> {
164+
String name = m.getHttp().getMarshallLocationName();
165+
nameToField.put(name, namingStrategy.getSdkFieldFieldName(m));
166+
});
167+
return nameToField;
168+
}
169+
153170
private CodeBlock containerSdkFieldInitializer(MemberModel m) {
154171
ClassName sdkFieldType = ClassName.get(SdkField.class);
155172
return CodeBlock.builder()
@@ -392,18 +409,29 @@ private CodeBlock sdkFieldsInitializer(List<FieldSpec> fields) {
392409
return builder.build();
393410
}
394411

395-
private CodeBlock memberNameToFieldInitializer(Map<String, String> nameToField) {
396-
CodeBlock.Builder builder = CodeBlock.builder();
412+
private CodeBlock memberNameToFieldInitializer() {
413+
return CodeBlock.builder()
414+
.add("memberNameToFieldInitializer()")
415+
.build();
416+
}
417+
418+
private MethodSpec memberNameToFieldInitializerMethod(Map<String, String> nameToField) {
419+
ParameterizedTypeName sdkFieldT = ParameterizedTypeName.get(ClassName.get(SdkField.class),
420+
WildcardTypeName.subtypeOf(Object.class));
421+
ParameterizedTypeName mapT = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class),
422+
sdkFieldT);
423+
MethodSpec.Builder builder = MethodSpec.methodBuilder("memberNameToFieldInitializer")
424+
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
425+
.returns(mapT);
426+
397427
if (nameToField.isEmpty()) {
398-
builder.add("$T.emptyMap()", Collections.class);
399-
return builder.build();
428+
builder.addStatement("return $T.emptyMap()", Collections.class);
429+
} else {
430+
builder.addStatement("$T map = new $T<>()", mapT, HashMap.class);
431+
nameToField.forEach((name, field) -> builder.addStatement("map.put($S, $L)", name, field));
432+
builder.addStatement("return $T.unmodifiableMap(map)", Collections.class);
400433
}
401-
builder.add("$T.unmodifiableMap(", Collections.class);
402-
builder.add("new $T<$T, $T<?>>() {{\n", HashMap.class, String.class, SdkField.class);
403-
nameToField.forEach((name, field) -> builder.add("put($S, $L);\n", name, field));
404-
builder.add("}}");
405-
builder.add(")");
434+
406435
return builder.build();
407436
}
408-
409437
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/alltypesrequest.java

Lines changed: 598 additions & 597 deletions
Large diffs are not rendered by default.

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/alltypesresponse.java

Lines changed: 598 additions & 597 deletions
Large diffs are not rendered by default.

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/alltypesunionstructure.java

Lines changed: 604 additions & 603 deletions
Large diffs are not rendered by default.

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/basetype.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,12 @@
2525
@Generated("software.amazon.awssdk:codegen")
2626
public final class BaseType implements SdkPojo, Serializable, ToCopyableBuilder<BaseType.Builder, BaseType> {
2727
private static final SdkField<String> BASE_MEMBER_FIELD = SdkField.<String> builder(MarshallingType.STRING)
28-
.memberName("BaseMember").getter(getter(BaseType::baseMember)).setter(setter(Builder::baseMember))
29-
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("BaseMember").build()).build();
28+
.memberName("BaseMember").getter(getter(BaseType::baseMember)).setter(setter(Builder::baseMember))
29+
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("BaseMember").build()).build();
3030

3131
private static final List<SdkField<?>> SDK_FIELDS = Collections.unmodifiableList(Arrays.asList(BASE_MEMBER_FIELD));
3232

33-
private static final Map<String, SdkField<?>> SDK_NAME_TO_FIELD = Collections
34-
.unmodifiableMap(new HashMap<String, SdkField<?>>() {
35-
{
36-
put("BaseMember", BASE_MEMBER_FIELD);
37-
}
38-
});
33+
private static final Map<String, SdkField<?>> SDK_NAME_TO_FIELD = memberNameToFieldInitializer();
3934

4035
private static final long serialVersionUID = 1L;
4136

@@ -53,7 +48,7 @@ private BaseType(BuilderImpl builder) {
5348

5449
/**
5550
* Returns the value of the BaseMember property for this object.
56-
*
51+
*
5752
* @return The value of the BaseMember property for this object.
5853
*/
5954
public final String baseMember() {
@@ -62,7 +57,7 @@ public final String baseMember() {
6257

6358
/**
6459
* Custom shape of type string
65-
*
60+
*
6661
* @return Custom shape of type string
6762
*/
6863
public final String customShape1() {
@@ -71,7 +66,7 @@ public final String customShape1() {
7166

7267
/**
7368
* Custom shape of type integer
74-
*
69+
*
7570
* @return Custom shape of type integer
7671
*/
7772
public final Integer customShape2() {
@@ -118,7 +113,7 @@ public final boolean equalsBySdkFields(Object obj) {
118113
}
119114
BaseType other = (BaseType) obj;
120115
return Objects.equals(baseMember(), other.baseMember()) && Objects.equals(customShape1(), other.customShape1())
121-
&& Objects.equals(customShape2(), other.customShape2());
116+
&& Objects.equals(customShape2(), other.customShape2());
122117
}
123118

124119
/**
@@ -128,19 +123,19 @@ public final boolean equalsBySdkFields(Object obj) {
128123
@Override
129124
public final String toString() {
130125
return ToString.builder("BaseType").add("BaseMember", baseMember()).add("CustomShape1", customShape1())
131-
.add("CustomShape2", customShape2()).build();
126+
.add("CustomShape2", customShape2()).build();
132127
}
133128

134129
public final <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) {
135130
switch (fieldName) {
136-
case "BaseMember":
137-
return Optional.ofNullable(clazz.cast(baseMember()));
138-
case "CustomShape1":
139-
return Optional.ofNullable(clazz.cast(customShape1()));
140-
case "CustomShape2":
141-
return Optional.ofNullable(clazz.cast(customShape2()));
142-
default:
143-
return Optional.empty();
131+
case "BaseMember":
132+
return Optional.ofNullable(clazz.cast(baseMember()));
133+
case "CustomShape1":
134+
return Optional.ofNullable(clazz.cast(customShape1()));
135+
case "CustomShape2":
136+
return Optional.ofNullable(clazz.cast(customShape2()));
137+
default:
138+
return Optional.empty();
144139
}
145140
}
146141

@@ -154,6 +149,12 @@ public final Map<String, SdkField<?>> sdkFieldNameToField() {
154149
return SDK_NAME_TO_FIELD;
155150
}
156151

152+
private static Map<String, SdkField<?>> memberNameToFieldInitializer() {
153+
Map<String, SdkField<?>> map = new HashMap<>();
154+
map.put("BaseMember", BASE_MEMBER_FIELD);
155+
return Collections.unmodifiableMap(map);
156+
}
157+
157158
private static <T> Function<Object, T> getter(Function<BaseType, T> g) {
158159
return obj -> g.apply((BaseType) obj);
159160
}
@@ -174,7 +175,7 @@ public interface Builder extends SdkPojo, CopyableBuilder<Builder, BaseType> {
174175

175176
/**
176177
* Custom shape of type string
177-
*
178+
*
178179
* @param customShape1
179180
* Custom shape of type string
180181
* @return Returns a reference to this object so that method calls can be chained together.
@@ -183,7 +184,7 @@ public interface Builder extends SdkPojo, CopyableBuilder<Builder, BaseType> {
183184

184185
/**
185186
* Custom shape of type integer
186-
*
187+
*
187188
* @param customShape2
188189
* Custom shape of type integer
189190
* @return Returns a reference to this object so that method calls can be chained together.

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/deprecatedrenamerequest.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,22 @@
2525
*/
2626
@Generated("software.amazon.awssdk:codegen")
2727
public final class DeprecatedRenameRequest extends JsonProtocolTestsRequest implements
28-
ToCopyableBuilder<DeprecatedRenameRequest.Builder, DeprecatedRenameRequest> {
28+
ToCopyableBuilder<DeprecatedRenameRequest.Builder, DeprecatedRenameRequest> {
2929
private static final SdkField<String> NEW_NAME_NO_DEPRECATION_FIELD = SdkField.<String> builder(MarshallingType.STRING)
30-
.memberName("NewNameNoDeprecation").getter(getter(DeprecatedRenameRequest::newNameNoDeprecation))
31-
.setter(setter(Builder::newNameNoDeprecation))
32-
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("OriginalNameNoDeprecation").build())
33-
.build();
30+
.memberName("NewNameNoDeprecation").getter(getter(DeprecatedRenameRequest::newNameNoDeprecation))
31+
.setter(setter(Builder::newNameNoDeprecation))
32+
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("OriginalNameNoDeprecation").build())
33+
.build();
3434

3535
private static final SdkField<String> NEW_NAME_FIELD = SdkField.<String> builder(MarshallingType.STRING)
36-
.memberName("NewName").getter(getter(DeprecatedRenameRequest::newName)).setter(setter(Builder::newName))
37-
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("OriginalNameDeprecated").build())
38-
.build();
36+
.memberName("NewName").getter(getter(DeprecatedRenameRequest::newName)).setter(setter(Builder::newName))
37+
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("OriginalNameDeprecated").build())
38+
.build();
3939

4040
private static final List<SdkField<?>> SDK_FIELDS = Collections.unmodifiableList(Arrays.asList(NEW_NAME_NO_DEPRECATION_FIELD,
41-
NEW_NAME_FIELD));
41+
NEW_NAME_FIELD));
4242

43-
private static final Map<String, SdkField<?>> SDK_NAME_TO_FIELD = Collections
44-
.unmodifiableMap(new HashMap<String, SdkField<?>>() {
45-
{
46-
put("OriginalNameNoDeprecation", NEW_NAME_NO_DEPRECATION_FIELD);
47-
put("OriginalNameDeprecated", NEW_NAME_FIELD);
48-
}
49-
});
43+
private static final Map<String, SdkField<?>> SDK_NAME_TO_FIELD = memberNameToFieldInitializer();
5044

5145
private final String newNameNoDeprecation;
5246

@@ -60,7 +54,7 @@ private DeprecatedRenameRequest(BuilderImpl builder) {
6054

6155
/**
6256
* Returns the value of the NewNameNoDeprecation property for this object.
63-
*
57+
*
6458
* @return The value of the NewNameNoDeprecation property for this object.
6559
*/
6660
public final String newNameNoDeprecation() {
@@ -69,7 +63,7 @@ public final String newNameNoDeprecation() {
6963

7064
/**
7165
* Returns the value of the NewName property for this object.
72-
*
66+
*
7367
* @return The value of the NewName property for this object.
7468
* @deprecated Use {@link #newName()}
7569
*/
@@ -80,7 +74,7 @@ public final String originalNameDeprecated() {
8074

8175
/**
8276
* Returns the value of the NewName property for this object.
83-
*
77+
*
8478
* @return The value of the NewName property for this object.
8579
*/
8680
public final String newName() {
@@ -136,19 +130,19 @@ public final boolean equalsBySdkFields(Object obj) {
136130
@Override
137131
public final String toString() {
138132
return ToString.builder("DeprecatedRenameRequest").add("NewNameNoDeprecation", newNameNoDeprecation())
139-
.add("NewName", newName()).build();
133+
.add("NewName", newName()).build();
140134
}
141135

142136
public final <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) {
143137
switch (fieldName) {
144-
case "NewNameNoDeprecation":
145-
return Optional.ofNullable(clazz.cast(newNameNoDeprecation()));
146-
case "NewName":
147-
return Optional.ofNullable(clazz.cast(newName()));
148-
case "OriginalNameDeprecated":
149-
return Optional.ofNullable(clazz.cast(newName()));
150-
default:
151-
return Optional.empty();
138+
case "NewNameNoDeprecation":
139+
return Optional.ofNullable(clazz.cast(newNameNoDeprecation()));
140+
case "NewName":
141+
return Optional.ofNullable(clazz.cast(newName()));
142+
case "OriginalNameDeprecated":
143+
return Optional.ofNullable(clazz.cast(newName()));
144+
default:
145+
return Optional.empty();
152146
}
153147
}
154148

@@ -162,6 +156,13 @@ public final Map<String, SdkField<?>> sdkFieldNameToField() {
162156
return SDK_NAME_TO_FIELD;
163157
}
164158

159+
private static Map<String, SdkField<?>> memberNameToFieldInitializer() {
160+
Map<String, SdkField<?>> map = new HashMap<>();
161+
map.put("OriginalNameNoDeprecation", NEW_NAME_NO_DEPRECATION_FIELD);
162+
map.put("OriginalNameDeprecated", NEW_NAME_FIELD);
163+
return Collections.unmodifiableMap(map);
164+
}
165+
165166
private static <T> Function<Object, T> getter(Function<DeprecatedRenameRequest, T> g) {
166167
return obj -> g.apply((DeprecatedRenameRequest) obj);
167168
}

0 commit comments

Comments
 (0)