Skip to content

Commit 3bd2865

Browse files
jansabbezoewangg
jansabbe
authored andcommitted
Avoid using collect(toMap) in places where the value might be null
1 parent da5f60b commit 3bd2865

23 files changed

+100
-46
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"type": "bugfix",
4+
"description": "Fixes nullpointerexception when server responds with null values in map."
5+
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.squareup.javapoet.WildcardTypeName;
2525
import java.util.Collection;
2626
import java.util.Collections;
27+
import java.util.HashMap;
2728
import java.util.Map;
2829
import java.util.Optional;
2930
import java.util.stream.Collectors;
@@ -269,25 +270,25 @@ private CodeBlock mapCopyBody(boolean checkForModeledEnum) {
269270
CodeBlock keyCopyExpr =
270271
Optional.ofNullable(mapModel.getKeyModel())
271272
.map(model -> serviceModelCopiers.copierClassFor(model)
272-
.map(copier -> CodeBlock.of("e -> $T.$N(e.getKey())",
273+
.map(copier -> CodeBlock.of("$T.$N(e.getKey())",
273274
copier,
274275
copyMethod))
275276
.orElseGet(() -> checkForModeledEnum && model.getEnumType() != null
276-
? CodeBlock.of("e -> e.getKey().toString()")
277-
: CodeBlock.of("$T::getKey", Map.Entry.class)))
278-
.orElseGet(() -> CodeBlock.of("e -> $T.$N(e.getKey())",
277+
? CodeBlock.of("e.getKey().toString()")
278+
: CodeBlock.of("e.getKey()")))
279+
.orElseGet(() -> CodeBlock.of("$T.$N(e.getKey())",
279280
StandardMemberCopier.class,
280281
copyMethod));
281282

282283
CodeBlock valueCopyExpr =
283284
Optional.ofNullable(mapModel.getValueModel())
284285
.map(model -> serviceModelCopiers.copierClassFor(model)
285-
.map(copier -> CodeBlock.of("e -> $T.$N(e.getValue())",
286+
.map(copier -> CodeBlock.of("$T.$N(e.getValue())",
286287
copier,
287288
copyMethod))
288289
.orElseGet(() -> checkForModeledEnum && model.getEnumType() != null
289-
? CodeBlock.of("e -> e.getValue().toString()")
290-
: CodeBlock.of("$T::getValue", Map.Entry.class)))
290+
? CodeBlock.of("e.getValue().toString()")
291+
: CodeBlock.of("e.getValue()")))
291292
.orElseGet(() -> CodeBlock.of("e -> $T.$N(e.getValue())",
292293
StandardMemberCopier.class,
293294
copyMethod));
@@ -303,8 +304,9 @@ private CodeBlock mapCopyBody(boolean checkForModeledEnum) {
303304
.endControlFlow();
304305
}
305306

306-
builder.addStatement("$T $N = $N.entrySet().stream().collect(toMap($L, $L))", typeProvider.fieldType(memberModel),
307-
copyName, memberParamName(), keyCopyExpr, valueCopyExpr);
307+
builder.addStatement("$T $N = $N.entrySet().stream().collect($T::new, (m, e) -> m.put($L, $L), $T::putAll)",
308+
typeProvider.fieldType(memberModel), copyName, memberParamName(), HashMap.class, keyCopyExpr, valueCopyExpr,
309+
HashMap.class);
308310

309311
return builder.addStatement("return $T.unmodifiableMap($N)", Collections.class, copyName).build();
310312
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.SdkBytes;
@@ -17,7 +18,7 @@ static Map<String, SdkBytes> copy(Map<String, SdkBytes> blobMapTypeParam) {
1718
return DefaultSdkAutoConstructMap.getInstance();
1819
}
1920
Map<String, SdkBytes> blobMapTypeParamCopy = blobMapTypeParam.entrySet().stream()
20-
.collect(toMap(Map.Entry::getKey, e -> StandardMemberCopier.copy(e.getValue())));
21+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), StandardMemberCopier.copy(e.getValue())), HashMap::putAll);
2122
return Collections.unmodifiableMap(blobMapTypeParamCopy);
2223
}
2324
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.util.DefaultSdkAutoConstructMap;
@@ -15,7 +16,7 @@ static Map<String, String> copy(Map<String, String> mapOfEnumToEnumParam) {
1516
return DefaultSdkAutoConstructMap.getInstance();
1617
}
1718
Map<String, String> mapOfEnumToEnumParamCopy = mapOfEnumToEnumParam.entrySet().stream()
18-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
19+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1920
return Collections.unmodifiableMap(mapOfEnumToEnumParamCopy);
2021
}
2122

@@ -24,7 +25,7 @@ static Map<String, String> copyEnumToString(Map<EnumType, EnumType> mapOfEnumToE
2425
return DefaultSdkAutoConstructMap.getInstance();
2526
}
2627
Map<String, String> mapOfEnumToEnumParamCopy = mapOfEnumToEnumParam.entrySet().stream()
27-
.collect(toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));
28+
.collect(HashMap::new, (m, e) -> m.put(e.getKey().toString(), e.getValue().toString()), HashMap::putAll);
2829
return Collections.unmodifiableMap(mapOfEnumToEnumParamCopy);
2930
}
3031
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.util.DefaultSdkAutoConstructMap;
@@ -15,7 +16,7 @@ static Map<String, SimpleStruct> copy(Map<String, SimpleStruct> mapOfEnumToSimpl
1516
return DefaultSdkAutoConstructMap.getInstance();
1617
}
1718
Map<String, SimpleStruct> mapOfEnumToSimpleStructParamCopy = mapOfEnumToSimpleStructParam.entrySet().stream()
18-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
19+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1920
return Collections.unmodifiableMap(mapOfEnumToSimpleStructParamCopy);
2021
}
2122

@@ -31,7 +32,7 @@ static Map<String, SimpleStruct> copyEnumToString(Map<EnumType, SimpleStruct> ma
3132
return DefaultSdkAutoConstructMap.getInstance();
3233
}
3334
Map<String, SimpleStruct> mapOfEnumToSimpleStructParamCopy = mapOfEnumToSimpleStructParam.entrySet().stream()
34-
.collect(toMap(e -> e.getKey().toString(), Map.Entry::getValue));
35+
.collect(HashMap::new, (m, e) -> m.put(e.getKey().toString(), e.getValue()), HashMap::putAll);
3536
return Collections.unmodifiableMap(mapOfEnumToSimpleStructParamCopy);
3637
}
3738
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.util.DefaultSdkAutoConstructMap;
@@ -15,7 +16,7 @@ static Map<String, String> copy(Map<String, String> mapOfEnumToStringParam) {
1516
return DefaultSdkAutoConstructMap.getInstance();
1617
}
1718
Map<String, String> mapOfEnumToStringParamCopy = mapOfEnumToStringParam.entrySet().stream()
18-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
19+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1920
return Collections.unmodifiableMap(mapOfEnumToStringParamCopy);
2021
}
2122

@@ -24,7 +25,7 @@ static Map<String, String> copyEnumToString(Map<EnumType, String> mapOfEnumToStr
2425
return DefaultSdkAutoConstructMap.getInstance();
2526
}
2627
Map<String, String> mapOfEnumToStringParamCopy = mapOfEnumToStringParam.entrySet().stream()
27-
.collect(toMap(e -> e.getKey().toString(), Map.Entry::getValue));
28+
.collect(HashMap::new, (m, e) -> m.put(e.getKey().toString(), e.getValue()), HashMap::putAll);
2829
return Collections.unmodifiableMap(mapOfEnumToStringParamCopy);
2930
}
3031
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.util.DefaultSdkAutoConstructMap;
@@ -15,7 +16,7 @@ static Map<String, String> copy(Map<String, String> mapOfStringToEnumParam) {
1516
return DefaultSdkAutoConstructMap.getInstance();
1617
}
1718
Map<String, String> mapOfStringToEnumParamCopy = mapOfStringToEnumParam.entrySet().stream()
18-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
19+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1920
return Collections.unmodifiableMap(mapOfStringToEnumParamCopy);
2021
}
2122

@@ -24,7 +25,7 @@ static Map<String, String> copyEnumToString(Map<String, EnumType> mapOfStringToE
2425
return DefaultSdkAutoConstructMap.getInstance();
2526
}
2627
Map<String, String> mapOfStringToEnumParamCopy = mapOfStringToEnumParam.entrySet().stream()
27-
.collect(toMap(Map.Entry::getKey, e -> e.getValue().toString()));
28+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue().toString()), HashMap::putAll);
2829
return Collections.unmodifiableMap(mapOfStringToEnumParamCopy);
2930
}
3031
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Collection;
66
import java.util.Collections;
7+
import java.util.HashMap;
78
import java.util.List;
89
import java.util.Map;
910
import software.amazon.awssdk.annotations.Generated;
@@ -17,7 +18,7 @@ static Map<String, List<Integer>> copy(Map<String, ? extends Collection<Integer>
1718
return DefaultSdkAutoConstructMap.getInstance();
1819
}
1920
Map<String, List<Integer>> mapOfStringToIntegerListParamCopy = mapOfStringToIntegerListParam.entrySet().stream()
20-
.collect(toMap(Map.Entry::getKey, e -> ListOfIntegersCopier.copy(e.getValue())));
21+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), ListOfIntegersCopier.copy(e.getValue())), HashMap::putAll);
2122
return Collections.unmodifiableMap(mapOfStringToIntegerListParamCopy);
2223
}
2324
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Collection;
66
import java.util.Collections;
7+
import java.util.HashMap;
78
import java.util.List;
89
import java.util.Map;
910
import software.amazon.awssdk.annotations.Generated;
@@ -13,12 +14,14 @@
1314
@Generated("software.amazon.awssdk:codegen")
1415
final class MapOfStringToListOfListOfStringsCopier {
1516
static Map<String, List<List<String>>> copy(
16-
Map<String, ? extends Collection<? extends Collection<String>>> mapOfStringToListOfListOfStringsParam) {
17+
Map<String, ? extends Collection<? extends Collection<String>>> mapOfStringToListOfListOfStringsParam) {
1718
if (mapOfStringToListOfListOfStringsParam == null || mapOfStringToListOfListOfStringsParam instanceof SdkAutoConstructMap) {
1819
return DefaultSdkAutoConstructMap.getInstance();
1920
}
2021
Map<String, List<List<String>>> mapOfStringToListOfListOfStringsParamCopy = mapOfStringToListOfListOfStringsParam
21-
.entrySet().stream().collect(toMap(Map.Entry::getKey, e -> ListOfListOfStringsCopier.copy(e.getValue())));
22+
.entrySet()
23+
.stream()
24+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), ListOfListOfStringsCopier.copy(e.getValue())), HashMap::putAll);
2225
return Collections.unmodifiableMap(mapOfStringToListOfListOfStringsParamCopy);
2326
}
2427
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.util.DefaultSdkAutoConstructMap;
@@ -15,7 +16,7 @@ static Map<String, SimpleStruct> copy(Map<String, SimpleStruct> mapOfStringToSim
1516
return DefaultSdkAutoConstructMap.getInstance();
1617
}
1718
Map<String, SimpleStruct> mapOfStringToSimpleStructParamCopy = mapOfStringToSimpleStructParam.entrySet().stream()
18-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
19+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1920
return Collections.unmodifiableMap(mapOfStringToSimpleStructParamCopy);
2021
}
2122

@@ -24,6 +25,6 @@ static Map<String, SimpleStruct> copyFromBuilder(Map<String, ? extends SimpleStr
2425
return null;
2526
}
2627
return copy(mapOfStringToSimpleStructParam.entrySet().stream()
27-
.collect(toMap(Map.Entry::getKey, e -> e.getValue().build())));
28+
.collect(toMap(Map.Entry::getKey, e -> e.getValue().build())));
2829
}
2930
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.util.DefaultSdkAutoConstructMap;
@@ -15,7 +16,7 @@ static Map<String, String> copy(Map<String, String> mapOfStringToStringParam) {
1516
return DefaultSdkAutoConstructMap.getInstance();
1617
}
1718
Map<String, String> mapOfStringToStringParamCopy = mapOfStringToStringParam.entrySet().stream()
18-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
19+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1920
return Collections.unmodifiableMap(mapOfStringToStringParamCopy);
2021
}
2122
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89
import software.amazon.awssdk.core.SdkBytes;
@@ -15,7 +16,7 @@ static Map<String, SdkBytes> copy(Map<String, SdkBytes> blobMapTypeParam) {
1516
return null;
1617
}
1718
Map<String, SdkBytes> blobMapTypeParamCopy = blobMapTypeParam.entrySet().stream()
18-
.collect(toMap(Map.Entry::getKey, e -> StandardMemberCopier.copy(e.getValue())));
19+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), StandardMemberCopier.copy(e.getValue())), HashMap::putAll);
1920
return Collections.unmodifiableMap(blobMapTypeParamCopy);
2021
}
2122
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89

@@ -13,7 +14,7 @@ static Map<String, String> copy(Map<String, String> mapOfEnumToEnumParam) {
1314
return null;
1415
}
1516
Map<String, String> mapOfEnumToEnumParamCopy = mapOfEnumToEnumParam.entrySet().stream()
16-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
17+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1718
return Collections.unmodifiableMap(mapOfEnumToEnumParamCopy);
1819
}
1920

@@ -22,7 +23,7 @@ static Map<String, String> copyEnumToString(Map<EnumType, EnumType> mapOfEnumToE
2223
return null;
2324
}
2425
Map<String, String> mapOfEnumToEnumParamCopy = mapOfEnumToEnumParam.entrySet().stream()
25-
.collect(toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));
26+
.collect(HashMap::new, (m, e) -> m.put(e.getKey().toString(), e.getValue().toString()), HashMap::putAll);
2627
return Collections.unmodifiableMap(mapOfEnumToEnumParamCopy);
2728
}
2829
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89

@@ -13,7 +14,7 @@ static Map<String, SimpleStruct> copy(Map<String, SimpleStruct> mapOfEnumToSimpl
1314
return null;
1415
}
1516
Map<String, SimpleStruct> mapOfEnumToSimpleStructParamCopy = mapOfEnumToSimpleStructParam.entrySet().stream()
16-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
17+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1718
return Collections.unmodifiableMap(mapOfEnumToSimpleStructParamCopy);
1819
}
1920

@@ -29,7 +30,7 @@ static Map<String, SimpleStruct> copyEnumToString(Map<EnumType, SimpleStruct> ma
2930
return null;
3031
}
3132
Map<String, SimpleStruct> mapOfEnumToSimpleStructParamCopy = mapOfEnumToSimpleStructParam.entrySet().stream()
32-
.collect(toMap(e -> e.getKey().toString(), Map.Entry::getValue));
33+
.collect(HashMap::new, (m, e) -> m.put(e.getKey().toString(), e.getValue()), HashMap::putAll);
3334
return Collections.unmodifiableMap(mapOfEnumToSimpleStructParamCopy);
3435
}
3536
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.stream.Collectors.toMap;
44

55
import java.util.Collections;
6+
import java.util.HashMap;
67
import java.util.Map;
78
import software.amazon.awssdk.annotations.Generated;
89

@@ -13,7 +14,7 @@ static Map<String, String> copy(Map<String, String> mapOfEnumToStringParam) {
1314
return null;
1415
}
1516
Map<String, String> mapOfEnumToStringParamCopy = mapOfEnumToStringParam.entrySet().stream()
16-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
17+
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
1718
return Collections.unmodifiableMap(mapOfEnumToStringParamCopy);
1819
}
1920

@@ -22,7 +23,7 @@ static Map<String, String> copyEnumToString(Map<EnumType, String> mapOfEnumToStr
2223
return null;
2324
}
2425
Map<String, String> mapOfEnumToStringParamCopy = mapOfEnumToStringParam.entrySet().stream()
25-
.collect(toMap(e -> e.getKey().toString(), Map.Entry::getValue));
26+
.collect(HashMap::new, (m, e) -> m.put(e.getKey().toString(), e.getValue()), HashMap::putAll);
2627
return Collections.unmodifiableMap(mapOfEnumToStringParamCopy);
2728
}
2829
}

0 commit comments

Comments
 (0)