Skip to content

Commit

Permalink
Prefer non-synthetic constructors in MockMethodAdvice.ConstructorShor…
Browse files Browse the repository at this point in the history
…tcut

After moving spy creation to instrumenting constructor chains in
ByteBuddyMockMaker, creating spies for Robolectric-instrumented Android
classes started failing in some situations due to fields not being
copied on base classes (specifically, ContextWrapper.mBase). The problem
is that AsmVisitorWrapper.ForDeclaredMethods does not visit the
synthetic constructrs that are added by Robolectric during runtime.
While the visibility issuer is still being explored, a workaround is to
prefer non-synthetic constructors when selecting which parent
constructor to call in MockMethodAdvice.ConstructorShortcut.

Fixes #2040
  • Loading branch information
hoisie committed Sep 16, 2020
1 parent b6ae6cf commit 6c220a1
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@ public MethodVisitor wrap(
.asErasure()
.getDeclaredMethods()
.filter(isConstructor().and(not(isPrivate())));
MethodList<MethodDescription.InDefinedShape> nonSynthetics =
constructors.filter(not(isSynthetic()));
// Prefer non-synthetic constructors as a workaround to synthetic Robolectric
// generated constructors not being visited by AsmVisitorWrapper.ForDeclaredMethods.
if (nonSynthetics.size() > 0) {
constructors = nonSynthetics;
}
int arguments = Integer.MAX_VALUE;
boolean visible = false;
MethodDescription.InDefinedShape current = null;
Expand Down

0 comments on commit 6c220a1

Please sign in to comment.