aboutsummaryrefslogtreecommitdiff
path: root/value/src/main/java/com/google/auto/value/processor/builder.vm
blob: 630330cafa056c981111c6488527901d5144fd64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
## Copyright 2014 Google LLC
##
## 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.
##
##
##
## Template for AutoValue and AutoBuilder builders.
## This template uses the Apache Velocity Template Language (VTL).
## The variables ($isFinal, $props, and so on) are defined by the fields of AutoValueOrBuilderTemplateVars.
##
## Comments, like this one, begin with ##. The comment text extends up to and including the newline
## character at the end of the line. So comments also serve to join a line to the next one.
## Velocity deletes a newline after a directive (#if, #foreach, #end etc) so ## is not needed there.
## That does mean that we sometimes need an extra blank line after such a directive.
##
## Post-processing will remove unwanted spaces and blank lines, but will not join two lines.
## It will also replace classes spelled as (e.g.) `java.util.Arrays`, with the backquotes, to
## use just Arrays if that class can be imported unambiguously, or java.util.Arrays if not.
##
#foreach ($a in $builderAnnotations)
$a
#end
#if (!$autoBuilder) static #end##
#if ($isFinal) final #end##
class ${builderName}${builderFormalTypes} ##
#if ($builderIsInterface) implements #else extends #end
    ${builderTypeName}${builderActualTypes} {

#foreach ($p in $props)

  #if ($p.kind.primitive)

  private $types.boxedClass($p.typeMirror).simpleName $p;

  #else

    #if ($builderPropertyBuilders[$p.name])
    ## If you have ImmutableList.Builder<String> stringsBuilder() then we define two fields:
    ## private ImmutableList.Builder<String> stringsBuilder$;
    ## private ImmutableList<String> strings;

  private ${builderPropertyBuilders[$p.name].builderType} ##
      ${builderPropertyBuilders[$p.name].name};

    #end

  private $p.type $p #if ($p.optional && !$p.nullable) = $p.optional.empty #end ;

  #end
#end

  ${builderName}() {
  }

#if ($toBuilderConstructor)

  private ${builderName}(${origClass}${actualTypes} source) {

  #foreach ($p in $props)

    this.$p = source.${p.getter}();

  #end

  }

#end

#foreach ($p in $props)

  ## The following is either null or an instance of PropertyBuilderClassifier.PropertyBuilder
  #set ($propertyBuilder = $builderPropertyBuilders[$p.name])

  ## Setter and/or property builder

  #foreach ($setter in $builderSetters[$p.name])

  @`java.lang.Override`
  ${setter.access}${builderTypeName}${builderActualTypes} ##
      ${setter.name}(${setter.nullableAnnotation}$setter.parameterType $p) {

    ## Omit null check for primitive, or @Nullable, or if we are going to be calling a copy method
    ## such as Optional.of, which will have its own null check if appropriate.
    #if (!$setter.primitiveParameter && !$p.nullable && ${setter.copy($p)} == $p)

      #if ($identifiers)

    if ($p == null) {
      throw new NullPointerException("Null $p.name");
    }
      #else
        ## Just throw NullPointerException with no message if it's null.
        ## The Object cast has no effect on the code but silences an ErrorProne warning.

    ((`java.lang.Object`) ${p}).getClass();
      #end

    #end

    #if ($propertyBuilder)

    if (${propertyBuilder.name} != null) {
      throw new IllegalStateException(#if ($identifiers)"Cannot set $p after calling ${p.name}Builder()"#end);
    }

    #end

    this.$p = ${setter.copy($p)};
    return this;
  }

  #end

  #if ($propertyBuilder)

  @`java.lang.Override`
  ${propertyBuilder.access}$propertyBuilder.builderType ${p.name}Builder($propertyBuilder.propertyBuilderMethodParameters) {
    if (${propertyBuilder.name} == null) {

      ## This is the first time someone has asked for the builder. If the property it sets already
      ## has a value (because it came from a toBuilder() call on the AutoValue class, or because
      ## there is also a setter for this property) then we copy that value into the builder.
      ## Otherwise the builder starts out empty.
      ## If we have neither a setter nor a toBuilder() method, then the builder always starts
      ## off empty.

      #if ($builderSetters[$p.name].empty && $toBuilderMethods.empty)

      ${propertyBuilder.name} = ${propertyBuilder.initializer};

      #else

      if ($p == null) {
        ${propertyBuilder.name} = ${propertyBuilder.initializer};
      } else {

        #if (${propertyBuilder.builtToBuilder})

        ${propertyBuilder.name} = ${p}.${propertyBuilder.builtToBuilder}();

        #else

        ${propertyBuilder.name} = ${propertyBuilder.initializer};
        ${propertyBuilder.name}.${propertyBuilder.copyAll}($p);

        #end

        $p = null;
      }

      #end

    } #if (!$propertyBuilder.propertyBuilderMethodParameters.empty) else {
        ## This check only happens if the property-builder method has a parameter.
        ## We don't know if the existing builder was created with the same parameter,
        ## so we throw to avoid possibly giving you a builder that is different from
        ## the one you asked for.

      throw new IllegalStateException("Property builder for $p.name is already defined");
    }
      #end

    return $propertyBuilder.name;
  }

  #end

  ## Getter

  #if ($builderGetters[$p.name])

  @`java.lang.Override`
  ${p.nullableAnnotation}${builderGetters[$p.name].access}$builderGetters[$p.name].type ${p.getter}() {
    #if ($builderGetters[$p.name].optional)

    if ($p == null) {
      return $builderGetters[$p.name].optional.empty;
    } else {
      return ${builderGetters[$p.name].optional.rawType}.of($p);
    }

    #else
      #if ($builderRequiredProperties.contains($p))

    if ($p == null) {
      throw new IllegalStateException(#if ($identifiers)"Property \"$p.name\" has not been set"#end);
    }

      #end

      #if ($propertyBuilder)

    if (${propertyBuilder.name} != null) {
      return ${propertyBuilder.name}.build();
    }
    if ($p == null) {
      ${propertyBuilder.beforeInitDefault}
      $p = ${propertyBuilder.initDefault};
    }

      #end

    return $p;

    #end

  }

  #end
#end

## build() method

  @`java.lang.Override`
  ${buildMethod.get().access}${builtType} ${buildMethod.get().name}() ${buildMethod.get().throws} {

#foreach ($p in $props)
  #set ($propertyBuilder = $builderPropertyBuilders[$p.name])
  #if ($propertyBuilder)

    if (${propertyBuilder.name} != null) {
      this.$p = ${propertyBuilder.name}.build();
    } else if (this.$p == null) {
      ${propertyBuilder.beforeInitDefault}
      this.$p = ${propertyBuilder.initDefault};
    }

  #end
#end

#if (!$builderRequiredProperties.empty)
    if (#foreach ($p in $builderRequiredProperties)##
        this.$p == null##
          #if ($foreach.hasNext)

        || #end
        #end) {

  #if ($identifiers)  ## build a friendly message showing all missing properties
    #if ($builderRequiredProperties.size() == 1)

    `java.lang.String` missing = " $builderRequiredProperties.iterator().next()";

    #else

    `java.lang.StringBuilder` missing = new `java.lang.StringBuilder`();

      #foreach ($p in $builderRequiredProperties)

    if (this.$p == null) {
      missing.append(" $p.name");
    }

      #end
    #end

    throw new IllegalStateException("Missing required properties:" + missing);

  #else  ## just throw an exception if anything is missing

    throw new IllegalStateException();

  #end

  }

#end

    #if ($builtType != "void") return #end ${build}(
#foreach ($p in $props)

        this.$p #if ($foreach.hasNext) , #end
#end );
  }
}