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