aboutsummaryrefslogtreecommitdiff
path: root/value/src/main/java/com/google
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 00:58:09 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 00:58:09 +0000
commit0260f896969dae34df7290fd75132e5d2bc4b2a5 (patch)
treeccd72f5c53dbf7261407709e8cb7b6778d33ea17 /value/src/main/java/com/google
parent558f459f324ee0cc24b2786ddc169b9d087acceb (diff)
parent941c7a94ba9ccee7b4f2b2bfe49d2c87ec7d2b3e (diff)
downloadauto-0260f896969dae34df7290fd75132e5d2bc4b2a5.tar.gz
Change-Id: I0a05330dd86d65f183c1dc9012138248f92ec820
Diffstat (limited to 'value/src/main/java/com/google')
-rw-r--r--value/src/main/java/com/google/auto/value/AutoAnnotation.java33
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AnnotationOutput.java64
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java2
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java85
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoOneOfProcessor.java9
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java3
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java31
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java142
-rw-r--r--value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java7
-rw-r--r--value/src/main/java/com/google/auto/value/processor/BuilderSpec.java34
-rw-r--r--value/src/main/java/com/google/auto/value/processor/GwtCompatibility.java30
-rw-r--r--value/src/main/java/com/google/auto/value/processor/GwtSerialization.java16
-rw-r--r--value/src/main/java/com/google/auto/value/processor/autovalue.vm6
-rw-r--r--value/src/main/java/com/google/auto/value/processor/builder.vm10
14 files changed, 327 insertions, 145 deletions
diff --git a/value/src/main/java/com/google/auto/value/AutoAnnotation.java b/value/src/main/java/com/google/auto/value/AutoAnnotation.java
index d36d8e28..c6fab240 100644
--- a/value/src/main/java/com/google/auto/value/AutoAnnotation.java
+++ b/value/src/main/java/com/google/auto/value/AutoAnnotation.java
@@ -71,6 +71,39 @@ import java.lang.reflect.AnnotatedElement;
* parameter corresponding to an array-valued annotation member, and the implementation of each such
* member will also return a clone of the array.
*
+ * <p>If your annotation has many elements, you may consider using {@code @AutoBuilder} to make it
+ * easier to construct instances. In that case, {@code default} values from the annotation will
+ * become default values for the parameters of the {@code @AutoAnnotation} method. For example:
+ *
+ * <pre>
+ * class Example {
+ * {@code @interface} MyAnnotation {
+ * String name() default "foo";
+ * int number() default 23;
+ * }
+ *
+ * {@code @AutoAnnotation}
+ * static MyAnnotation myAnnotation(String value) {
+ * return new AutoAnnotation_Example_myAnnotation(value);
+ * }
+ *
+ * {@code @AutoBuilder(callMethod = "myAnnotation")}
+ * interface MyAnnotationBuilder {
+ * MyAnnotationBuilder name(String name);
+ * MyAnnotationBuilder number(int number);
+ * MyAnnotation build();
+ * }
+ *
+ * static MyAnnotationBuilder myAnnotationBuilder() {
+ * return new AutoBuilder_Example_MyAnnotationBuilder();
+ * }
+ * }
+ * </pre>
+ *
+ * Here, {@code myAnnotationBuilder().build()} is the same as {@code
+ * myAnnotationBuilder().name("foo").number(23).build()} because those are the defaults in the
+ * annotation definition.
+ *
* @author emcmanus@google.com (Éamonn McManus)
*/
@Target(ElementType.METHOD)
diff --git a/value/src/main/java/com/google/auto/value/processor/AnnotationOutput.java b/value/src/main/java/com/google/auto/value/processor/AnnotationOutput.java
index ed986ab7..ed6abaa6 100644
--- a/value/src/main/java/com/google/auto/value/processor/AnnotationOutput.java
+++ b/value/src/main/java/com/google/auto/value/processor/AnnotationOutput.java
@@ -15,6 +15,8 @@
*/
package com.google.auto.value.processor;
+import com.google.auto.common.MoreTypes;
+import com.google.auto.value.processor.MissingTypes.MissingTypeException;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.util.List;
@@ -24,8 +26,10 @@ import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
import javax.tools.Diagnostic;
@@ -130,13 +134,13 @@ final class AnnotationOutput {
private static class InitializerSourceFormVisitor extends SourceFormVisitor {
private final ProcessingEnvironment processingEnv;
private final String memberName;
- private final Element context;
+ private final Element errorContext;
InitializerSourceFormVisitor(
- ProcessingEnvironment processingEnv, String memberName, Element context) {
+ ProcessingEnvironment processingEnv, String memberName, Element errorContext) {
this.processingEnv = processingEnv;
this.memberName = memberName;
- this.context = context;
+ this.errorContext = errorContext;
}
@Override
@@ -148,7 +152,7 @@ final class AnnotationOutput {
"@AutoAnnotation cannot yet supply a default value for annotation-valued member '"
+ memberName
+ "'",
- context);
+ errorContext);
sb.append("null");
return null;
}
@@ -209,9 +213,9 @@ final class AnnotationOutput {
AnnotationValue annotationValue,
ProcessingEnvironment processingEnv,
String memberName,
- Element context) {
+ Element errorContext) {
SourceFormVisitor visitor =
- new InitializerSourceFormVisitor(processingEnv, memberName, context);
+ new InitializerSourceFormVisitor(processingEnv, memberName, errorContext);
StringBuilder sb = new StringBuilder();
visitor.visit(annotationValue, sb);
return sb.toString();
@@ -222,11 +226,59 @@ final class AnnotationOutput {
* Java source file to reproduce the annotation in source form.
*/
static String sourceFormForAnnotation(AnnotationMirror annotationMirror) {
+ // If a value in the annotation is a reference to a class constant and that class constant is
+ // undefined, javac unhelpfully converts it into a string "<error>" and visits that instead. We
+ // want to catch this case and defer processing to allow the class to be defined by another
+ // annotation processor. So we look for annotation elements whose type is Class but whose
+ // reported value is a string. Unfortunately we can't extract the ErrorType corresponding to the
+ // missing class portably. With javac, the AttributeValue is a
+ // com.sun.tools.javac.code.Attribute.UnresolvedClass, which has a public field classType that
+ // is the ErrorType we need, but obviously that's nonportable and fragile.
+ validateClassValues(annotationMirror);
StringBuilder sb = new StringBuilder();
new AnnotationSourceFormVisitor().visitAnnotation(annotationMirror, sb);
return sb.toString();
}
+ /**
+ * Throws an exception if this annotation contains a value for a Class element that is not
+ * actually a type. The assumption is that the value is the string {@code "<error>"} which javac
+ * presents when a Class value is an undefined type.
+ */
+ private static void validateClassValues(AnnotationMirror annotationMirror) {
+ // A class literal can appear in three places:
+ // * for an element of type Class, for example @SomeAnnotation(Foo.class);
+ // * for an element of type Class[], for example @SomeAnnotation({Foo.class, Bar.class});
+ // * inside a nested annotation, for example @SomeAnnotation(@Nested(Foo.class)).
+ // These three possibilities are the three branches of the if/else chain below.
+ annotationMirror
+ .getElementValues()
+ .forEach(
+ (method, value) -> {
+ TypeMirror type = method.getReturnType();
+ if (isJavaLangClass(type) && !(value.getValue() instanceof TypeMirror)) {
+ throw new MissingTypeException(null);
+ } else if (type.getKind().equals(TypeKind.ARRAY)
+ && isJavaLangClass(MoreTypes.asArray(type).getComponentType())
+ && value.getValue() instanceof List<?>) {
+ @SuppressWarnings("unchecked") // a List can only be a List<AnnotationValue> here
+ List<AnnotationValue> values = (List<AnnotationValue>) value.getValue();
+ if (values.stream().anyMatch(av -> !(av.getValue() instanceof TypeMirror))) {
+ throw new MissingTypeException(null);
+ }
+ } else if (type.getKind().equals(TypeKind.DECLARED)
+ && MoreTypes.asElement(type).getKind().equals(ElementKind.ANNOTATION_TYPE)
+ && value.getValue() instanceof AnnotationMirror) {
+ validateClassValues((AnnotationMirror) value.getValue());
+ }
+ });
+ }
+
+ private static boolean isJavaLangClass(TypeMirror type) {
+ return type.getKind().equals(TypeKind.DECLARED)
+ && MoreTypes.asTypeElement(type).getQualifiedName().contentEquals("java.lang.Class");
+ }
+
private static StringBuilder appendQuoted(StringBuilder sb, String s) {
sb.append('"');
for (int i = 0; i < s.length(); i++) {
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java
index 3acf9332..cc0e62ec 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java
@@ -287,7 +287,7 @@ public class AutoAnnotationProcessor extends AbstractProcessor {
private String generatedClassName(ExecutableElement method) {
TypeElement type = MoreElements.asType(method.getEnclosingElement());
String name = type.getSimpleName().toString();
- while (type.getEnclosingElement() instanceof TypeElement) {
+ while (MoreElements.isType(type.getEnclosingElement())) {
type = MoreElements.asType(type.getEnclosingElement());
name = type.getSimpleName() + "_" + name;
}
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java
index b6a578fc..fc0d8b3e 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java
@@ -20,6 +20,7 @@ import static com.google.auto.common.MoreElements.getPackage;
import static com.google.auto.common.MoreStreams.toImmutableList;
import static com.google.auto.common.MoreStreams.toImmutableSet;
import static com.google.auto.value.processor.AutoValueProcessor.OMIT_IDENTIFIERS_OPTION;
+import static com.google.auto.value.processor.ClassNames.AUTO_ANNOTATION_NAME;
import static com.google.auto.value.processor.ClassNames.AUTO_BUILDER_NAME;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toCollection;
@@ -38,6 +39,7 @@ import com.google.auto.value.processor.MissingTypes.MissingTypeException;
import com.google.common.base.Ascii;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import java.lang.reflect.Field;
@@ -60,6 +62,7 @@ import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
@@ -77,7 +80,7 @@ public class AutoBuilderProcessor extends AutoValueishProcessor {
private static final String ALLOW_OPTION = "com.google.auto.value.AutoBuilderIsUnstable";
public AutoBuilderProcessor() {
- super(AUTO_BUILDER_NAME);
+ super(AUTO_BUILDER_NAME, /* appliesToInterfaces= */ true);
}
@Override
@@ -95,21 +98,9 @@ public class AutoBuilderProcessor extends AutoValueishProcessor {
@Override
void processType(TypeElement autoBuilderType) {
- if (!processingEnv.getOptions().containsKey(ALLOW_OPTION)) {
- errorReporter()
- .abortWithError(
- autoBuilderType,
- "Compile with -A%s to enable this UNSUPPORTED AND UNSTABLE prototype",
- ALLOW_OPTION);
- }
- if (autoBuilderType.getKind() != ElementKind.CLASS
- && autoBuilderType.getKind() != ElementKind.INTERFACE) {
- errorReporter()
- .abortWithError(
- autoBuilderType,
- "[AutoBuilderWrongType] @AutoBuilder only applies to classes and interfaces");
+ if (processingEnv.getOptions().containsKey(ALLOW_OPTION)) {
+ errorReporter().reportWarning(autoBuilderType, "The -A%s option is obsolete", ALLOW_OPTION);
}
- checkModifiersIfNested(autoBuilderType);
// The annotation is guaranteed to be present by the contract of Processor#process
AnnotationMirror autoBuilderAnnotation =
getAnnotationMirror(autoBuilderType, AUTO_BUILDER_NAME).get();
@@ -126,7 +117,7 @@ public class AutoBuilderProcessor extends AutoValueishProcessor {
Optional<BuilderMethodClassifier<VariableElement>> maybeClassifier =
BuilderMethodClassifierForAutoBuilder.classify(
methods, errorReporter(), processingEnv, executable, builtType, autoBuilderType);
- if (!maybeClassifier.isPresent()) {
+ if (!maybeClassifier.isPresent() || errorReporter().errorCount() > 0) {
// We've already output one or more error messages.
return;
}
@@ -134,7 +125,7 @@ public class AutoBuilderProcessor extends AutoValueishProcessor {
Map<String, String> propertyToGetterName =
Maps.transformValues(classifier.builderGetters(), PropertyGetter::getName);
AutoBuilderTemplateVars vars = new AutoBuilderTemplateVars();
- vars.props = propertySet(executable, propertyToGetterName);
+ vars.props = propertySet(autoBuilderType, executable, propertyToGetterName);
builder.defineVars(vars, classifier);
vars.identifiers = !processingEnv.getOptions().containsKey(OMIT_IDENTIFIERS_OPTION);
String generatedClassName = generatedClassName(autoBuilderType, "AutoBuilder_");
@@ -152,7 +143,15 @@ public class AutoBuilderProcessor extends AutoValueishProcessor {
}
private ImmutableSet<Property> propertySet(
- ExecutableElement executable, Map<String, String> propertyToGetterName) {
+ TypeElement autoBuilderType,
+ ExecutableElement executable,
+ Map<String, String> propertyToGetterName) {
+ boolean autoAnnotation =
+ MoreElements.getAnnotationMirror(executable, AUTO_ANNOTATION_NAME).isPresent();
+ ImmutableMap<String, String> builderInitializers =
+ autoAnnotation
+ ? autoAnnotationInitializers(autoBuilderType, executable)
+ : ImmutableMap.of();
// Fix any parameter names that are reserved words in Java. Java source code can't have
// such parameter names, but Kotlin code might, for example.
Map<VariableElement, String> identifiers =
@@ -161,18 +160,58 @@ public class AutoBuilderProcessor extends AutoValueishProcessor {
fixReservedIdentifiers(identifiers);
return executable.getParameters().stream()
.map(
- v ->
- newProperty(
- v, identifiers.get(v), propertyToGetterName.get(v.getSimpleName().toString())))
+ v -> {
+ String name = v.getSimpleName().toString();
+ return newProperty(
+ v,
+ identifiers.get(v),
+ propertyToGetterName.get(name),
+ Optional.ofNullable(builderInitializers.get(name)));
+ })
.collect(toImmutableSet());
}
- private Property newProperty(VariableElement var, String identifier, String getterName) {
+ private Property newProperty(
+ VariableElement var,
+ String identifier,
+ String getterName,
+ Optional<String> builderInitializer) {
String name = var.getSimpleName().toString();
TypeMirror type = var.asType();
Optional<String> nullableAnnotation = nullableAnnotationFor(var, var.asType());
return new Property(
- name, identifier, TypeEncoder.encode(type), type, nullableAnnotation, getterName);
+ name,
+ identifier,
+ TypeEncoder.encode(type),
+ type,
+ nullableAnnotation,
+ getterName,
+ builderInitializer);
+ }
+
+ private ImmutableMap<String, String> autoAnnotationInitializers(
+ TypeElement autoBuilderType, ExecutableElement autoAnnotationMethod) {
+ // We expect the return type of an @AutoAnnotation method to be an annotation type. If it isn't,
+ // AutoAnnotation will presumably complain, so we don't need to complain further.
+ TypeMirror returnType = autoAnnotationMethod.getReturnType();
+ if (!returnType.getKind().equals(TypeKind.DECLARED)) {
+ return ImmutableMap.of();
+ }
+ // This might not actually be an annotation (if the code is wrong), but if that's the case we
+ // just won't see any contained ExecutableElement where getDefaultValue() returns something.
+ TypeElement annotation = MoreTypes.asTypeElement(returnType);
+ ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
+ for (ExecutableElement method : methodsIn(annotation.getEnclosedElements())) {
+ AnnotationValue defaultValue = method.getDefaultValue();
+ if (defaultValue != null) {
+ String memberName = method.getSimpleName().toString();
+ builder.put(
+ memberName,
+ AnnotationOutput.sourceFormForInitializer(
+ defaultValue, processingEnv, memberName, autoBuilderType));
+ }
+ }
+ return builder.build();
}
private ExecutableElement findExecutable(
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoOneOfProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoOneOfProcessor.java
index 711b138c..4d19d216 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoOneOfProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoOneOfProcessor.java
@@ -60,7 +60,7 @@ import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING)
public class AutoOneOfProcessor extends AutoValueishProcessor {
public AutoOneOfProcessor() {
- super(AUTO_ONE_OF_NAME);
+ super(AUTO_ONE_OF_NAME, /* appliesToInterfaces= */ false);
}
@Override
@@ -75,13 +75,6 @@ public class AutoOneOfProcessor extends AutoValueishProcessor {
@Override
void processType(TypeElement autoOneOfType) {
- if (autoOneOfType.getKind() != ElementKind.CLASS) {
- errorReporter()
- .abortWithError(
- autoOneOfType,
- "[AutoOneOfNotClass] @" + AUTO_ONE_OF_NAME + " only applies to classes");
- }
- checkModifiersIfNested(autoOneOfType);
DeclaredType kindMirror = mirrorForKindType(autoOneOfType);
// We are going to classify the methods of the @AutoOneOf class into several categories.
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java b/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java
index 86cf4974..9fbc1652 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java
@@ -109,7 +109,8 @@ abstract class AutoValueOrBuilderTemplateVars extends AutoValueishTemplateVars {
*
* <ul>
* <li>it is {@code @Nullable} (in which case it defaults to null);
- * <li>it is {@code Optional} (in which case it defaults to empty);
+ * <li>it has a builder initializer (for example it is {@code Optional}, which will have an
+ * initializer of {@code Optional.empty()});
* <li>it has a property-builder method (in which case it defaults to empty).
* </ul>
*/
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java
index ab7da924..4479a056 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java
@@ -18,6 +18,7 @@ package com.google.auto.value.processor;
import static com.google.auto.common.MoreElements.getLocalAndInheritedMethods;
import static com.google.auto.common.MoreStreams.toImmutableList;
import static com.google.auto.value.processor.ClassNames.AUTO_VALUE_NAME;
+import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Sets.difference;
import static com.google.common.collect.Sets.intersection;
import static java.util.Comparator.naturalOrder;
@@ -45,7 +46,6 @@ import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.AnnotationMirror;
-import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
@@ -79,21 +79,24 @@ public class AutoValueProcessor extends AutoValueishProcessor {
@VisibleForTesting
AutoValueProcessor(ClassLoader loaderForExtensions) {
- super(AUTO_VALUE_NAME);
- this.extensions = null;
- this.loaderForExtensions = loaderForExtensions;
+ this(ImmutableList.of(), loaderForExtensions);
}
@VisibleForTesting
- public AutoValueProcessor(Iterable<? extends AutoValueExtension> extensions) {
- super(AUTO_VALUE_NAME);
- this.extensions = ImmutableList.copyOf(extensions);
- this.loaderForExtensions = null;
+ public AutoValueProcessor(Iterable<? extends AutoValueExtension> testExtensions) {
+ this(testExtensions, null);
+ }
+
+ private AutoValueProcessor(
+ Iterable<? extends AutoValueExtension> testExtensions, ClassLoader loaderForExtensions) {
+ super(AUTO_VALUE_NAME, /* appliesToInterfaces= */ false);
+ this.extensions = ImmutableList.copyOf(testExtensions);
+ this.loaderForExtensions = loaderForExtensions;
}
// Depending on how this AutoValueProcessor was constructed, we might already have a list of
- // extensions when init() is run, or, if `extensions` is null, we have a ClassLoader that will be
- // used to get the list using the ServiceLoader API.
+ // extensions when init() is run, or, if `loaderForExtensions` is not null, it is a ClassLoader
+ // that will be used to get the list using the ServiceLoader API.
private ImmutableList<AutoValueExtension> extensions;
private final ClassLoader loaderForExtensions;
@@ -108,7 +111,8 @@ public class AutoValueProcessor extends AutoValueishProcessor {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
- if (extensions == null) {
+ if (loaderForExtensions != null) {
+ checkState(extensions.isEmpty());
try {
extensions = extensionsFromLoader(loaderForExtensions);
} catch (RuntimeException | Error e) {
@@ -165,10 +169,6 @@ public class AutoValueProcessor extends AutoValueishProcessor {
@Override
void processType(TypeElement type) {
- if (type.getKind() != ElementKind.CLASS) {
- errorReporter()
- .abortWithError(type, "[AutoValueNotClass] @AutoValue only applies to classes");
- }
if (ancestorIsAutoValue(type)) {
errorReporter()
.abortWithError(type, "[AutoValueExtend] One @AutoValue class may not extend another");
@@ -180,7 +180,6 @@ public class AutoValueProcessor extends AutoValueishProcessor {
"[AutoValueImplAnnotation] @AutoValue may not be used to implement an annotation"
+ " interface; try using @AutoAnnotation instead");
}
- checkModifiersIfNested(type);
// We are going to classify the methods of the @AutoValue class into several categories.
// This covers the methods in the class itself and the ones it inherits from supertypes.
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java
index 93f2f79e..31f1ec1c 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueishProcessor.java
@@ -29,6 +29,7 @@ import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
+import static javax.lang.model.util.ElementFilter.constructorsIn;
import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;
@@ -92,19 +93,22 @@ import javax.tools.JavaFileObject;
*/
abstract class AutoValueishProcessor extends AbstractProcessor {
private final String annotationClassName;
+ private final boolean appliesToInterfaces;
/**
- * Qualified names of {@code @AutoValue} or {@code AutoOneOf} classes that we attempted to process
- * but had to abandon because we needed other types that they referenced and those other types
- * were missing.
+ * Qualified names of {@code @AutoValue} (etc) classes that we attempted to process but had to
+ * abandon because we needed other types that they referenced and those other types were missing.
*/
private final List<String> deferredTypeNames = new ArrayList<>();
- AutoValueishProcessor(String annotationClassName) {
+ AutoValueishProcessor(String annotationClassName, boolean appliesToInterfaces) {
this.annotationClassName = annotationClassName;
+ this.appliesToInterfaces = appliesToInterfaces;
}
- /** The annotation we are processing, {@code AutoValue} or {@code AutoOneOf}. */
+ /**
+ * The annotation we are processing, for example {@code AutoValue} or {@code AutoBuilder}.
+ */
private TypeElement annotationType;
/** The simple name of {@link #annotationType}. */
private String simpleAnnotationName;
@@ -117,6 +121,10 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
super.init(processingEnv);
errorReporter = new ErrorReporter(processingEnv);
nullables = new Nullables(processingEnv);
+ annotationType = elementUtils().getTypeElement(annotationClassName);
+ if (annotationType != null) {
+ simpleAnnotationName = annotationType.getSimpleName().toString();
+ }
}
final ErrorReporter errorReporter() {
@@ -132,9 +140,9 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
}
/**
- * Qualified names of {@code @AutoValue} or {@code AutoOneOf} classes that we attempted to process
- * but had to abandon because we needed other types that they referenced and those other types
- * were missing. This is used by tests.
+ * Qualified names of {@code @AutoValue} (etc) classes that we attempted to process but had to
+ * abandon because we needed other types that they referenced and those other types were missing.
+ * This is used by tests.
*/
final ImmutableList<String> deferredTypeNames() {
return ImmutableList.copyOf(deferredTypeNames);
@@ -160,6 +168,7 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
private final Optional<String> nullableAnnotation;
private final Optionalish optional;
private final String getter;
+ private final String builderInitializer; // empty, or with initial ` = `.
Property(
String name,
@@ -167,17 +176,41 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
String type,
TypeMirror typeMirror,
Optional<String> nullableAnnotation,
- String getter) {
+ String getter,
+ Optional<String> maybeBuilderInitializer) {
this.name = name;
this.identifier = identifier;
this.type = type;
this.typeMirror = typeMirror;
this.nullableAnnotation = nullableAnnotation;
this.optional = Optionalish.createIfOptional(typeMirror);
+ this.builderInitializer =
+ maybeBuilderInitializer.isPresent()
+ ? " = " + maybeBuilderInitializer.get()
+ : builderInitializer();
this.getter = getter;
}
/**
+ * Returns the appropriate initializer for a builder property. Builder properties are never
+ * primitive; if the built property is an {@code int} the builder property will be an {@code
+ * Integer}. So the default value for a builder property will be null unless there is an
+ * initializer. The caller of the constructor may have supplied an initializer, but otherwise we
+ * supply one only if this property is an {@code Optional} and is not {@code @Nullable}. In that
+ * case the initializer sets it to {@code Optional.empty()}.
+ */
+ private String builderInitializer() {
+ if (nullableAnnotation.isPresent()) {
+ return "";
+ }
+ Optionalish optional = Optionalish.createIfOptional(typeMirror);
+ if (optional == null) {
+ return "";
+ }
+ return " = " + optional.getEmpty();
+ }
+
+ /**
* Returns the name of the property as it should be used when declaring identifiers (fields and
* parameters). If the original getter method was {@code foo()} then this will be {@code foo}.
* If it was {@code getFoo()} then it will be {@code foo}. If it was {@code getPackage()} then
@@ -219,6 +252,14 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
}
/**
+ * Returns a string to be used as an initializer for a builder field for this property,
+ * including the leading {@code =}, or an empty string if there is no explicit initializer.
+ */
+ public String getBuilderInitializer() {
+ return builderInitializer;
+ }
+
+ /**
* Returns the string to use as a method annotation to indicate the nullability of this
* property. It is either the empty string, if the property is not nullable, or an annotation
* string with a trailing space, such as {@code "@`javax.annotation.Nullable` "}, where the
@@ -266,7 +307,8 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
type,
method.getReturnType(),
nullableAnnotation,
- method.getSimpleName().toString());
+ method.getSimpleName().toString(),
+ Optional.empty());
this.method = method;
this.fieldAnnotations = fieldAnnotations;
this.methodAnnotations = methodAnnotations;
@@ -305,7 +347,6 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
@Override
public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
- annotationType = elementUtils().getTypeElement(annotationClassName);
if (annotationType == null) {
// This should not happen. If the annotation type is not found, how did the processor get
// triggered?
@@ -318,7 +359,6 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
+ " because the annotation class was not found");
return false;
}
- simpleAnnotationName = annotationType.getSimpleName().toString();
List<TypeElement> deferredTypes =
deferredTypeNames.stream()
.map(name -> elementUtils().getTypeElement(name))
@@ -330,9 +370,10 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
for (TypeElement type : deferredTypes) {
errorReporter.reportError(
type,
- "[AutoValueUndefined] Did not generate @%s class for %s because it references"
+ "[%sUndefined] Did not generate @%s class for %s because it references"
+ " undefined types",
simpleAnnotationName,
+ simpleAnnotationName,
type.getQualifiedName());
}
return false;
@@ -347,6 +388,7 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
deferredTypeNames.clear();
for (TypeElement type : types) {
try {
+ validateType(type);
processType(type);
} catch (AbortProcessingException e) {
// We abandoned this type; continue with the next.
@@ -362,7 +404,8 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
String trace = Throwables.getStackTraceAsString(e);
errorReporter.reportError(
type,
- "[AutoValueException] @%s processor threw an exception: %s",
+ "[%sException] @%s processor threw an exception: %s",
+ simpleAnnotationName,
simpleAnnotationName,
trace);
throw e;
@@ -372,8 +415,44 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
}
/**
- * Analyzes a single {@code @AutoValue} or {@code @AutoOneOf} class, and outputs the corresponding
- * implementation class or classes.
+ * Validations common to all the subclasses. An {@code @AutoFoo} type must be a class, or possibly
+ * an interface for {@code @AutoBuilder}. If it is a class then it must have a non-private no-arg
+ * constructor. And, since we'll be generating a subclass, it can't be final.
+ */
+ private void validateType(TypeElement type) {
+ ElementKind kind = type.getKind();
+ boolean kindOk =
+ kind.equals(ElementKind.CLASS)
+ || (appliesToInterfaces && kind.equals(ElementKind.INTERFACE));
+ if (!kindOk) {
+ String appliesTo = appliesToInterfaces ? "classes and interfaces" : "classes";
+ errorReporter.abortWithError(
+ type,
+ "[%sWrongType] @%s only applies to %s",
+ simpleAnnotationName,
+ simpleAnnotationName,
+ appliesTo);
+ }
+ checkModifiersIfNested(type);
+ if (!hasVisibleNoArgConstructor(type)) {
+ errorReporter.reportError(
+ type,
+ "[%sConstructor] @%s class must have a non-private no-arg constructor",
+ simpleAnnotationName,
+ simpleAnnotationName);
+ }
+ if (type.getModifiers().contains(Modifier.FINAL)) {
+ errorReporter.abortWithError(
+ type,
+ "[%sFinal] @%s class must not be final",
+ simpleAnnotationName,
+ simpleAnnotationName);
+ }
+ }
+
+ /**
+ * Analyzes a single {@code @AutoValue} (etc) class, and outputs the corresponding implementation
+ * class or classes.
*
* @param type the class with the {@code @AutoValue} or {@code @AutoOneOf} annotation.
*/
@@ -435,7 +514,9 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
if (p.isNullable() && returnType.getKind().isPrimitive()) {
errorReporter()
.reportError(
- propertyMethod, "[AutoValueNullPrimitive] Primitive types cannot be @Nullable");
+ propertyMethod,
+ "[%sNullPrimitive] Primitive types cannot be @Nullable",
+ simpleAnnotationName);
}
});
return props.build();
@@ -467,24 +548,23 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
/** Returns the spelling to be used in the generated code for the given list of annotations. */
static ImmutableList<String> annotationStrings(List<? extends AnnotationMirror> annotations) {
- // TODO(b/68008628): use ImmutableList.toImmutableList() when that works.
return annotations.stream()
.map(AnnotationOutput::sourceFormForAnnotation)
+ .sorted() // ensures deterministic order
.collect(toImmutableList());
}
/**
- * Returns the name of the generated {@code @AutoValue} or {@code @AutoOneOf} class, for example
- * {@code AutoOneOf_TaskResult} or {@code $$AutoValue_SimpleMethod}.
+ * Returns the name of the generated {@code @AutoValue} (etc) class, for example {@code
+ * AutoOneOf_TaskResult} or {@code $$AutoValue_SimpleMethod}.
*
- * @param type the name of the type bearing the {@code @AutoValue} or {@code @AutoOneOf}
- * annotation.
+ * @param type the name of the type bearing the {@code @AutoValue} (etc) annotation.
* @param prefix the prefix to use in the generated class. This may start with one or more dollar
* signs, for an {@code @AutoValue} implementation where there are AutoValue extensions.
*/
static String generatedClassName(TypeElement type, String prefix) {
String name = type.getSimpleName().toString();
- while (type.getEnclosingElement() instanceof TypeElement) {
+ while (MoreElements.isType(type.getEnclosingElement())) {
type = MoreElements.asType(type.getEnclosingElement());
name = type.getSimpleName() + "_" + name;
}
@@ -555,7 +635,8 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
for (ExecutableElement context : contexts) {
errorReporter.reportError(
context,
- "[AutoValueDupProperty] More than one @%s property called %s",
+ "[%sDupProperty] More than one @%s property called %s",
+ simpleAnnotationName,
simpleAnnotationName,
name);
}
@@ -589,8 +670,9 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
List<? extends AnnotationMirror> elementAnnotations = element.getAnnotationMirrors();
OptionalInt nullableAnnotationIndex = nullableAnnotationIndex(elementAnnotations);
if (nullableAnnotationIndex.isPresent()) {
- ImmutableList<String> annotations = annotationStrings(elementAnnotations);
- return Optional.of(annotations.get(nullableAnnotationIndex.getAsInt()) + " ");
+ AnnotationMirror annotation = elementAnnotations.get(nullableAnnotationIndex.getAsInt());
+ String annotationString = AnnotationOutput.sourceFormForAnnotation(annotation);
+ return Optional.of(annotationString + " ");
} else {
return Optional.empty();
}
@@ -1152,6 +1234,14 @@ abstract class AutoValueishProcessor extends AbstractProcessor {
return getAnnotationMirror(element, annotationName).isPresent();
}
+ /** True if the type is a class with a non-private no-arg constructor, or is an interface. */
+ static boolean hasVisibleNoArgConstructor(TypeElement type) {
+ return type.getKind().isInterface()
+ || constructorsIn(type.getEnclosedElements()).stream()
+ .anyMatch(
+ c -> c.getParameters().isEmpty() && !c.getModifiers().contains(Modifier.PRIVATE));
+ }
+
final void writeSourceFile(String className, String text, TypeElement originatingType) {
try {
JavaFileObject sourceFile =
diff --git a/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java b/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
index 51773e6f..a4336f5e 100644
--- a/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
+++ b/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
@@ -386,14 +386,17 @@ abstract class BuilderMethodClassifier<E extends Element> {
DeclaredType builderTypeMirror = MoreTypes.asDeclared(builderType.asType());
ExecutableType methodMirror =
MoreTypes.asExecutable(typeUtils.asMemberOf(builderTypeMirror, method));
- if (TYPE_EQUIVALENCE.equivalent(methodMirror.getReturnType(), builderType.asType())) {
+ TypeMirror returnType = methodMirror.getReturnType();
+ if (typeUtils.isSubtype(builderType.asType(), returnType)
+ && !MoreTypes.isTypeOf(Object.class, returnType)) {
+ // We allow the return type to be a supertype (other than Object), to support step builders.
TypeMirror parameterType = Iterables.getOnlyElement(methodMirror.getParameterTypes());
propertyNameToSetters.put(
propertyName, new PropertySetter(method, parameterType, function.get()));
} else {
errorReporter.reportError(
method,
- "[%sBuilderRet] Setter methods must return %s",
+ "[%sBuilderRet] Setter methods must return %s or a supertype",
autoWhat(),
builderType.asType());
}
diff --git a/value/src/main/java/com/google/auto/value/processor/BuilderSpec.java b/value/src/main/java/com/google/auto/value/processor/BuilderSpec.java
index 9f45d172..b612c104 100644
--- a/value/src/main/java/com/google/auto/value/processor/BuilderSpec.java
+++ b/value/src/main/java/com/google/auto/value/processor/BuilderSpec.java
@@ -18,6 +18,7 @@ package com.google.auto.value.processor;
import static com.google.auto.common.MoreElements.getLocalAndInheritedMethods;
import static com.google.auto.common.MoreStreams.toImmutableSet;
import static com.google.auto.value.processor.AutoValueishProcessor.hasAnnotationMirror;
+import static com.google.auto.value.processor.AutoValueishProcessor.hasVisibleNoArgConstructor;
import static com.google.auto.value.processor.AutoValueishProcessor.nullableAnnotationFor;
import static com.google.auto.value.processor.ClassNames.AUTO_VALUE_BUILDER_NAME;
import static com.google.common.collect.Sets.immutableEnumSet;
@@ -86,16 +87,9 @@ class BuilderSpec {
Optional<TypeElement> builderTypeElement = Optional.empty();
for (TypeElement containedClass : typesIn(autoValueClass.getEnclosedElements())) {
if (hasAnnotationMirror(containedClass, AUTO_VALUE_BUILDER_NAME)) {
- if (!CLASS_OR_INTERFACE.contains(containedClass.getKind())) {
- errorReporter.reportError(
- containedClass,
- "[AutoValueBuilderClass] @AutoValue.Builder can only apply to a class or an"
- + " interface");
- } else if (!containedClass.getModifiers().contains(Modifier.STATIC)) {
- errorReporter.reportError(
- containedClass,
- "[AutoValueInnerBuilder] @AutoValue.Builder cannot be applied to a non-static class");
- } else if (builderTypeElement.isPresent()) {
+ findBuilderError(containedClass)
+ .ifPresent(error -> errorReporter.reportError(containedClass, "%s", error));
+ if (builderTypeElement.isPresent()) {
errorReporter.reportError(
containedClass,
"[AutoValueTwoBuilders] %s already has a Builder: %s",
@@ -114,6 +108,24 @@ class BuilderSpec {
}
}
+ /** Finds why this {@code @AutoValue.Builder} class is bad, if it is bad. */
+ private Optional<String> findBuilderError(TypeElement builderTypeElement) {
+ if (!CLASS_OR_INTERFACE.contains(builderTypeElement.getKind())) {
+ return Optional.of(
+ "[AutoValueBuilderClass] @AutoValue.Builder can only apply to a class or an"
+ + " interface");
+ } else if (!builderTypeElement.getModifiers().contains(Modifier.STATIC)) {
+ return Optional.of(
+ "[AutoValueInnerBuilder] @AutoValue.Builder cannot be applied to a non-static class");
+ } else if (builderTypeElement.getKind().equals(ElementKind.CLASS)
+ && !hasVisibleNoArgConstructor(builderTypeElement)) {
+ return Optional.of(
+ "[AutoValueBuilderConstructor] @AutoValue.Builder class must have a non-private no-arg"
+ + " constructor");
+ }
+ return Optional.empty();
+ }
+
/** Representation of an {@code AutoValue.Builder} class or interface. */
class Builder implements AutoValueExtension.BuilderContext {
private final TypeElement builderTypeElement;
@@ -333,7 +345,7 @@ class BuilderSpec {
vars.builderRequiredProperties =
vars.props.stream()
.filter(p -> !p.isNullable())
- .filter(p -> p.getOptional() == null)
+ .filter(p -> p.getBuilderInitializer().isEmpty())
.filter(p -> !vars.builderPropertyBuilders.containsKey(p.getName()))
.collect(toImmutableSet());
}
diff --git a/value/src/main/java/com/google/auto/value/processor/GwtCompatibility.java b/value/src/main/java/com/google/auto/value/processor/GwtCompatibility.java
index fae4e092..35fcbbf0 100644
--- a/value/src/main/java/com/google/auto/value/processor/GwtCompatibility.java
+++ b/value/src/main/java/com/google/auto/value/processor/GwtCompatibility.java
@@ -15,15 +15,9 @@
*/
package com.google.auto.value.processor;
-import static java.util.stream.Collectors.joining;
-
-import java.util.Collections;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
-import javax.lang.model.element.AnnotationValue;
-import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
@@ -46,29 +40,7 @@ class GwtCompatibility {
return gwtCompatibleAnnotation;
}
- // Get rid of the misconceived <? extends ExecutableElement, ? extends AnnotationValue>
- // in the return type of getElementValues().
- static Map<ExecutableElement, AnnotationValue> getElementValues(AnnotationMirror annotation) {
- return Collections.<ExecutableElement, AnnotationValue>unmodifiableMap(
- annotation.getElementValues());
- }
-
String gwtCompatibleAnnotationString() {
- if (gwtCompatibleAnnotation.isPresent()) {
- AnnotationMirror annotation = gwtCompatibleAnnotation.get();
- TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement();
- String annotationArguments;
- if (annotation.getElementValues().isEmpty()) {
- annotationArguments = "";
- } else {
- annotationArguments =
- getElementValues(annotation).entrySet().stream()
- .map(e -> e.getKey().getSimpleName() + " = " + e.getValue())
- .collect(joining(", ", "(", ")"));
- }
- return "@" + annotationElement.getQualifiedName() + annotationArguments;
- } else {
- return "";
- }
+ return gwtCompatibleAnnotation.map(AnnotationOutput::sourceFormForAnnotation).orElse("");
}
}
diff --git a/value/src/main/java/com/google/auto/value/processor/GwtSerialization.java b/value/src/main/java/com/google/auto/value/processor/GwtSerialization.java
index 30ad0926..8673d3db 100644
--- a/value/src/main/java/com/google/auto/value/processor/GwtSerialization.java
+++ b/value/src/main/java/com/google/auto/value/processor/GwtSerialization.java
@@ -18,6 +18,7 @@ package com.google.auto.value.processor;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;
+import com.google.auto.common.AnnotationMirrors;
import com.google.auto.value.processor.AutoValueishProcessor.GetterProperty;
import com.google.auto.value.processor.PropertyBuilderClassifier.PropertyBuilder;
import com.google.common.collect.ImmutableMap;
@@ -26,13 +27,10 @@ import com.google.escapevelocity.Template;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
import java.util.zip.CRC32;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
-import javax.lang.model.element.AnnotationValue;
-import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
@@ -60,13 +58,11 @@ class GwtSerialization {
Optional<AnnotationMirror> optionalGwtCompatible = gwtCompatibility.gwtCompatibleAnnotation();
if (optionalGwtCompatible.isPresent()) {
AnnotationMirror gwtCompatible = optionalGwtCompatible.get();
- for (Map.Entry<ExecutableElement, AnnotationValue> entry :
- GwtCompatibility.getElementValues(gwtCompatible).entrySet()) {
- if (entry.getKey().getSimpleName().contentEquals("serializable")
- && entry.getValue().getValue().equals(true)) {
- return true;
- }
- }
+ return AnnotationMirrors.getAnnotationValuesWithDefaults(gwtCompatible).entrySet().stream()
+ .anyMatch(
+ e ->
+ e.getKey().getSimpleName().contentEquals("serializable")
+ && e.getValue().getValue().equals(true));
}
return false;
}
diff --git a/value/src/main/java/com/google/auto/value/processor/autovalue.vm b/value/src/main/java/com/google/auto/value/processor/autovalue.vm
index 86cfe493..18ca827a 100644
--- a/value/src/main/java/com/google/auto/value/processor/autovalue.vm
+++ b/value/src/main/java/com/google/auto/value/processor/autovalue.vm
@@ -75,15 +75,11 @@ ${modifiers}class $subclass$formalTypes extends $origClass$actualTypes {
## the constructor is called from the extension code.
#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();
+ `java.util.Objects`.requireNonNull($p);
#end
#end
diff --git a/value/src/main/java/com/google/auto/value/processor/builder.vm b/value/src/main/java/com/google/auto/value/processor/builder.vm
index 630330ca..b1787f25 100644
--- a/value/src/main/java/com/google/auto/value/processor/builder.vm
+++ b/value/src/main/java/com/google/auto/value/processor/builder.vm
@@ -40,7 +40,7 @@ class ${builderName}${builderFormalTypes} ##
#if ($p.kind.primitive)
- private $types.boxedClass($p.typeMirror).simpleName $p;
+ private $types.boxedClass($p.typeMirror).simpleName $p $p.builderInitializer;
#else
@@ -54,7 +54,7 @@ class ${builderName}${builderFormalTypes} ##
#end
- private $p.type $p #if ($p.optional && !$p.nullable) = $p.optional.empty #end ;
+ private $p.type $p $p.builderInitializer;
#end
#end
@@ -94,15 +94,11 @@ class ${builderName}${builderFormalTypes} ##
#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();
+ `java.util.Objects`.requireNonNull($p);
#end
#end