aboutsummaryrefslogtreecommitdiff
path: root/value/src/main/java/com/google/auto/value/processor/autoannotation.vm
blob: 1d14d3fbd6658839a769507b906344846e174e66 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
## 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 each generated AutoAnnotation_Foo_bar class.
## This template uses the Apache Velocity Template Language (VTL).
## The variables ($pkg, $props, and so on) are defined by the fields of AutoAnnotationTemplateVars.
##
## 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.

#macro (cloneArray $a)
  #if ($gwtCompatible)
    `java.util.Arrays`.copyOf($a, ${a}.length)
  #else
    ${a}.clone()
  #end
#end

#if (!$pkg.empty)
package $pkg;
#end

## The following line will be replaced by the required imports during post-processing.
`import`

#if (!$generated.empty)
@${generated}("com.google.auto.value.processor.AutoAnnotationProcessor")
#else
// Generated by com.google.auto.value.processor.AutoAnnotationProcessor
#end
final class $className implements $annotationName {

## Fields

#foreach ($m in $members)
  #if ($params.containsKey($m.toString()))

  private final $m.type $m;

  #else

  private static final $m.type $m = $m.defaultValue;

  #end
#end

## Constructor

  $className(
#foreach ($p in $params.keySet())

      $params[$p].type $members[$p] #if ($foreach.hasNext) , #end
#end ) {
#foreach ($p in $params.keySet())
  #if (!$members[$p].kind.primitive)

    if ($p == null) {
      throw new NullPointerException("Null $p");
    }

  #end

  #if ($members[$p].kind == "ARRAY")
    #if ($params[$p].kind == "ARRAY")

    this.$p = #cloneArray(${p});

    #elseif ($members[$p].typeMirror.componentType.kind.primitive)

    this.$p = ${members[$p].typeMirror.componentType}ArrayFromCollection($p);

    #elseif ($members[$p].arrayOfClassWithBounds)

    @SuppressWarnings({"unchecked", "rawtypes"})
    ${members[$p].componentType}[] ${p}$ = ${p}.toArray(new Class[0]);
    this.$p = ${p}$;

    #else

    this.$p = ${p}.toArray(new ${members[$p].componentType}[0]);

    #end
  #else

    this.$p = $p;

  #end
#end

  }

## annotationType method (defined by the Annotation interface)

  @Override
  public Class<? extends $annotationName> annotationType() {
    return ${annotationName}.class;
  }

## Member getters

#foreach ($m in $members)

  @Override
  public ${m.type} ${m}() {

  #if ($m.kind == "ARRAY")

    return #cloneArray(${m});

  #else

    return ${m};

  #end

  }

#end

## toString

#macro (appendMemberString $m)
  #if ($m.typeMirror.toString() == "java.lang.String")
    #set ($appendQuotedStringMethod = "true")

    appendQuoted(sb, $m) ##
  #elseif ($m.typeMirror.toString() == "char")
    #set ($appendQuotedCharMethod = "true")

    appendQuoted(sb, $m) ##
  #elseif ($m.typeMirror.toString() == "java.lang.String[]")
    #set ($appendQuotedStringArrayMethod = "true")

    appendQuoted(sb, $m) ##
  #elseif ($m.typeMirror.toString() == "char[]")
    #set ($appendQuotedCharArrayMethod = "true")

    appendQuoted(sb, $m) ##
  #elseif ($m.kind == "ARRAY")

    sb.append(`java.util.Arrays`.toString($m)) ##
  #else

    sb.append($m) ##
  #end
#end

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("@$annotationFullName(");

  #foreach ($p in $params.keySet())

    #if ($params.size() > 1 || $params.keySet().iterator().next() != "value")

    sb.append("$p=");
    #end

    #appendMemberString($members[$p]);

    #if ($foreach.hasNext)

    sb.append(", ");
    #end

  #end

    return sb.append(')').toString();
  }

## equals

## An expression that compares `this.something` against `that.something()`.
## It can appear in a chain of && conditions, so if that would cause precedence
## problems the expression needs to be parenthesized.
#macro (memberEqualsThatExpression $m)
  #if ($m.kind == "FLOAT")
    Float.floatToIntBits($m) == Float.floatToIntBits(that.${m}()) ##
  #elseif ($m.kind == "DOUBLE")
    Double.doubleToLongBits($m) == Double.doubleToLongBits(that.${m}()) ##
  #elseif ($m.kind.primitive)
    ($m == that.${m}()) ## parens not strictly needed but avoid confusion when comparing booleans
  #elseif ($m.kind == "ARRAY")
    #if ($params.containsKey($m.toString()))
    `java.util.Arrays`.equals($m,
        (that instanceof $className)
            ? (($className) that).$m
            : that.${m}()) ##
    #else ## default value, so if |that| is also a $className then it has the same constant value
    (that instanceof $className || `java.util.Arrays`.equals($m, that.${m}()))
    #end
  #else
    ${m}.equals(that.${m}()) ##
  #end
#end

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (o instanceof $annotationName) {

  #if ($members.isEmpty())

      return true;

  #else

      $annotationName that = ($annotationName) o;
      return ##
           #foreach ($m in $members)
           #memberEqualsThatExpression ($m)##
             #if ($foreach.hasNext)

           && ##
             #end
           #end
           ;
  #end

    }
    return false;
  }

## hashCode

## An expression that returns the hashCode of `this.something`.
## It appears on the right-hand side of an ^ operator, so if that would cause precedence
## problems the expression needs to be parenthesized.
#macro (memberHashCodeExpression $m)
  #if ($m.kind == "LONG")
    (int) (($m >>> 32) ^ $m) ##
  #elseif ($m.kind == "FLOAT")
    Float.floatToIntBits($m) ##
  #elseif ($m.kind == "DOUBLE")
    (int) ((Double.doubleToLongBits($m) >>> 32) ^ Double.doubleToLongBits($m)) ##
  #elseif ($m.kind == "BOOLEAN")
    ($m ? 1231 : 1237) ##
  #elseif ($m.kind.primitive)
    $m ##
  #elseif ($m.kind == "ARRAY")
    `java.util.Arrays`.hashCode($m) ##
  #else
    ${m}.hashCode() ##
  #end
#end

## The hashCode is the sum of two parts, an invariable part and a variable part. The invariable part
## comes from defaulted members (ones that don't appear in the @AutoAnnotation method parameters)
## whose values have hash codes that never change. (That doesn't include Class constants, for
## example.) We precompute the invariable part, as an optimization but also in order to avoid
## falling afoul of constant-overflow checks in the compiler.

  @Override
  public int hashCode() {
    return
    ## If the invariable part is 0, we avoid outputting `return 0 + ...` just because it generates
    ## unnecessary byte code. But if there are no members then we must say `return 0;` here.
    ## We must write $members.isEmpty() because $members is a Map and Velocity interprets
    ## $members.empty as meaning $members["empty"] in that case.
    #if ($invariableHashSum != 0 || $members.isEmpty())

        $invariableHashSum
        // $invariableHashSum is the contribution from default members $invariableHashes
    #end
    #foreach ($m in $members)
      #if (!$invariableHashes.contains($m.toString()))

        + ($m.nameHash ^ #memberHashCodeExpression($m))
            // $m.nameHash is 127 * "${m}".hashCode()
      #end
    #end

        ;

  }

## support functions

#foreach ($w in $wrapperTypesUsedInCollections)
  #set ($prim = $w.getField("TYPE").get(""))

  private static ${prim}[] ${prim}ArrayFromCollection(`java.util.Collection`<${w.simpleName}> c) {
    ${prim}[] a = new ${prim}[c.size()];
    int i = 0;
    for (${prim} x : c) {
      a[i++] = x;
    }
    return a;
  }
#end

#if ($appendQuotedStringArrayMethod)
  #set ($appendQuotedStringMethod = "true")

  private static void appendQuoted(StringBuilder sb, String[] strings) {
    sb.append('[');
    String sep = "";
    for (String s : strings) {
      sb.append(sep);
      sep = ", ";
      appendQuoted(sb, s);
    }
    sb.append(']');
  }
#end

#if ($appendQuotedCharArrayMethod)
  #set ($appendQuotedCharMethod = "true")

  private static void appendQuoted(StringBuilder sb, char[] chars) {
    sb.append('[');
    String sep = "";
    for (char c : chars) {
      sb.append(sep);
      sep = ", ";
      appendQuoted(sb, c);
    }
    sb.append(']');
  }
#end

#if ($appendQuotedStringMethod)
  #set ($appendEscapedMethod = "true")

  private static void appendQuoted(StringBuilder sb, String s) {
    sb.append('"');
    for (int i = 0; i < s.length(); i++) {
      appendEscaped(sb, s.charAt(i));
    }
    sb.append('"');
  }
#end

#if ($appendQuotedCharMethod)
  #set ($appendEscapedMethod = "true")

  private static void appendQuoted(StringBuilder sb, char c) {
    sb.append('\'');
    appendEscaped(sb, c);
    sb.append('\'');
  }
#end

#if ($appendEscapedMethod)
  private static void appendEscaped(StringBuilder sb, char c) {
    switch (c) {
    case '\\':
    case '"':
    case '\'':
      sb.append('\\').append(c);
      break;
    case '\n':
      sb.append("\\n");
      break;
    case '\r':
      sb.append("\\r");
      break;
    case '\t':
      sb.append("\\t");
      break;
    default:
      if (c < 0x20) {
        sb.append('\\');
        appendWithZeroPadding(sb, Integer.toOctalString(c), 3);
      } else if (c < 0x7f || Character.isLetter(c)) {
        sb.append(c);
      } else {
        sb.append("\\u");
        appendWithZeroPadding(sb, Integer.toHexString(c), 4);
      }
      break;
    }
  }

  ## We use this rather than String.format because that doesn't exist on GWT.

  private static void appendWithZeroPadding(StringBuilder sb, String s, int width) {
    for (int i = width - s.length(); i > 0; i--) {
      sb.append('0');
    }
    sb.append(s);
  }
#end
}