Skip to content

Commit

Permalink
#1483 strictModifiable=false
Browse files Browse the repository at this point in the history
  • Loading branch information
elucash committed Sep 24, 2023
1 parent e07209c commit c4597ef
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
10 changes: 9 additions & 1 deletion value-annotations/src/org/immutables/value/Value.java
Expand Up @@ -635,7 +635,7 @@
String underrideToString() default "";

/**
* 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.
* 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.
* <p><em>Note: If specified, it will take precedence over any other {@code toString} customization mechanism</em>
* @return fully qualified static method name, if empty (by default) will not be used
*/
Expand Down Expand Up @@ -839,6 +839,14 @@
*/
boolean strictBuilder() default false;

/**
* Strict modifiable will refuse any accessor value (by throwing {@link IllegalStateException})
* which is mandatory. Enabled by default. Set it to {@code false} and it will allow to get
* current field value even if not initialized ({@code null} for references, {@code 0}, {@code false} &mdash; for primitives).
* @return default is {@code true}, enabling strict modifiable
*/
boolean strictModifiable() default true;

/**
* When {@code true} &mdash; disables check that all required attributes have been provided to a
* builder.
Expand Down
Expand Up @@ -26,10 +26,11 @@ public abstract class FromManyTypes implements Iface {
}

@Value.Immutable
@Value.Style(from = "")
@Value.Style(from = "", strictModifiable = false)
@Value.Modifiable
interface NoFrom {
int a();
String b();
class Builder extends ImmutableNoFrom.Builder {}
}
}
Expand Up @@ -355,4 +355,19 @@ public void nullableMap() {
check(nlm).is(nlm2);
nlm2.clear(); // no NPE
}

@Test void nonStrict() {
ModifiableNoFrom m = ModifiableNoFrom.create();
check(!m.isInitialized());
check(!m.aIsSet());
check(!m.bIsSet());
check(m.a()).is(0);
check(m.b()).isNull();
try {
m.toImmutable();
check(false);
} catch (IllegalStateException e) {
check(true);
}
}
}
Expand Up @@ -500,9 +500,9 @@ public [thisReturnType type] [type.names.from]([type.typeModifiable] instance) {
@Override
[v.access][if not v.beanFriendlyModifiable]final [/if][v.atNullability][v.implementationModifiableType] [v.names.get]() {
[if v.mandatory]
if (![isSet v]()) {[-- opportunistically reuse attributes check --]
[disambiguateAccessor type 'checkRequiredAttributes']();
}
[if type.useStrictModifiable][-- opportunistically reuse attributes check --]
if (![isSet v]()) [disambiguateAccessor type 'checkRequiredAttributes']();
[/if]
[/if]
[if v.generateLazy]
[if v.attributeValueKindModifyFrom]
Expand Down
Expand Up @@ -201,6 +201,10 @@ public Class<? extends Annotation> annotationType() {
@Override
public abstract boolean strictBuilder();

@Value.Parameter
@Override
public abstract boolean strictModifiable();

@Value.Parameter
@Override
public abstract ValidationMethod validationMethod();
Expand Down Expand Up @@ -504,6 +508,7 @@ static StyleInfo infoFrom(StyleMirror input) {
input.packageGenerated(),
ToImmutableInfo.FUNCTION.apply(input.defaults()),
input.strictBuilder(),
input.strictModifiable(),
input.validationMethod(),
input.allParameters(),
input.defaultAsDefault(),
Expand Down
Expand Up @@ -177,6 +177,8 @@ private ValueMirrors() {}

boolean strictBuilder() default false;

boolean strictModifiable() default true;

ValidationMethod validationMethod() default ValidationMethod.SIMPLE;

boolean allParameters() default false;
Expand Down
Expand Up @@ -1055,6 +1055,10 @@ public boolean isUseStrictBuilder() {
|| style().stagedBuilder();
}

public boolean isUseStrictModifiable() {
return style().strictModifiable();
}

public boolean isUseJavaValidationApi() {
return style().validationMethod() == ValueMirrors.Style.ValidationMethod.VALIDATION_API;
}
Expand Down

0 comments on commit c4597ef

Please sign in to comment.