-
Notifications
You must be signed in to change notification settings - Fork 272
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
Support abstract classes declaring static interned instances of their own type #1274
Conversation
There was a problem hiding this 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"); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
c347c64
to
9cd6cc7
Compare
[else] | ||
private static final java.util.concurrent.ConcurrentHashMap<InternProxy, [type.typeImmutable.relative]> INTERNER = | ||
static final java.util.concurrent.ConcurrentHashMap<InternProxy, [type.typeImmutable.relative]> INTERNER = |
There was a problem hiding this comment.
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
Thanks! |
Summary says it.