• 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.AutoValueishProcessor.Property;
19 import com.google.auto.value.processor.PropertyBuilderClassifier.PropertyBuilder;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.collect.ImmutableMultimap;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.Optional;
25 import javax.annotation.processing.ProcessingEnvironment;
26 import javax.lang.model.util.Types;
27 
28 /**
29  * Variables to substitute into the autovalue.vm or builder.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 abstract class AutoValueOrBuilderTemplateVars extends AutoValueishTemplateVars {
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<Property> props;
40 
41   /**
42    * The simple name of the generated builder, or empty if there is no builder. This is just
43    * {@code Builder} for AutoValue, since it is nested inside the {@code AutoValue_Foo} class. But
44    * it is {@code AutoBuilder_Foo} for AutoBuilder.
45    */
46   String builderName = "";
47 
48   /**
49    * The name of the builder type as it should appear in source code, or empty if there is no
50    * builder type. If class {@code Address} contains {@code @AutoValue.Builder} class Builder then
51    * this will typically be {@code "Address.Builder"}.
52    */
53   String builderTypeName = "";
54 
55   /**
56    * The formal generic signature of the {@code AutoValue.Builder} class. This is empty, or contains
57    * type variables with optional bounds, for example {@code <K, V extends K>}.
58    */
59   String builderFormalTypes = "";
60 
61   /**
62    * The generic signature used by the generated builder subclass for its superclass reference. This
63    * is empty, or contains only type variables with no bounds, for example {@code <K, V>}.
64    */
65   String builderActualTypes = "";
66 
67   /** True if the builder being implemented is an interface, false if it is an abstract class. */
68   Boolean builderIsInterface = false;
69 
70   /**
71    * The full spelling of any annotations to add to the generated builder subclass, or an empty list
72    * if there are none. A non-empty value might look something like {@code
73    * @`java.lang.SuppressWarnings`("Immutable")}. The {@code ``} marks are explained in
74    * {@link TypeEncoder}.
75    */
76   ImmutableList<String> builderAnnotations = ImmutableList.of();
77 
78   /** The builder's build method, often {@code "build"}. */
79   Optional<SimpleMethod> buildMethod = Optional.empty();
80 
81   /** The type that will be built by the {@code build()} method of a builder. */
82   String builtType;
83 
84   /**
85    * The constructor or method invocation that the {@code build()} method of a builder should use,
86    * without any parameters. This might be {@code "new Foo"} or {@code "Foo.someMethod"}.
87    */
88   String build;
89 
90   /**
91    * A multimap from property names (like foo) to the corresponding setters. The same property may
92    * be set by more than one setter. For example, an ImmutableList might be set by {@code
93    * setFoo(ImmutableList<String>)} and {@code setFoo(String[])}.
94    */
95   ImmutableMultimap<String, BuilderSpec.PropertySetter> builderSetters = ImmutableMultimap.of();
96 
97   /**
98    * A map from property names to information about the associated property builder. A property
99    * called foo (defined by a method foo() or getFoo()) can have a property builder called
100    * fooBuilder(). The type of foo must be a type that has an associated builder following certain
101    * conventions. Guava immutable types such as ImmutableList follow those conventions, as do many
102    * {@code @AutoValue} types.
103    */
104   ImmutableMap<String, PropertyBuilder> builderPropertyBuilders = ImmutableMap.of();
105 
106   /**
107    * Properties that are required to be set. A property must be set explicitly except in the
108    * following cases:
109    *
110    * <ul>
111    *   <li>it is {@code @Nullable} (in which case it defaults to null);
112    *   <li>it has a builder initializer (for example it is {@code Optional}, which will have an
113    *       initializer of {@code Optional.empty()});
114    *   <li>it has a property-builder method (in which case it defaults to empty).
115    * </ul>
116    */
117   ImmutableSet<Property> builderRequiredProperties = ImmutableSet.of();
118 
119   /**
120    * A map from property names to information about the associated property getter. A property
121    * called foo (defined by a method foo() or getFoo()) can have a property getter method with the
122    * same name (foo() or getFoo()) and either the same return type or an Optional (or OptionalInt,
123    * etc) wrapping it.
124    */
125   ImmutableMap<String, BuilderSpec.PropertyGetter> builderGetters = ImmutableMap.of();
126 
127   /**
128    * True if the generated builder should have a second constructor with a parameter of the built
129    * class. The constructor produces a new builder that starts off with the values from the
130    * parameter.
131    */
132   Boolean toBuilderConstructor;
133 
134   /**
135    * Any {@code toBuilder()} methods, that is methods that return the builder type. AutoBuilder does
136    * not currently support this, but it's included in these shared variables to simplify the
137    * template.
138    */
139   ImmutableList<SimpleMethod> toBuilderMethods;
140 
141   /**
142    * Whether to include identifiers in strings in the generated code. If false, exception messages
143    * will not mention properties by name, and {@code toString()} will include neither property names
144    * nor the name of the {@code @AutoValue} class.
145    */
146   Boolean identifiers;
147 
148   /**
149    * True if the generated class should be final (there are no extensions that will generate
150    * subclasses)
151    */
152   Boolean isFinal = false;
153 
154   /** The type utilities returned by {@link ProcessingEnvironment#getTypeUtils()}. */
155   Types types;
156 }
157