Skip to content

Commit

Permalink
Fix an NPE when adding a ToPrettyString annotation to an `@AutoValu…
Browse files Browse the repository at this point in the history
…e` with a generic property

RELNOTES=n/a
PiperOrigin-RevId: 655236646
  • Loading branch information
lukesandberg authored and Google Java Core Libraries committed Jul 23, 2024
1 parent 74e5dc3 commit 5cfba12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ static class KindVisitor extends SimpleTypeVisitor8<PrettyPrintableKind, Void> {
private final Types types;

KindVisitor(Types types, Elements elements) {
super(REGULAR_OBJECT); // default value, covers generic types
this.types = types;
this.elements = elements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,4 +958,24 @@ public void javaBeans() {
assertThat(valueType.toString())
.isEqualTo("JavaBeans{int=4, boolean=false, notAJavaIdentifier=not}");
}

@AutoValue
abstract static class Generic<T> {
abstract T get();

@ToPrettyString
abstract String toPrettyString();
}

@Test
public void generic() {
Generic<String> valueType = new AutoValue_ToPrettyStringTest_Generic<>("hello");

assertThat(valueType.toPrettyString()).isEqualTo("Generic {\n get = hello,\n}");

// Check to make sure that we use the same property names that AutoValue does. This is mostly
// defensive, since in some scenarios AutoValue considers the property names of a java bean as
// having the prefix removed.
assertThat(valueType.toString()).isEqualTo("Generic{get=hello}");
}
}

0 comments on commit 5cfba12

Please sign in to comment.