|
16 | 16 | package org.inferred.freebuilder.processor;
|
17 | 17 |
|
18 | 18 | import static com.google.common.base.MoreObjects.firstNonNull;
|
| 19 | +import static javax.lang.model.type.TypeKind.DECLARED; |
19 | 20 | import static org.inferred.freebuilder.processor.BuilderMethods.getter;
|
20 | 21 | import static org.inferred.freebuilder.processor.BuilderMethods.mapper;
|
21 | 22 | import static org.inferred.freebuilder.processor.BuilderMethods.setter;
|
| 23 | +import static org.inferred.freebuilder.processor.util.ObjectsExcerpts.Nullability.NULLABLE; |
22 | 24 | import static org.inferred.freebuilder.processor.util.feature.FunctionPackage.FUNCTION_PACKAGE;
|
23 | 25 |
|
24 | 26 | import com.google.common.annotations.VisibleForTesting;
|
|
29 | 31 | import org.inferred.freebuilder.processor.PropertyCodeGenerator.Config;
|
30 | 32 | import org.inferred.freebuilder.processor.util.Block;
|
31 | 33 | import org.inferred.freebuilder.processor.util.Excerpt;
|
| 34 | +import org.inferred.freebuilder.processor.util.Excerpts; |
| 35 | +import org.inferred.freebuilder.processor.util.ObjectsExcerpts; |
32 | 36 | import org.inferred.freebuilder.processor.util.ParameterizedType;
|
33 | 37 | import org.inferred.freebuilder.processor.util.PreconditionExcerpts;
|
34 | 38 | import org.inferred.freebuilder.processor.util.SourceBuilder;
|
@@ -164,12 +168,34 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
|
164 | 168 |
|
165 | 169 | @Override
|
166 | 170 | public void addMergeFromValue(Block code, String value) {
|
167 |
| - code.addLine("%s(%s.%s());", setter(property), value, property.getGetterName()); |
| 171 | + Excerpt defaults = Declarations.freshBuilder(code, metadata).orNull(); |
| 172 | + if (defaults != null) { |
| 173 | + code.addLine("if (%s) {", ObjectsExcerpts.notEquals( |
| 174 | + Excerpts.add("%s.%s()", value, property.getGetterName()), |
| 175 | + Excerpts.add("%s.%s()", defaults, getter(property)), |
| 176 | + DECLARED, |
| 177 | + NULLABLE)); |
| 178 | + } |
| 179 | + code.addLine(" %s(%s.%s());", setter(property), value, property.getGetterName()); |
| 180 | + if (defaults != null) { |
| 181 | + code.addLine("}"); |
| 182 | + } |
168 | 183 | }
|
169 | 184 |
|
170 | 185 | @Override
|
171 | 186 | public void addMergeFromBuilder(Block code, String builder) {
|
172 |
| - code.addLine("%s(%s.%s());", setter(property), builder, getter(property)); |
| 187 | + Excerpt defaults = Declarations.freshBuilder(code, metadata).orNull(); |
| 188 | + if (defaults != null) { |
| 189 | + code.addLine("if (%s) {", ObjectsExcerpts.notEquals( |
| 190 | + Excerpts.add("%s.%s()", builder, getter(property)), |
| 191 | + Excerpts.add("%s.%s()", defaults, getter(property)), |
| 192 | + DECLARED, |
| 193 | + NULLABLE)); |
| 194 | + } |
| 195 | + code.addLine(" %s(%s.%s());", setter(property), builder, getter(property)); |
| 196 | + if (defaults != null) { |
| 197 | + code.addLine("}"); |
| 198 | + } |
173 | 199 | }
|
174 | 200 |
|
175 | 201 | @Override
|
|
0 commit comments