Skip to content

Commit

Permalink
When scanning a Builder for referenced types, don't include non-abstr…
Browse files Browse the repository at this point in the history
…act methods. The referenced types are used to determine the imports that will be needed by the implementing class, and that class never references non-abstract methods.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=124980576
  • Loading branch information
eamonnmcmanus committed Aug 30, 2016
1 parent 660ea56 commit 07d6fdd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,19 @@ ImmutableSet<ExecutableElement> toBuilderMethods(
return builderMethods;
}

/**
* Returns the types that are referenced by abstract methods in the builder, either as
* parameters or as return types.
*/
Set<TypeMirror> referencedTypes() {
Set<TypeMirror> types = new TypeMirrorSet();
for (ExecutableElement method :
ElementFilter.methodsIn(builderTypeElement.getEnclosedElements())) {
types.add(method.getReturnType());
for (VariableElement parameter : method.getParameters()) {
types.add(parameter.asType());
if (method.getModifiers().contains(Modifier.ABSTRACT)) {
types.add(method.getReturnType());
for (VariableElement parameter : method.getParameters()) {
types.add(parameter.asType());
}
}
}
return types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ public void correctBuilder() throws Exception {
"import com.google.common.base.Optional;",
"import com.google.common.collect.ImmutableList;",
"",
"import java.util.ArrayList;",
"import java.util.List;",
"import javax.annotation.Nullable;",
"",
Expand All @@ -607,21 +608,26 @@ public void correctBuilder() throws Exception {
" public abstract Builder<T> toBuilder();",
"",
" @AutoValue.Builder",
" public interface Builder<T extends Number> {",
" Builder<T> anInt(int x);",
" Builder<T> aByteArray(byte[] x);",
" Builder<T> aNullableIntArray(@Nullable int[] x);",
" Builder<T> aList(List<T> x);",
" Builder<T> anImmutableList(List<T> x);",
" ImmutableList.Builder<T> anImmutableListBuilder();",
" Builder<T> anOptionalString(Optional<String> s);",
" Builder<T> anOptionalString(String s);",
" public abstract static class Builder<T extends Number> {",
" public abstract Builder<T> anInt(int x);",
" public abstract Builder<T> aByteArray(byte[] x);",
" public abstract Builder<T> aNullableIntArray(@Nullable int[] x);",
" public abstract Builder<T> aList(List<T> x);",
" public abstract Builder<T> anImmutableList(List<T> x);",
" public abstract ImmutableList.Builder<T> anImmutableListBuilder();",
" public abstract Builder<T> anOptionalString(Optional<String> s);",
" public abstract Builder<T> anOptionalString(String s);",
"",
" public Builder<T> aList(ArrayList<T> x) {",
// ArrayList should not be imported in the generated class.
" return aList((List<T>) x);",
" }",
"",
" Optional<Integer> anInt();",
" List<T> aList();",
" ImmutableList<T> anImmutableList();",
" public abstract Optional<Integer> anInt();",
" public abstract List<T> aList();",
" public abstract ImmutableList<T> anImmutableList();",
"",
" Baz<T> build();",
" public abstract Baz<T> build();",
" }",
"",
" public static <T extends Number> Builder<T> builder() {",
Expand Down Expand Up @@ -742,7 +748,7 @@ public void correctBuilder() throws Exception {
" return new Builder<T>(this);",
" }",
"",
" static final class Builder<T extends Number> implements Baz.Builder<T> {",
" static final class Builder<T extends Number> extends Baz.Builder<T> {",
" private Integer anInt;",
" private byte[] aByteArray;",
" private int[] aNullableIntArray;",
Expand Down

0 comments on commit 07d6fdd

Please sign in to comment.