Skip to content

Commit

Permalink
Merge pull request #1481 from SimY4/topic/re-enable-jdk9-collections
Browse files Browse the repository at this point in the history
re-enable jdk9 collections support.
  • Loading branch information
elucash committed Sep 23, 2023
2 parents be599d4 + b9df716 commit 3f85a42
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
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
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);
}

public static class Wrapper {
Expand Down
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);
}

@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
Expand Up @@ -1662,7 +1662,7 @@ checkNotIsSet([v.names.isSet](), "[v.names.raw]");
[varargsSafety v]
public final java.util.List<[attributeBuilderBuilderType v.getAttributeBuilderDescriptor]> [v.names.getBuilders]() {
[if v.generateJdkOnly]
return createUnmodifiableList(false, this.[v.name]);
return [if v.generateJdk9]java.util.List.copyOf(this.[v.name])[else]createUnmodifiableList(false, this.[v.name])[/if];
[else]
return this.[v.name].build();
[/if]
Expand Down 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
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,12 @@ 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:
case LIST:
case SET:
return !attribute.isGenerateJdk9();
default:
return true;
}
}
return false;
Expand Down Expand Up @@ -1867,7 +1864,7 @@ public Reporter report() {
public List<String> getDebugLines() {
return constitution.protoclass().getDebugLines();
}

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

0 comments on commit 3f85a42

Please sign in to comment.