Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-enable jdk9 collections support. #1481

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,13 @@ public void nullValuesInJdkMapErrorMsg() {
try {
ImmutableEntityWithMap.builder().properties(new HashMap<>()).build().withProperties(properties);
} catch (NullPointerException e) {
check(e.getMessage()).is("value for key: b");
boolean isJava8 = System.getProperty("java.version").startsWith("1.8");
if (isJava8) {
check(e.getMessage()).is("properties value for key: b");
} else {
// Java 9+ copy methods NPE has no message
check(e.getMessage()).isNull();
}
}
try {
ImmutableEntityWithMap.builder().putAllProperties(properties).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import java.io.IOException;
Expand All @@ -27,7 +28,8 @@
public class ObjectMappedTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
{
OBJECT_MAPPER.registerModule(new GuavaModule());
OBJECT_MAPPER.registerModule(new GuavaModule())
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make serialization stable between JDK8 and JDK9+.

}

public static class Wrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public void collections() {
.addPols(RetentionPolicy.RUNTIME, RetentionPolicy.RUNTIME)
.build();

check(coll.ints()).isOf(1, 2, 3, 4, 5, 6);
check(coll.navs()).isOf(3, 2, 1);
check(coll.ords()).isOf(4, 5, 6, 7, 8, 9);
check(coll.pols()).isOf(RetentionPolicy.RUNTIME);
check(coll.ints()).hasContentInAnyOrder(1, 2, 3, 4, 5, 6);
check(coll.navs()).hasContentInAnyOrder(3, 2, 1);
check(coll.ords()).hasContentInAnyOrder(4, 5, 6, 7, 8, 9);
check(coll.pols()).hasContentInAnyOrder(RetentionPolicy.RUNTIME);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make comparisons stable between JDK8 and JDK9+.
isOf was taking verifying elements is exact order

}

@Test
Expand Down Expand Up @@ -111,9 +111,9 @@ public void maps() {
.putAllOrds(ImmutableMap.of(2, "2", 1, "1"))
.build();

check(maps.navs().keySet()).isOf("33", "22");
check(maps.just().keySet()).isOf(1L, 2L);
check(maps.ords().keySet()).isOf(1, 2);
check(maps.navs().keySet()).hasContentInAnyOrder("33", "22");
check(maps.just().keySet()).hasContentInAnyOrder(1L, 2L);
check(maps.ords().keySet()).hasContentInAnyOrder(1, 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2442,8 +2442,13 @@ return [type.factoryOf]([output.linesShortable][for v in type.settableAttributes
[if v.nullable][expression][else][expression].clone()[/if]
[else if v.attributeValueKindCopy]
[if v.nullable][expression] == null ? null : [/if][deepCopyOf v][expression][/deepCopyOf]
[else if v.collectionType or v.mapType]
[else if v.mapType]
[if v.nullable][expression] == null ? null : [/if][immutableCollectionCopyOf v expression]
[else if v.collectionType]
[if v.nullable][expression] == null ? null : [/if][if v.isGenerateJdk9][expression] instanceof java.util.Collection<?>
? java.util.[v.rawCollectionType].copyOf((java.util.Collection<[v.consumedElementType]>) [expression])
: java.util.stream.StreamSupport.stream([expression].spliterator(), false)
.collect(java.util.stream.Collectors.toUnmodifiable[v.rawCollectionType]())[else][immutableCollectionCopyOf v expression][/if]
[else]
[maybeCopyOf v][maybeNonNullValue v][expression][/maybeNonNullValue][/maybeCopyOf]
[/if]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,8 @@ public boolean isGenerateJdkOnly() {
return style().jdkOnly() || noGuavaInClasspath();
}

// Until we can fix copy constructors generation for new Java9 collections
private static final boolean java9CollectionsEnabled = Boolean.getBoolean("immutables.java9-collections");

public boolean isGenerateJdk9() {
return java9CollectionsEnabled && constitution.protoclass().environment().hasJava9Collections();
return constitution.protoclass().environment().hasJava9Collections();
}

public boolean isGenerateBuildOrThrow() {
Expand Down Expand Up @@ -1162,12 +1159,10 @@ public boolean apply(ValueAttribute attribute) {
&& !attribute.isGuavaImmutableDeclared();
if (def) {
switch (kind) {
case MAP:
case LIST:
case SET:
return !attribute.isGenerateJdk9();
default:
return true;
case MAP:
return !attribute.isGenerateJdk9();
default:
return true;
}
}
return false;
Expand Down Expand Up @@ -1867,7 +1862,7 @@ public Reporter report() {
public List<String> getDebugLines() {
return constitution.protoclass().getDebugLines();
}

public boolean isDataInline() {
return DataInlineMirror.isPresent(element);
}
Expand Down