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

Support abstract classes declaring static interned instances of their own type #1274

Merged

Conversation

Stephan202
Copy link
Contributor

Summary says it.

Copy link
Contributor Author

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Some context :)

@Value.Immutable(intern = true)
@Value.Style(allParameters = true)
abstract class InternedClassWithSelfConstant {
public static final ImmutableInternedClassWithSelfConstant FOO = ImmutableInternedClassWithSelfConstant.of("foo");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what the PR is about: the changes make this construction possible.

public class InternedClassWithSelfConstantTest {
@Test
public void testInstantiation() {
assertSame(ImmutableInternedClassWithSelfConstant.of("foo"), ImmutableInternedClassWithSelfConstant.FOO);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the fix below this test fails with:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.017 s <<< FAILURE! - in org.immutables.fixture.InternedClassWithSelfConstantTest
[ERROR] testInstantiation  Time elapsed: 0.015 s  <<< ERROR!
java.lang.ExceptionInInitializerError
	at org.immutables.fixture.InternedClassWithSelfConstantTest.testInstantiation(InternedClassWithSelfConstantTest.java:24)
Caused by: java.lang.NullPointerException
	at org.immutables.fixture.InternedClassWithSelfConstantTest.testInstantiation(InternedClassWithSelfConstantTest.java:24)

@@ -697,21 +697,23 @@ public [type.typeAbstract.relative] [v.names.with]([v.atNullability][v.type] val
[else if type.useSingletonOnly][-- deliberately left empty --]
[else if type.useInterned]

private static final class InternerHolder {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're effectively applying the Initialization-on-demand holder idiom.

@Stephan202 Stephan202 force-pushed the bugfix/interning-self-constants branch from c347c64 to 9cd6cc7 Compare March 18, 2021 20:11
[else]
private static final java.util.concurrent.ConcurrentHashMap<InternProxy, [type.typeImmutable.relative]> INTERNER =
static final java.util.concurrent.ConcurrentHashMap<InternProxy, [type.typeImmutable.relative]> INTERNER =
Copy link
Member

Choose a reason for hiding this comment

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

I'm merging this, just in case what removing private does here

@elucash elucash merged commit 715317b into immutables:master Apr 16, 2021
@elucash
Copy link
Member

elucash commented Apr 16, 2021

Thanks!

@Stephan202 Stephan202 deleted the bugfix/interning-self-constants branch May 22, 2021 08:01
@elucash elucash added this to the 2.9.0 milestone Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants