Skip to content

Commit

Permalink
Make JavaBean attributes nullable by default (except for primitives /…
Browse files Browse the repository at this point in the history
… optionals / collections)

Optional* type of matchers will be selected by default for JavaBeans
  • Loading branch information
asereda-gs committed Oct 19, 2019
1 parent 18bef01 commit 0840420
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
32 changes: 32 additions & 0 deletions criteria/common/test/org/immutables/criteria/javabean/DepBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 Immutables Authors and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.immutables.criteria.javabean;

import org.immutables.criteria.Criteria;

@Criteria
public class DepBean {
private String foo;

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class JavaBean1 extends AbstractBean {

private int int1;

private Integer boxedInteger;

private DepBean depBean;

@Criteria.Id
private String string1;

Expand All @@ -41,4 +45,20 @@ public String getString1() {
public void setString1(String string1) {
this.string1 = string1;
}

public DepBean getDepBean() {
return depBean;
}

public void setDepBean(DepBean depBean) {
this.depBean = depBean;
}

public Integer getBoxedInteger() {
return boxedInteger;
}

public void setBoxedInteger(Integer boxedInteger) {
this.boxedInteger = boxedInteger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ public void nullable() {
check(findFn.apply("nullableDep").isNullable());
}

/**
* JavaBean(s) have nullable types by default (except for primitives / optionals / iterables)
*/
@Test
public void nullableByDefault() {
ValueType valueType = rule.value(Model1.class);
Function<String, ValueAttribute> findFn = name -> valueType.attributes.stream().filter(a -> a.name().equals(name)).findAny().get();

// TODO: generate optional version of criteria ?
check(findFn.apply("dep").isNullable());

// boxed versions are nullable
check(findFn.apply("boxedInteger").isNullable());

// base is string (and can be null)
check(findFn.apply("base").isNullable());

// exclude nullable property from primitives / collections and optionals
// foo is int (primitive)
check(!findFn.apply("foo").isNullable());
// set is boolean
check(!findFn.apply("set").isNullable());
// deps is collection
check(!findFn.apply("deps").isNullable());
}

@Test
public void visibility() {
ValueType valueType = rule.value(VisibilityModel.class);
Expand Down Expand Up @@ -131,6 +157,8 @@ static class Model1 extends Base {
private int foo;
private List<Dep> deps;

private Integer boxedInteger;

private Dep dep;

@Nullable
Expand Down Expand Up @@ -190,6 +218,14 @@ public void setDep(Dep dep) {
public void setNullableDep(Dep nullableDep) {
this.nullableDep = nullableDep;
}

public Integer getBoxedInteger() {
return boxedInteger;
}

public void setBoxedInteger(Integer boxedInteger) {
this.boxedInteger = boxedInteger;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,7 @@ void initAndValidate(@Nullable InstantiationCreator instantiationCreator) {
}

initAttributeBuilder();
maybeInitJavaBean();
}

private Set<String> thrownCheckedExceptions = ImmutableSet.of();
Expand Down Expand Up @@ -1672,6 +1673,25 @@ private void initBuilderParamsIfApplicable() {
}
}

/**
* Init (or override) internal attributes specific to JavaBeans
*/
private void maybeInitJavaBean() {
if (!containingType.kind().isJavaBean()) {
return;
}

// JavaBeans have nullable attributes by default (except for primitives / optionals / collections / criteria)
// allow only scalar types to be nullable (by default) for JavaBeans
// override nullability if not set
if (this.nullability == null
&& !isPrimitive()
&& !isOptionalType()
&& !isCollectionType()) {
this.nullability = NullabilityAnnotationInfo.forTypeUse();
}
}

@Nullable
public SwitcherModel builderSwitcherModel;
public boolean isBuilderParameter;
Expand Down

0 comments on commit 0840420

Please sign in to comment.