Skip to content

Commit

Permalink
Add ignore matcher for Groovy meta methods for inline Byte Buddy mock…
Browse files Browse the repository at this point in the history
… maker. Fixes mockito#2522.
  • Loading branch information
raphw authored and stevegreenn committed Mar 20, 2022
1 parent b9f01a8 commit 0c677a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Expand Up @@ -20,8 +20,15 @@ public interface BytecodeGenerator {

default void clearAllCaches() {}

static ElementMatcher<MethodDescription> isGroovyMethod() {
return isDeclaredBy(named("groovy.lang.GroovyObjectSupport"))
.or(isAnnotatedWith(named("groovy.transform.Internal")));
static ElementMatcher<MethodDescription> isGroovyMethod(boolean inline) {
ElementMatcher.Junction<MethodDescription> matcher =
isDeclaredBy(named("groovy.lang.GroovyObjectSupport"))
.or(isAnnotatedWith(named("groovy.transform.Internal")));
if (inline) {
return matcher.or(
named("$getStaticMetaClass").and(returns(named("groovy.lang.MetaClass"))));
} else {
return matcher;
}
}
}
Expand Up @@ -117,15 +117,19 @@ public InlineBytecodeGenerator(
.or(isEquals())
.or(isDefaultFinalizer())))
.and(
not(
isDeclaredBy(nameStartsWith("java."))
not(isDeclaredBy(nameStartsWith("java."))
.<MethodDescription>and(
isPackagePrivate()))),
isPackagePrivate()))
.and(
not(
BytecodeGenerator
.isGroovyMethod(
true)))),
Advice.withCustomMapping()
.bind(MockMethodAdvice.Identifier.class, identifier)
.to(MockMethodAdvice.class))
.method(
isStatic(),
isStatic().and(not(BytecodeGenerator.isGroovyMethod(true))),
Advice.withCustomMapping()
.bind(MockMethodAdvice.Identifier.class, identifier)
.to(MockMethodAdvice.ForStatic.class))
Expand Down
Expand Up @@ -12,9 +12,12 @@
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.hasParameters;
import static net.bytebuddy.matcher.ElementMatchers.hasType;
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
import static net.bytebuddy.matcher.ElementMatchers.isEquals;
import static net.bytebuddy.matcher.ElementMatchers.isHashCode;
import static net.bytebuddy.matcher.ElementMatchers.isPackagePrivate;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.whereAny;
import static org.mockito.internal.util.StringUtil.join;
Expand Down Expand Up @@ -221,7 +224,7 @@ public <T> Class<? extends T> mockClass(MockFeatures<T> features) {
byteBuddy
.subclass(features.mockedType)
.name(name)
.ignoreAlso(BytecodeGenerator.isGroovyMethod())
.ignoreAlso(BytecodeGenerator.isGroovyMethod(false))
.annotateType(
features.stripAnnotations
? new Annotation[0]
Expand Down

0 comments on commit 0c677a2

Please sign in to comment.