Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf04627

Browse files
committed
Make JavaBean attributes nullable by default (except for primitives / optionals / collections)
Optional* type of matchers will be selected by default for JavaBeans
1 parent18bef01 commitbf04627

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019 Immutables Authors and Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
packageorg.immutables.criteria.javabean;
18+
19+
importorg.immutables.criteria.Criteria;
20+
21+
@Criteria
22+
publicclassDepBean {
23+
privateStringfoo;
24+
25+
publicStringgetFoo() {
26+
returnfoo;
27+
}
28+
29+
publicvoidsetFoo(Stringfoo) {
30+
this.foo =foo;
31+
}
32+
}

‎criteria/common/test/org/immutables/criteria/javabean/JavaBean1.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public class JavaBean1 extends AbstractBean {
2323

2424
privateintint1;
2525

26+
privateIntegerboxedInteger;
27+
28+
privateDepBeandepBean;
29+
2630
@Criteria.Id
2731
privateStringstring1;
2832

@@ -41,4 +45,20 @@ public String getString1() {
4145
publicvoidsetString1(Stringstring1) {
4246
this.string1 =string1;
4347
}
48+
49+
publicDepBeangetDepBean() {
50+
returndepBean;
51+
}
52+
53+
publicvoidsetDepBean(DepBeandepBean) {
54+
this.depBean =depBean;
55+
}
56+
57+
publicIntegergetBoxedInteger() {
58+
returnboxedInteger;
59+
}
60+
61+
publicvoidsetBoxedInteger(IntegerboxedInteger) {
62+
this.boxedInteger =boxedInteger;
63+
}
4464
}

‎criteria/common/test/org/immutables/criteria/processor/JavaBeanModelTest.java‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void javaBean() {
4646

4747
List<String>attributes =valueType.allMarshalingAttributes().stream().map(ValueAttribute::name).collect(Collectors.toList());
4848

49-
check(attributes).hasContentInAnyOrder("set","foo","base","dep","deps","nullableDep");
49+
check(attributes).hasContentInAnyOrder("set","foo","base","dep","deps","nullableDep","boxedInteger");
5050
// java.lang.Object should not be included
5151
check(attributes).not().hasContentInAnyOrder("equals","hashCode","toString","getClass");
5252
check(attributes).not().hasContentInAnyOrder("ignore","missingField");
@@ -71,6 +71,32 @@ public void nullable() {
7171
check(findFn.apply("nullableDep").isNullable());
7272
}
7373

74+
/**
75+
* JavaBean(s) have nullable types by default (except for primitives / optionals / iterables)
76+
*/
77+
@Test
78+
publicvoidnullableByDefault() {
79+
ValueTypevalueType =rule.value(Model1.class);
80+
Function<String,ValueAttribute>findFn =name ->valueType.attributes.stream().filter(a ->a.name().equals(name)).findAny().get();
81+
82+
// TODO: generate optional version of criteria ?
83+
check(findFn.apply("dep").isNullable());
84+
85+
// boxed versions are nullable
86+
check(findFn.apply("boxedInteger").isNullable());
87+
88+
// base is string (and can be null)
89+
check(findFn.apply("base").isNullable());
90+
91+
// exclude nullable property from primitives / collections and optionals
92+
// foo is int (primitive)
93+
check(!findFn.apply("foo").isNullable());
94+
// set is boolean
95+
check(!findFn.apply("set").isNullable());
96+
// deps is collection
97+
check(!findFn.apply("deps").isNullable());
98+
}
99+
74100
@Test
75101
publicvoidvisibility() {
76102
ValueTypevalueType =rule.value(VisibilityModel.class);
@@ -131,6 +157,8 @@ static class Model1 extends Base {
131157
privateintfoo;
132158
privateList<Dep>deps;
133159

160+
privateIntegerboxedInteger;
161+
134162
privateDepdep;
135163

136164
@Nullable
@@ -190,6 +218,14 @@ public void setDep(Dep dep) {
190218
publicvoidsetNullableDep(DepnullableDep) {
191219
this.nullableDep =nullableDep;
192220
}
221+
222+
publicIntegergetBoxedInteger() {
223+
returnboxedInteger;
224+
}
225+
226+
publicvoidsetBoxedInteger(IntegerboxedInteger) {
227+
this.boxedInteger =boxedInteger;
228+
}
193229
}
194230

195231
/**

‎value-processor/src/org/immutables/value/processor/meta/ValueAttribute.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,7 @@ void initAndValidate(@Nullable InstantiationCreator instantiationCreator) {
11781178
}
11791179

11801180
initAttributeBuilder();
1181+
maybeInitJavaBean();
11811182
}
11821183

11831184
privateSet<String>thrownCheckedExceptions =ImmutableSet.of();
@@ -1672,6 +1673,25 @@ private void initBuilderParamsIfApplicable() {
16721673
}
16731674
}
16741675

1676+
/**
1677+
* Init (or override) internal attributes specific to JavaBeans
1678+
*/
1679+
privatevoidmaybeInitJavaBean() {
1680+
if (!containingType.kind().isJavaBean()) {
1681+
return;
1682+
}
1683+
1684+
// JavaBeans have nullable attributes by default (except for primitives / optionals / collections / criteria)
1685+
// allow only scalar types to be nullable (by default) for JavaBeans
1686+
// override nullability if not set
1687+
if (this.nullability ==null
1688+
&& !isPrimitive()
1689+
&& !isOptionalType()
1690+
&& !isCollectionType()) {
1691+
this.nullability =NullabilityAnnotationInfo.forTypeUse();
1692+
}
1693+
}
1694+
16751695
@Nullable
16761696
publicSwitcherModelbuilderSwitcherModel;
16771697
publicbooleanisBuilderParameter;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp