Skip to content

Commit

Permalink
Add a way for extensions to retrieve the name of the final AutoValue_…
Browse files Browse the repository at this point in the history
…Foo class.

RELNOTES=Add a way for extensions to retrieve the name of the final AutoValue_Foo class.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=273511598
  • Loading branch information
eamonnmcmanus authored and nick-someone committed Oct 9, 2019
1 parent c9b3553 commit 4543619
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
* <p>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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static ImmutableSet<String> 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_");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@
class ExtensionContext implements AutoValueExtension.Context {

private final ProcessingEnvironment processingEnvironment;
private final TypeElement typeElement;
private final TypeElement autoValueClass;
private final ImmutableMap<String, ExecutableElement> properties;
private final ImmutableMap<String, TypeMirror> propertyTypes;
private final ImmutableSet<ExecutableElement> abstractMethods;
private Optional<BuilderContext> builderContext = Optional.empty();

ExtensionContext(
ProcessingEnvironment processingEnvironment,
TypeElement typeElement,
TypeElement autoValueClass,
ImmutableMap<String, ExecutableElement> properties,
ImmutableMap<ExecutableElement, TypeMirror> propertyMethodsAndTypes,
ImmutableSet<ExecutableElement> abstractMethods) {
this.processingEnvironment = processingEnvironment;
this.typeElement = typeElement;
this.autoValueClass = autoValueClass;
this.properties = properties;
this.propertyTypes =
ImmutableMap.copyOf(Maps.transformValues(properties, propertyMethodsAndTypes::get));
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4543619

Please sign in to comment.