Top Description Inners Constructors Methods
com.google.auto.value.extension

public abstract Class AutoValueExtension

extends Object
Class Inheritance
Known Direct Subclasses
com.google.auto.value.extension.memoized.processor.MemoizeExtension, com.google.auto.value.extension.serializable.processor.SerializableAutoValueExtension, com.google.auto.value.extension.toprettystring.processor.ToPrettyStringExtension
Imports
com.google.common.collect.ImmutableList, .ImmutableSet, java.util.List, .Map, .Optional, .Set, javax.annotation.processing.ProcessingEnvironment, javax.annotation.processing.Processor, javax.annotation.processing.SupportedOptions, javax.lang.model.element.AnnotationMirror, javax.lang.model.element.ExecutableElement, javax.lang.model.element.TypeElement, javax.lang.model.type.TypeMirror

An AutoValueExtension allows for extra functionality to be created during the generation of an AutoValue class.

Extensions are discovered at compile time using the java.util.ServiceLoader APIs, allowing them to run without any additional annotations. To be found by ServiceLoader, an extension class must be public with a public no-arg constructor, and its fully-qualified name must appear in a file called META-INF/services/com.google.auto.value.extension.AutoValueExtension in a jar that is on the compiler's -classpath or -processorpath.

When the AutoValue processor runs for a class Foo, it will ask each Extension whether it is applicable. Suppose two Extensions reply that they are. Then the processor will generate the AutoValue logic in a direct subclass of Foo, and it will ask the first Extension to generate a subclass of that, and the second Extension to generate a subclass of the subclass. So we might have this hierarchy:

@AutoValue abstract class Foo {...}                          // the hand-written class
abstract class $$AutoValue_Foo extends Foo {...}             // generated by AutoValue processor
abstract class $AutoValue_Foo extends $$AutoValue_Foo {...}  // generated by first Extension
final class AutoValue_Foo extends $AutoValue_Foo {...}       // generated by second Extension

(The exact naming scheme illustrated here is not fixed and should not be relied on.)

If an Extension needs its generated class to be the final class in the inheritance hierarchy, its mustBeFinal(Context) method returns true. Only one Extension can return true for a given context. Only generated classes that will be the final class in the inheritance hierarchy can be declared final. All others should be declared abstract.

The first generated class in the hierarchy will always be the one generated by the AutoValue processor and the last one will always be the one generated by the Extension that mustBeFinal, if any. Other than that, the order of the classes in the hierarchy is unspecified. The last class in the hierarchy is AutoValue_Foo and that is the one that the Foo class will reference, for example with new AutoValue_Foo(...).

Each Extension must also be sure to generate a constructor with arguments corresponding to all properties in com.google.auto.value.extension.AutoValueExtension.Context#propertyTypes(), in order, and to call the superclass constructor with the same arguments. This constructor must have at least package visibility.

Because the class generated by the AutoValue processor is at the top of the generated hierarchy, Extensions can override its methods, for example hashCode(), toString(), or the implementations of the various bar() property methods.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
AutoValueExtension.BuilderContext

Represents a Builder associated with an @AutoValue class.

public static interface
AutoValueExtension.Context

The context of the generation cycle.

public static enum
AutoValueExtension.IncrementalExtensionType

Indicates to an annotation processor environment supporting incremental annotation processing (currently a feature specific to Gradle starting with version 4.8) the incremental type of an Extension.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
applicable(AutoValueExtension.Context
The Context of the code generation for this class.
context
)

Determines whether this Extension applies to the given context.

public Set<javax.lang.model.element.ExecutableElement>
consumeMethods(AutoValueExtension.Context
the Context of the code generation for this class.
context
)

Returns a possible empty set of abstract methods that this Extension intends to implement.

public Set<String>
consumeProperties(AutoValueExtension.Context
the Context of the code generation for this class.
context
)

Returns a possibly empty set of property names that this Extension intends to implement.

public abstract String

Returns:

The source code of the generated class, or null if this extension does not generate a class in the hierarchy.
generateClass
(AutoValueExtension.Context
The Context of the code generation for this class.
context
,
String
The simple name of the resulting class. The returned code will be written to a file named accordingly.
className
,
String
The simple name of the direct parent of the generated class. This could be the AutoValue generated class, or a class generated as the result of another Extension.
classToExtend
,
boolean
True if this class is the last class in the chain, meaning it should be marked as final. Otherwise it should be marked as abstract.
isFinal
)

Returns the generated source code of the class named className to extend classToExtend, or null if this extension does not generate a class in the hierarchy.

public Set<String>

Returns:

the set of options recognized by this extension or an empty set if none
getSupportedOptions
()

Analogous to Processor#getSupportedOptions(), here to allow extensions to report their own.

public AutoValueExtension.IncrementalExtensionType
incrementalType(javax.annotation.processing.ProcessingEnvironment processingEnvironment)

Determines the incremental type of this Extension.

public boolean
mustBeFinal(AutoValueExtension.Context
the Context of the code generation for this class.
context
)

Denotes that the class generated by this Extension must be the final class in the inheritance hierarchy.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AutoValueExtensionback to summary
public AutoValueExtension()

Method Detail

applicableback to summary
public boolean applicable(AutoValueExtension.Context context)

Determines whether this Extension applies to the given context. If an Extension returns false for a given class, it will not be called again during the processing of that class. An Extension can return true and still choose not to generate any code for the class, by returning null from generateClass. That is often a more flexible approach.

Parameters
context:AutoValueExtension.Context

The Context of the code generation for this class.

consumeMethodsback to summary
public Set<javax.lang.model.element.ExecutableElement> consumeMethods(AutoValueExtension.Context context)

Returns a possible empty set of abstract methods that this Extension intends to implement. This will prevent AutoValue from generating an implementation, in cases where it would have, and it will also avoid warnings about abstract methods that AutoValue doesn't expect. The default set returned by this method is empty.

Each returned method must be one of the abstract methods in Context#abstractMethods().

For example, Android's Parcelable interface includes a method void writeToParcel(Parcel, int). Normally AutoValue would not know what to do with that abstract method. But an AutoValueExtension that understands Parcelable can provide a useful implementation and return the writeToParcel method here. That will prevent a warning about the method from AutoValue.

Parameters
context:AutoValueExtension.Context

the Context of the code generation for this class.

consumePropertiesback to summary
public Set<String> consumeProperties(AutoValueExtension.Context context)

Returns a possibly empty set of property names that this Extension intends to implement. This will prevent AutoValue from generating an implementation, and remove the supplied properties from builders, constructors, toString, equals, and hashCode. The default set returned by this method is empty.

Each returned string must be one of the property names in Context#properties().

Returning a property name from this method is equivalent to returning the property's getter method from consumeMethods.

For example, Android's Parcelable interface includes a method int describeContents(). Since this is an abstract method with no parameters, by default AutoValue will consider that it defines an int property called describeContents. If an @AutoValue class implements Parcelable and does not provide an implementation of this method, by default its implementation will include describeContents in builders, constructors, and so on. But an AutoValueExtension that understands Parcelable can instead provide a useful implementation and return a set containing "describeContents". Then describeContents will be omitted from builders and the rest.

Parameters
context:AutoValueExtension.Context

the Context of the code generation for this class.

generateClassback to summary
public abstract String generateClass(AutoValueExtension.Context context, String className, String classToExtend, boolean isFinal)

Returns the generated source code of the class named className to extend classToExtend, or null if this extension does not generate a class in the hierarchy. If there is a generated class, it should be final if isFinal is true; otherwise it should be abstract. The returned string should be a complete Java class definition of the class className in the package context.packageName().

The returned string will typically look like this:

package <package>;
...
<finalOrAbstract> class <className> extends <classToExtend> {
  // Constructor
  <className>(<constructorParameters>) {
    super(<constructorParameterNames>);
    ...
  }
  ...
}

Here, <package> is Context#packageName(); <finalOrAbstract> is the keyword final if isFinal is true or abstract otherwise; and <className> and <classToExtend> are the values of this method's parameters of the same name. The <constructorParameters> and <constructorParameterNames> are typically derived from Context#propertyTypes().

Parameters
context:AutoValueExtension.Context

The Context of the code generation for this class.

className:String

The simple name of the resulting class. The returned code will be written to a file named accordingly.

classToExtend:String

The simple name of the direct parent of the generated class. This could be the AutoValue generated class, or a class generated as the result of another Extension.

isFinal:boolean

True if this class is the last class in the chain, meaning it should be marked as final. Otherwise it should be marked as abstract.

Returns:String

The source code of the generated class, or null if this extension does not generate a class in the hierarchy.

getSupportedOptionsback to summary
public Set<String> getSupportedOptions()

Analogous to Processor#getSupportedOptions(), here to allow extensions to report their own.

By default, if the extension class is annotated with SupportedOptions, this will return a set with the strings in the annotation. If the class is not so annotated, an empty set is returned.

Returns:Set<String>

the set of options recognized by this extension or an empty set if none

See Also
SupportedOptions
incrementalTypeback to summary
public AutoValueExtension.IncrementalExtensionType incrementalType(javax.annotation.processing.ProcessingEnvironment processingEnvironment)

Determines the incremental type of this Extension.

The ProcessingEnvironment can be used, among other things, to obtain the processor options, using ProcessingEnvironment#getOptions().

The actual incremental type of the AutoValue processor as a whole will be the loosest incremental types of the Extensions present in the annotation processor path. The default returned value is IncrementalExtensionType#UNKNOWN, which will disable incremental annotation processing entirely.

mustBeFinalback to summary
public boolean mustBeFinal(AutoValueExtension.Context context)

Denotes that the class generated by this Extension must be the final class in the inheritance hierarchy. Only one Extension may be the final class, so this should be used sparingly.

Parameters
context:AutoValueExtension.Context

the Context of the code generation for this class.

com.google.auto.value.extension back to summary

public Interface AutoValueExtension.BuilderContext

Known Direct Implementers
com.google.auto.value.processor.BuilderSpec.Builder

Represents a Builder associated with an @AutoValue class.

Method Summary

Modifier and TypeMethod and Description
public javax.lang.model.element.ExecutableElement
autoBuildMethod()

Returns the abstract build method.

public Set<javax.lang.model.element.ExecutableElement>
builderMethods()

Returns static no-argument methods in the @AutoValue class that return the builder type.

public javax.lang.model.element.TypeElement
builderType()

Returns the @AutoValue.Builder interface or abstract class that this object represents.

public Optional<javax.lang.model.element.ExecutableElement>
buildMethod()

Returns the method build() in the builder class, if it exists and returns the @AutoValue type.

public Map<String, javax.lang.model.element.ExecutableElement>
propertyBuilders()

Returns a map from property names to property builders.

public Map<String, Set<javax.lang.model.element.ExecutableElement>>
setters()

Returns a map from property names to the corresponding setters.

public Set<javax.lang.model.element.ExecutableElement>
toBuilderMethods()

Returns abstract no-argument methods in the @AutoValue class that return the builder type.

Method Detail

autoBuildMethodback to summary
public javax.lang.model.element.ExecutableElement autoBuildMethod()

Returns the abstract build method. If the @AutoValue class is Foo, this is an abstract no-argument method in the builder class that returns Foo. This might be called build(), or, following a common convention, it might be called autoBuild() and used in the implementation of a build() method that is defined in the builder class.

Extensions should call the build() method in preference to this one. But they should override this one if they want to customize build-time behaviour.

builderMethodsback to summary
public Set<javax.lang.model.element.ExecutableElement> builderMethods()

Returns static no-argument methods in the @AutoValue class that return the builder type.

Consider a class like this:

  @AutoValue abstract class Foo {
    abstract String bar();

    static Builder builder() {
      return new AutoValue_Foo.Builder()
          .setBar("default bar");
    }

    @AutoValue.Builder
    abstract class Builder {
      abstract Builder setBar(String x);
      abstract Foo build();
    }
  }

Here builderMethods() will return a set containing the method Foo.builder(). Generated code should usually call this method in preference to constructing AutoValue_Foo.Builder() directly, because this method can establish default values for properties, as it does here.

builderTypeback to summary
public javax.lang.model.element.TypeElement builderType()

Returns the @AutoValue.Builder interface or abstract class that this object represents.

buildMethodback to summary
public Optional<javax.lang.model.element.ExecutableElement> buildMethod()

Returns the method build() in the builder class, if it exists and returns the @AutoValue type. This is the method that generated code for @AutoValue class Foo should call in order to get an instance of Foo from its builder. The returned method is called build(); if the builder uses some other name then extensions have no good way to guess how they should build.

A common convention is for build() to be a concrete method in the @AutoValue.Builder class, which calls an abstract method autoBuild() that is implemented in the generated subclass. The build() method can then do validation, defaulting, and so on.

propertyBuildersback to summary
public Map<String, javax.lang.model.element.ExecutableElement> propertyBuilders()

Returns a map from property names to property builders. For example, if there is a property foo defined by abstract ImmutableList<String> foo(); or abstract ImmutableList<String> getFoo(); in the @AutoValue class, then there can potentially be a builder defined by abstract ImmutableList.Builder<String> fooBuilder(); in the @AutoValue.Builder class. This map would then map "foo" to the ExecutableElement representing fooBuilder().

settersback to summary
public Map<String, Set<javax.lang.model.element.ExecutableElement>> setters()

Returns a map from property names to the corresponding setters. A property may have more than one setter. For example, an ImmutableList<String> might be set by setFoo(ImmutableList<String>) and setFoo(String[]).

toBuilderMethodsback to summary
public Set<javax.lang.model.element.ExecutableElement> toBuilderMethods()

Returns abstract no-argument methods in the @AutoValue class that return the builder type.

Consider a class like this:

  @AutoValue abstract class Foo {
    abstract String bar();

    abstract Builder toBuilder();

    ...
    @AutoValue.Builder
    abstract static class Builder {...}
  }

Here toBuilderMethods() will return a set containing the method Foo.toBuilder().

com.google.auto.value.extension back to summary

public Interface AutoValueExtension.Context

Known Direct Implementers
com.google.auto.value.processor.ExtensionContext

The context of the generation cycle.

Method Summary

Modifier and TypeMethod and Description
public Set<javax.lang.model.element.ExecutableElement>
abstractMethods()

Returns the complete set of abstract methods defined in or inherited by the @AutoValue class.

public javax.lang.model.element.TypeElement
autoValueClass()

Returns the annotated class that this generation cycle is based on.

public default Optional<AutoValueExtension.BuilderContext>
builder()

Returns a representation of the Builder associated with the @AutoValue class, if there is one.

public default List<javax.lang.model.element.AnnotationMirror>
classAnnotationsToCopy(javax.lang.model.element.TypeElement classToCopyFrom)

Returns the complete list of annotations defined on the classToCopyFrom that should be added to any generated subclass.

public default String
finalAutoValueClassName()

The fully-qualified name of the last class in the AutoValue hierarchy.

public default List<javax.lang.model.element.AnnotationMirror>
methodAnnotationsToCopy(javax.lang.model.element.ExecutableElement method)

Returns the complete list of annotations defined on the method that should be applied to any override of that method.

public String
packageName()

Returns the package name of the classes to be generated.

public javax.annotation.processing.ProcessingEnvironment
processingEnvironment()

Returns the processing environment of this generation cycle.

public Map<String, javax.lang.model.element.ExecutableElement>
properties()

Returns the ordered collection of properties to be generated by AutoValue.

public default Map<String, javax.lang.model.type.TypeMirror>
propertyTypes()

Returns the properties to be generated by AutoValue, with their types.

Method Detail

abstractMethodsback to summary
public Set<javax.lang.model.element.ExecutableElement> abstractMethods()

Returns the complete set of abstract methods defined in or inherited by the @AutoValue class. This includes all methods that define properties (like abstract String getBar()), any abstract toBuilder() method, and any other abstract method even if it has been consumed by this or another Extension.

autoValueClassback to summary
public javax.lang.model.element.TypeElement autoValueClass()

Returns the annotated class that this generation cycle is based on.

Given @AutoValue public class Foo {...}, this will be Foo.

builderback to summary
public default Optional<AutoValueExtension.BuilderContext> builder()

Returns a representation of the Builder associated with the @AutoValue class, if there is one.

This method returns Optional#empty() if called from within the applicable method. If an Extension needs Builder information to decide whether it is applicable, it should return true from the applicable method and then return null from the generateClass method if it does not need to generate a class after all.

The default implementation of this method returns Optional#empty() for compatibility with extensions which may have implemented this interface themselves.

classAnnotationsToCopyback to summary
public default List<javax.lang.model.element.AnnotationMirror> classAnnotationsToCopy(javax.lang.model.element.TypeElement classToCopyFrom)

Returns the complete list of annotations defined on the classToCopyFrom that should be added to any generated subclass. Only annotations visible to the @AutoValue will be present. See AutoValue.CopyAnnotations for more information.

The default implementation of this method returns an empty list for compatibility with extensions which may have implemented this interface themselves.

finalAutoValueClassNameback to summary
public default String finalAutoValueClassName()

The fully-qualified name of the last class in the AutoValue hierarchy. For an @AutoValue class foo.bar.Baz, this will be foo.bar.AutoValue_Baz. The class may be generated by an extension, which will be the current extension if the isFinal parameter to AutoValueExtension#generateClass is true and the returned string is not null.

For compatibility reasons, this method has a default implementation that throws an exception. The AutoValue processor supplies an implementation that behaves as documented.

methodAnnotationsToCopyback to summary
public default List<javax.lang.model.element.AnnotationMirror> methodAnnotationsToCopy(javax.lang.model.element.ExecutableElement method)

Returns the complete list of annotations defined on the method that should be applied to any override of that method. Only annotations visible to the @AutoValue will be present. See AutoValue.CopyAnnotations for more information.

The default implementation of this method returns an empty list for compatibility with extensions which may have implemented this interface themselves.

packageNameback to summary
public String packageName()

Returns the package name of the classes to be generated.

processingEnvironmentback to summary
public javax.annotation.processing.ProcessingEnvironment processingEnvironment()

Returns the processing environment of this generation cycle. This can be used, among other things, to produce compilation warnings or errors, using ProcessingEnvironment#getMessager().

propertiesback to summary
public Map<String, javax.lang.model.element.ExecutableElement> properties()

Returns the ordered collection of properties to be generated by AutoValue. Each key is a property name, and the corresponding value is the getter method for that property. For example, if property bar is defined by abstract String getBar() then this map will have an entry mapping "bar" to the ExecutableElement for getBar().

To determine the type of a property, it is best to use propertyTypes() rather than looking at the return type of the ExecutableElement in this map. The reason is that the final type of the property might be different because of type variables. For example, if you have...

  interface Parent<T> {
    T bar();
  }
 @AutoValue abstract class Foo implements Parent<String> {...}
...then the type of the bar property in Foo is actually String, but the ExecutableElement will be the the method in Parent, whose return type is T.
propertyTypesback to summary
public default Map<String, javax.lang.model.type.TypeMirror> propertyTypes()

Returns the properties to be generated by AutoValue, with their types. Each key is a property name, and the corresponding value is the type of that property. The order of the map entries is the same as the order of the @AutoValue properties.

For example, if property bar is defined by abstract String getBar() then this map will have an entry mapping "bar" to the TypeMirror for String.

For compatibility reasons, this method has a default implementation that throws an exception. The AutoValue processor supplies an implementation that behaves as documented.

com.google.auto.value.extension back to summary

public final Enum AutoValueExtension.IncrementalExtensionType

extends Enum<AutoValueExtension.IncrementalExtensionType>
Class Inheritance

Indicates to an annotation processor environment supporting incremental annotation processing (currently a feature specific to Gradle starting with version 4.8) the incremental type of an Extension.

The constants for this enum are ordered by increasing performance (but also constraints).

See Also
Gradle documentation of its incremental annotation processing

Field Summary

Modifier and TypeField and Description
public static final AutoValueExtension.IncrementalExtensionType
AGGREGATING

This extension is aggregating, meaning that it may generate outputs based on several annotated input classes and it respects the constraints imposed on aggregating processors.

public static final AutoValueExtension.IncrementalExtensionType
ISOLATING

This extension is isolating, meaning roughly that its output depends on the @AutoValue class and its dependencies, but not on other @AutoValue classes that might be compiled at the same time.

public static final AutoValueExtension.IncrementalExtensionType
UNKNOWN

The incrementality of this extension is unknown, or it is neither aggregating nor isolating.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static AutoValueExtension.IncrementalExtensionType
public static AutoValueExtension.IncrementalExtensionType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

AGGREGATINGback to summary
public static final AutoValueExtension.IncrementalExtensionType AGGREGATING

This extension is aggregating, meaning that it may generate outputs based on several annotated input classes and it respects the constraints imposed on aggregating processors. It is unusual for AutoValue extensions to be aggregating.

See Also
Gradle definition of aggregating processors
ISOLATINGback to summary
public static final AutoValueExtension.IncrementalExtensionType ISOLATING

This extension is isolating, meaning roughly that its output depends on the @AutoValue class and its dependencies, but not on other @AutoValue classes that might be compiled at the same time. The constraints that an isolating extension must respect are the same as those that Gradle imposes on an isolating annotation processor.

See Also
Gradle definition of isolating processors
UNKNOWNback to summary
public static final AutoValueExtension.IncrementalExtensionType UNKNOWN

The incrementality of this extension is unknown, or it is neither aggregating nor isolating.

Constructor Detail

IncrementalExtensionTypeback to summary
private IncrementalExtensionType()

Method Detail

valueOfback to summary
public static AutoValueExtension.IncrementalExtensionType valueOf(String name)
valuesback to summary
public static AutoValueExtension.IncrementalExtensionType[] values()