diff --git a/value/src/main/java/com/google/auto/value/extension/AutoValueExtension.java b/value/src/main/java/com/google/auto/value/extension/AutoValueExtension.java index 045abc230f..2c8a3fb0fd 100644 --- a/value/src/main/java/com/google/auto/value/extension/AutoValueExtension.java +++ b/value/src/main/java/com/google/auto/value/extension/AutoValueExtension.java @@ -94,6 +94,20 @@ public interface Context { */ TypeElement autoValueClass(); + /** + * The fully-qualified name of the last class in the {@code AutoValue} hierarchy. For an + * {@code @AutoValue} class {@code foo.bar.Baz}, this will be {@code foo.bar.AutoValue_Baz}. + * The class may be generated by an extension, which will be the current extension if the + * {@code isFinal} parameter to {@link AutoValueExtension#generateClass} is true and the + * returned string is not {@code null}. + * + *

For compatibility reasons, this method has a default implementation that throws an + * exception. The AutoValue processor supplies an implementation that behaves as documented. + */ + default String finalAutoValueClassName() { + throw new UnsupportedOperationException(); + } + /** * 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 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 84b6146276..a60eef693b 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 @@ -151,7 +151,7 @@ private static ImmutableSet optionsFor( throw new AssertionError(incrementalType); } - private static String generatedSubclassName(TypeElement type, int depth) { + static String generatedSubclassName(TypeElement type, int depth) { return generatedClassName(type, Strings.repeat("$", depth) + "AutoValue_"); } diff --git a/value/src/main/java/com/google/auto/value/processor/ExtensionContext.java b/value/src/main/java/com/google/auto/value/processor/ExtensionContext.java index 47b4efcd5d..da844b0eed 100644 --- a/value/src/main/java/com/google/auto/value/processor/ExtensionContext.java +++ b/value/src/main/java/com/google/auto/value/processor/ExtensionContext.java @@ -31,7 +31,7 @@ class ExtensionContext implements AutoValueExtension.Context { private final ProcessingEnvironment processingEnvironment; - private final TypeElement typeElement; + private final TypeElement autoValueClass; private final ImmutableMap properties; private final ImmutableMap propertyTypes; private final ImmutableSet abstractMethods; @@ -39,12 +39,12 @@ class ExtensionContext implements AutoValueExtension.Context { ExtensionContext( ProcessingEnvironment processingEnvironment, - TypeElement typeElement, + TypeElement autoValueClass, ImmutableMap properties, ImmutableMap propertyMethodsAndTypes, ImmutableSet abstractMethods) { this.processingEnvironment = processingEnvironment; - this.typeElement = typeElement; + this.autoValueClass = autoValueClass; this.properties = properties; this.propertyTypes = ImmutableMap.copyOf(Maps.transformValues(properties, propertyMethodsAndTypes::get)); @@ -62,12 +62,17 @@ public ProcessingEnvironment processingEnvironment() { @Override public String packageName() { - return TypeSimplifier.packageNameOf(typeElement); + return TypeSimplifier.packageNameOf(autoValueClass); } @Override public TypeElement autoValueClass() { - return typeElement; + return autoValueClass; + } + + @Override + public String finalAutoValueClassName() { + return AutoValueProcessor.generatedSubclassName(autoValueClass, 0); } @Override diff --git a/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java b/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java index d095e5042e..dabe32b95a 100644 --- a/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java +++ b/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java @@ -925,6 +925,31 @@ public void propertyTypes() { .compilesWithoutError(); } + @Test + public void finalAutoValueClassName() { + JavaFileObject autoValueClass = JavaFileObjects.forSourceLines( + "foo.bar.Baz", + "package foo.bar;", + "", + "import com.google.auto.value.AutoValue;", + "", + "@AutoValue", + "abstract class Baz {", + "}"); + ContextChecker checker = + context -> { + assertThat(context.finalAutoValueClassName()).isEqualTo("foo.bar.AutoValue_Baz"); + }; + ContextCheckingExtension extension = new ContextCheckingExtension(checker); + assertThat(autoValueClass) + .processedWith(new AutoValueProcessor(ImmutableList.of(extension, new FinalExtension()))) + .compilesWithoutError() + .and() + .generatesFileNamed(StandardLocation.SOURCE_OUTPUT, "foo.bar", "AutoValue_Baz.java"); + // ContextCheckingExtension doesn't generate any code, so that name must be the class generated + // by FinalExtension. + } + @Test public void builderContext() { JavaFileObject parent = JavaFileObjects.forSourceLines(