• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 Google LLC
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 package com.google.auto.value.processor;
17 
18 import com.google.auto.value.processor.PropertyBuilderClassifier.PropertyBuilder;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.collect.ImmutableMultimap;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.escapevelocity.Template;
24 import java.util.Optional;
25 import javax.annotation.processing.ProcessingEnvironment;
26 import javax.lang.model.util.Types;
27 
28 /**
29  * The variables to substitute into the autovalue.vm template.
30  *
31  * @author emcmanus@google.com (Éamonn McManus)
32  */
33 @SuppressWarnings("unused") // the fields in this class are only read via reflection
34 class AutoValueTemplateVars extends AutoValueOrOneOfTemplateVars {
35   /**
36    * The properties defined by the parent class's abstract methods. The elements of this set are in
37    * the same order as the original abstract method declarations in the AutoValue class.
38    */
39   ImmutableSet<AutoValueOrOneOfProcessor.Property> props;
40 
41   /**
42    * Whether to include identifiers in strings in the generated code. If false, exception messages
43    * will not mention properties by name, and {@code toString()} will include neither property names
44    * nor the name of the {@code @AutoValue} class.
45    */
46   Boolean identifiers;
47 
48   /** The type utilities returned by {@link ProcessingEnvironment#getTypeUtils()}. */
49   Types types;
50 
51   /**
52    * The encoding of the {@code @GwtCompatible} annotation to add to this class, or an empty string
53    * if there is none. A non-empty value will look something like {@code
54    * "@`com.google.common.annotations.GwtCompatible`(serializable = true)"}, where the {@code ``}
55    * represent the encoding used by {@link TypeEncoder}.
56    */
57   String gwtCompatibleAnnotation;
58 
59   /** The text of the serialVersionUID constant, or empty if there is none. */
60   String serialVersionUID;
61 
62   /** The simple name of the generated subclass. */
63   String subclass;
64   /**
65    * The simple name of the final generated subclass. For {@code @AutoValue public static class Foo
66    * {}} this should always be "AutoValue_Foo".
67    */
68   String finalSubclass;
69 
70   /**
71    * True if the generated class should be final (there are no extensions that will generate
72    * subclasses)
73    */
74   Boolean isFinal = false;
75 
76   /**
77    * The modifiers (for example {@code final} or {@code abstract}) for the generated subclass,
78    * followed by a space if they are not empty.
79    */
80   String modifiers;
81 
82   /**
83    * The name of the builder type as it should appear in source code, or empty if there is no
84    * builder type. If class {@code Address} contains {@code @AutoValue.Builder} class Builder then
85    * this will typically be {@code "Address.Builder"}.
86    */
87   String builderTypeName = "";
88 
89   /**
90    * The formal generic signature of the {@code AutoValue.Builder} class. This is empty, or contains
91    * type variables with optional bounds, for example {@code <K, V extends K>}.
92    */
93   String builderFormalTypes = "";
94   /**
95    * The generic signature used by the generated builder subclass for its superclass reference. This
96    * is empty, or contains only type variables with no bounds, for example {@code <K, V>}.
97    */
98   String builderActualTypes = "";
99 
100   /** True if the builder being implemented is an interface, false if it is an abstract class. */
101   Boolean builderIsInterface = false;
102 
103   /**
104    * The full spelling of any annotations to add to the generated builder subclass, or an empty list
105    * if there are none. A non-empty value might look something like {@code
106    * @`java.lang.SuppressWarnings`("Immutable")}. The {@code ``} marks are explained in
107    * {@link TypeEncoder}.
108    */
109   ImmutableList<String> builderAnnotations = ImmutableList.of();
110 
111   /** The builder's build method, often {@code "build"}. */
112   Optional<SimpleMethod> buildMethod = Optional.empty();
113 
114   /**
115    * A multimap from property names (like foo) to the corresponding setters. The same property may
116    * be set by more than one setter. For example, an ImmutableList might be set by {@code
117    * setFoo(ImmutableList<String>)} and {@code setFoo(String[])}.
118    */
119   ImmutableMultimap<String, BuilderSpec.PropertySetter> builderSetters = ImmutableMultimap.of();
120 
121   /**
122    * A map from property names to information about the associated property builder. A property
123    * called foo (defined by a method foo() or getFoo()) can have a property builder called
124    * fooBuilder(). The type of foo must be a type that has an associated builder following certain
125    * conventions. Guava immutable types such as ImmutableList follow those conventions, as do many
126    * {@code @AutoValue} types.
127    */
128   ImmutableMap<String, PropertyBuilder> builderPropertyBuilders = ImmutableMap.of();
129 
130   /**
131    * Properties that are required to be set. A property must be set explicitly except in the
132    * following cases:
133    *
134    * <ul>
135    *   <li>it is {@code @Nullable} (in which case it defaults to null);
136    *   <li>it is {@code Optional} (in which case it defaults to empty);
137    *   <li>it has a property-builder method (in which case it defaults to empty).
138    * </ul>
139    */
140   ImmutableSet<AutoValueProcessor.Property> builderRequiredProperties = ImmutableSet.of();
141 
142   /**
143    * A map from property names to information about the associated property getter. A property
144    * called foo (defined by a method foo() or getFoo()) can have a property getter method with the
145    * same name (foo() or getFoo()) and either the same return type or an Optional (or OptionalInt,
146    * etc) wrapping it.
147    */
148   ImmutableMap<String, BuilderSpec.PropertyGetter> builderGetters = ImmutableMap.of();
149 
150   /** Any {@code toBuilder()} methods, that is methods that return the builder type. */
151   ImmutableList<SimpleMethod> toBuilderMethods;
152 
153   private static final Template TEMPLATE = parsedTemplateForResource("autovalue.vm");
154 
155   @Override
parsedTemplate()156   Template parsedTemplate() {
157     return TEMPLATE;
158   }
159 }
160