Skip to content

Commit eecbc5b

Browse files
committed
#1612 fixing and refining no-arg constructors
1 parent b659a65 commit eecbc5b

6 files changed

Lines changed: 84 additions & 14 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.immutables.fixture;
2+
3+
import org.immutables.value.Value;
4+
5+
@Value.Immutable
6+
@Value.Style(
7+
allParameters = true,
8+
privateNoargConstructor = true
9+
)
10+
public interface NoParametersPrivateConstructor {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.immutables.fixture;
2+
3+
import org.immutables.value.Value;
4+
5+
@Value.Immutable
6+
@Value.Style(
7+
allParameters = true,
8+
protectedNoargConstructor = true
9+
)
10+
public interface NoParametersProtectedConstructor {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.immutables.fixture;
2+
3+
import org.immutables.value.Value;
4+
5+
@Value.Immutable(singleton = true)
6+
@Value.Style(
7+
allParameters = true,
8+
protectedNoargConstructor = true
9+
)
10+
public interface NoParametersSingletonProtectedConstructor {}

value-fixture/test/org/immutables/fixture/ValuesTest.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,26 @@
1515
*/
1616
package org.immutables.fixture;
1717

18-
import com.google.common.collect.ImmutableList;
19-
import com.google.common.collect.ImmutableMap;
2018
import nonimmutables.GetterAnnotation;
21-
import org.immutables.fixture.ImmutableSampleCopyOfTypes.ByBuilder;
22-
import org.immutables.fixture.ImmutableSampleCopyOfTypes.ByConstructorAndWithers;
23-
import org.immutables.fixture.style.ImmutableOptionalWithNullable;
24-
import org.immutables.fixture.style.ImmutableOptionalWithoutNullable;
25-
import org.junit.jupiter.api.Test;
26-
27-
import javax.ws.rs.POST;
2819
import java.io.IOException;
2920
import java.lang.annotation.RetentionPolicy;
3021
import java.lang.reflect.AnnotatedType;
22+
import java.lang.reflect.Constructor;
3123
import java.lang.reflect.Method;
3224
import java.lang.reflect.Modifier;
3325
import java.util.Arrays;
3426
import java.util.Collections;
3527
import java.util.List;
3628
import java.util.concurrent.ExecutionException;
3729
import java.util.concurrent.atomic.AtomicInteger;
38-
30+
import javax.ws.rs.POST;
31+
import com.google.common.collect.ImmutableList;
32+
import com.google.common.collect.ImmutableMap;
33+
import org.immutables.fixture.ImmutableSampleCopyOfTypes.ByBuilder;
34+
import org.immutables.fixture.ImmutableSampleCopyOfTypes.ByConstructorAndWithers;
35+
import org.immutables.fixture.style.ImmutableOptionalWithNullable;
36+
import org.immutables.fixture.style.ImmutableOptionalWithoutNullable;
37+
import org.junit.jupiter.api.Test;
3938
import static org.immutables.check.Checkers.check;
4039
import static org.junit.jupiter.api.Assertions.assertThrows;
4140

@@ -664,4 +663,37 @@ public void noParametersConstructor() {
664663
check(c).not().same(b);
665664
check(c).is(b);
666665
}
666+
667+
@Test
668+
public void noParametersPrivateConstructor() throws Exception {
669+
NoParametersPrivateConstructor c = ImmutableNoParametersPrivateConstructor.of();
670+
NoParametersPrivateConstructor b = ImmutableNoParametersPrivateConstructor.builder().build();
671+
check(c).not().same(b);
672+
check(c).is(b);
673+
674+
Constructor<?> constructor = ImmutableNoParametersPrivateConstructor.class.getDeclaredConstructor();
675+
check((constructor.getModifiers() & Modifier.PRIVATE) != 0);
676+
}
677+
678+
@Test
679+
public void noParametersProtectedConstructor() throws Exception {
680+
NoParametersProtectedConstructor c = ImmutableNoParametersProtectedConstructor.of();
681+
NoParametersProtectedConstructor b = ImmutableNoParametersProtectedConstructor.builder().build();
682+
check(c).not().same(b);
683+
check(c).is(b);
684+
685+
Constructor<?> constructor = ImmutableNoParametersProtectedConstructor.class.getDeclaredConstructor();
686+
check((constructor.getModifiers() & Modifier.PROTECTED) != 0);
687+
}
688+
689+
@Test
690+
public void noParametersSingletonProtectedConstructor() throws Exception {
691+
NoParametersSingletonProtectedConstructor c = ImmutableNoParametersSingletonProtectedConstructor.of();
692+
NoParametersSingletonProtectedConstructor b = ImmutableNoParametersSingletonProtectedConstructor.builder().build();
693+
check(c).same(b);
694+
check(c).is(b);
695+
696+
Constructor<?> constructor = ImmutableNoParametersProtectedConstructor.class.getDeclaredConstructor();
697+
check((constructor.getModifiers() & Modifier.PROTECTED) != 0);
698+
}
667699
}

value-processor/src/org/immutables/value/processor/Immutables.generator

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,8 @@ checkNotIsSet([v.names.isSet](), "[v.names.raw]");
28892889
[returnFactoryBuild type]
28902890
[else if type.useSingleton andnot type.settableAttributes]
28912891
return [type.factoryInstance.relative]();
2892+
[else if type.useConstructor andnot type.constructorArguments]
2893+
return [validated type true]new [type.typeImmutable.relative]()[/validated];
28922894
[else if type.generateBuilderUseCopyConstructor]
28932895
[generateReturnBuilderConstructed type]
28942896
[else]
@@ -3158,7 +3160,7 @@ this.[n] = [valueFromValue v][invokeSuper v].[v.names.get]()[/valueFromValue];
31583160
[/if]
31593161
[if type.useSingleton]
31603162

3161-
private [type.typeImmutable.simple]() {[output.collapsible]
3163+
[if type.style.protectedNoargConstructor]protected[else]private[/if] [type.typeImmutable.simple]() {[output.collapsible]
31623164
[generateConstructorDefaultAttributes type type.implementedAttributes]
31633165
[generateDerivedConstruction type noAttributes]
31643166
[generateAfterConstruction type false]
@@ -3285,6 +3287,8 @@ this.[n] = [valueFromValue v][invokeSuper v].[v.names.get]()[/valueFromValue];
32853287
[suppressAnyCovariantCastForOptional type.constructorArguments]
32863288
public [type.typeImmutable.simple]([output.linesShortable][for v in type.constructorArguments][if not for.first],[/if]
32873289
[v.constructorParameterAnnotations][v.atNullability][constructorAcceptTypeInvokable v] [v.name][/for][/output.linesShortable]) {
3290+
[else if not type.constructorArguments]
3291+
[if type.style.protectedNoargConstructor]protected[else]private[/if] [type.typeImmutable.simple]() {
32883292
[else]
32893293
[suppressAnyCovariantCastForOptional type.constructorArguments]
32903294
private [type.typeImmutable.simple]([output.linesShortable][for v in type.constructorArguments][if not for.first],[/if]

value-processor/src/org/immutables/value/processor/meta/ValueType.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,11 @@ public boolean isUseJavaValidationApi() {
10951095
}
10961096

10971097
public boolean isGenerateNoargConstructor() {
1098-
return style().privateNoargConstructor()
1099-
|| style().protectedNoargConstructor();
1098+
return (style().privateNoargConstructor()
1099+
|| style().protectedNoargConstructor())
1100+
// this special case is when we have just empty constructor we don't want to
1101+
// collide with
1102+
&& !(isUseConstructor() && getConstructorArguments().isEmpty());
11001103
}
11011104

11021105
private @Nullable ThrowForInvalidImmutableState throwForInvalidImmutableState;
@@ -1364,7 +1367,8 @@ private boolean allAttributesSupportsThis() {
13641367

13651368
public boolean isGenerateBuilderConstructor() {
13661369
return isUseBuilder()
1367-
&& !(isUseSingleton() && settableAttributes.isEmpty())
1370+
&& !(isUseSingleton() && getSettableAttributes().isEmpty())
1371+
&& !(isUseConstructor() && getConstructorArguments().isEmpty())
13681372
&& !isGenerateBuilderUseCopyConstructor();
13691373
}
13701374

0 commit comments

Comments
 (0)