Skip to content

Commit

Permalink
Ensure type names in AutoOneOf generated code are qualified if needed.
Browse files Browse the repository at this point in the history
If the parent class has a nested type called `String` or `Object` then that is
what plain `String` or `Object` in the generated code will refer to. That's
pretty unlikely to happen, but we should still handle it properly. The use of
backticks in the template means that the fully-qualified name will be used if
necessary.

RELNOTES=n/a
PiperOrigin-RevId: 658406410
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Aug 6, 2024
1 parent 27c3118 commit 7d92c59
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,28 @@ public void arrayToString() {
OneOfArray oneOfArray = OneOfArray.ofInts(new int[] {1, 2});
assertThat(oneOfArray.toString()).isEqualTo("OneOfArray{ints=[1, 2]}");
}

@AutoOneOf(OneOfFunkyString.Kind.class)
public abstract static class OneOfFunkyString {
public enum Kind {
FUNKY_STRING
}

public static class String {}

public abstract Kind getKind();

public abstract String funkyString();

public static OneOfFunkyString ofFunkyString(String s) {
return AutoOneOf_AutoOneOfTest_OneOfFunkyString.funkyString(s);
}
}

@Test
public void funkyString() {
OneOfFunkyString oneOfFunkyString =
OneOfFunkyString.ofFunkyString(new OneOfFunkyString.String());
assertThat(oneOfFunkyString.funkyString()).isNotNull();
}
}
12 changes: 5 additions & 7 deletions value/src/main/java/com/google/auto/value/processor/autooneof.vm
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ final class $generatedClass {

#if (!$p.kind.primitive)

if ($p == null) {
throw new NullPointerException();
}
`java.util.Objects`.requireNonNull($p);

#end

Expand Down Expand Up @@ -139,7 +137,7 @@ final class $generatedClass {

#if ($serializable)

private Object readResolve() {
private `java.lang.Object` readResolve() {
return INSTANCE;
}

Expand All @@ -148,7 +146,7 @@ final class $generatedClass {
#if ($toString)

@`java.lang.Override`
public String toString() {
public `java.lang.String` toString() {
return "${simpleClassName}{$p.name}";
}

Expand All @@ -171,7 +169,7 @@ final class $generatedClass {

@`java.lang.Override`
public int hashCode() {
return System.identityHashCode(this);
return `java.lang.System`.identityHashCode(this);
}

#end
Expand All @@ -192,7 +190,7 @@ final class $generatedClass {
#if ($toString)

@`java.lang.Override`
public String toString() {
public `java.lang.String` toString() {
return "${simpleClassName}{$p.name=" ##
+ #if ($p.kind == "ARRAY") `java.util.Arrays`.toString(this.$p) #else this.$p #end
+ "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,20 @@ public void success() {
"foo.bar.AutoOneOf_TaskResult",
"package foo.bar;",
"",
"import java.util.Objects;",
GeneratedImport.importGeneratedAnnotationType(),
"",
"@Generated(\"com.google.auto.value.processor.AutoOneOfProcessor\")",
"final class AutoOneOf_TaskResult {",
" private AutoOneOf_TaskResult() {} // There are no instances of this type.",
"",
" static <V, T extends Throwable> TaskResult<V, T> value(V value) {",
" if (value == null) {",
" throw new NullPointerException();",
" }",
" Objects.requireNonNull(value);",
" return new Impl_value<V, T>(value);",
" }",
"",
" static <V, T extends Throwable> TaskResult<V, T> exception(Throwable exception) {",
" if (exception == null) {",
" throw new NullPointerException();",
" }",
" Objects.requireNonNull(exception);",
" return new Impl_exception<V, T>(exception);",
" }",
"",
Expand Down

0 comments on commit 7d92c59

Please sign in to comment.