Skip to content

Commit c4597ef

Browse files
committed
#1483 strictModifiable=false
1 parent e07209c commit c4597ef

7 files changed

Lines changed: 40 additions & 5 deletions

File tree

value-annotations/src/org/immutables/value/Value.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@
635635
String underrideToString() default "";
636636

637637
/**
638-
* Delegates {@code toString} implementation completely to a fully qualified path to a method name, example {@code delegateToString="com.whatever.packg.ToStringer.strigify"}. The path will be used literally in generated code, and a single parameter will be passed to it, {@code this} immutable object instance.
638+
* Delegates {@code toString} implementation completely to a fully qualified path to a method name, example {@code delegateToString="com.whatever.packg.ToStringer.stringify"}. The path will be used literally in generated code, and a single parameter will be passed to it, {@code this} immutable object instance.
639639
* <p><em>Note: If specified, it will take precedence over any other {@code toString} customization mechanism</em>
640640
* @return fully qualified static method name, if empty (by default) will not be used
641641
*/
@@ -839,6 +839,14 @@
839839
*/
840840
boolean strictBuilder() default false;
841841

842+
/**
843+
* Strict modifiable will refuse any accessor value (by throwing {@link IllegalStateException})
844+
* which is mandatory. Enabled by default. Set it to {@code false} and it will allow to get
845+
* current field value even if not initialized ({@code null} for references, {@code 0}, {@code false} &mdash; for primitives).
846+
* @return default is {@code true}, enabling strict modifiable
847+
*/
848+
boolean strictModifiable() default true;
849+
842850
/**
843851
* When {@code true} &mdash; disables check that all required attributes have been provided to a
844852
* builder.

value-fixture/src/org/immutables/fixture/modifiable/FromTypesModifiables.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ public abstract class FromManyTypes implements Iface {
2626
}
2727

2828
@Value.Immutable
29-
@Value.Style(from = "")
29+
@Value.Style(from = "", strictModifiable = false)
3030
@Value.Modifiable
3131
interface NoFrom {
3232
int a();
33+
String b();
3334
class Builder extends ImmutableNoFrom.Builder {}
3435
}
3536
}

value-fixture/test/org/immutables/fixture/modifiable/ModifiablesTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,19 @@ public void nullableMap() {
355355
check(nlm).is(nlm2);
356356
nlm2.clear(); // no NPE
357357
}
358+
359+
@Test void nonStrict() {
360+
ModifiableNoFrom m = ModifiableNoFrom.create();
361+
check(!m.isInitialized());
362+
check(!m.aIsSet());
363+
check(!m.bIsSet());
364+
check(m.a()).is(0);
365+
check(m.b()).isNull();
366+
try {
367+
m.toImmutable();
368+
check(false);
369+
} catch (IllegalStateException e) {
370+
check(true);
371+
}
372+
}
358373
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,9 @@ public [thisReturnType type] [type.names.from]([type.typeModifiable] instance) {
500500
@Override
501501
[v.access][if not v.beanFriendlyModifiable]final [/if][v.atNullability][v.implementationModifiableType] [v.names.get]() {
502502
[if v.mandatory]
503-
if (![isSet v]()) {[-- opportunistically reuse attributes check --]
504-
[disambiguateAccessor type 'checkRequiredAttributes']();
505-
}
503+
[if type.useStrictModifiable][-- opportunistically reuse attributes check --]
504+
if (![isSet v]()) [disambiguateAccessor type 'checkRequiredAttributes']();
505+
[/if]
506506
[/if]
507507
[if v.generateLazy]
508508
[if v.attributeValueKindModifyFrom]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ public Class<? extends Annotation> annotationType() {
201201
@Override
202202
public abstract boolean strictBuilder();
203203

204+
@Value.Parameter
205+
@Override
206+
public abstract boolean strictModifiable();
207+
204208
@Value.Parameter
205209
@Override
206210
public abstract ValidationMethod validationMethod();
@@ -504,6 +508,7 @@ static StyleInfo infoFrom(StyleMirror input) {
504508
input.packageGenerated(),
505509
ToImmutableInfo.FUNCTION.apply(input.defaults()),
506510
input.strictBuilder(),
511+
input.strictModifiable(),
507512
input.validationMethod(),
508513
input.allParameters(),
509514
input.defaultAsDefault(),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ private ValueMirrors() {}
177177

178178
boolean strictBuilder() default false;
179179

180+
boolean strictModifiable() default true;
181+
180182
ValidationMethod validationMethod() default ValidationMethod.SIMPLE;
181183

182184
boolean allParameters() default false;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,10 @@ public boolean isUseStrictBuilder() {
10551055
|| style().stagedBuilder();
10561056
}
10571057

1058+
public boolean isUseStrictModifiable() {
1059+
return style().strictModifiable();
1060+
}
1061+
10581062
public boolean isUseJavaValidationApi() {
10591063
return style().validationMethod() == ValueMirrors.Style.ValidationMethod.VALIDATION_API;
10601064
}

0 commit comments

Comments
 (0)