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 #2522.
  • Loading branch information
raphw committed Mar 16, 2022
1 parent cb948d2 commit 81bc5bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Expand Up @@ -4,6 +4,12 @@
*/
package org.mockito.internal.creation.bytebuddy;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.*;
import static net.bytebuddy.matcher.ElementMatchers.named;

public interface BytecodeGenerator {

<T> Class<? extends T> mockClass(MockFeatures<T> features);
Expand All @@ -13,4 +19,9 @@ public interface BytecodeGenerator {
void mockClassStatic(Class<?> type);

default void clearAllCaches() {}

static ElementMatcher<MethodDescription> isGroovyMethod() {
return isDeclaredBy(named("groovy.lang.GroovyObjectSupport"))
.or(isAnnotatedWith(named("groovy.transform.Internal")));
}
}
Expand Up @@ -117,15 +117,18 @@ public InlineBytecodeGenerator(
.or(isEquals())
.or(isDefaultFinalizer())))
.and(
not(
isDeclaredBy(nameStartsWith("java."))
not(isDeclaredBy(nameStartsWith("java."))
.<MethodDescription>and(
isPackagePrivate()))),
isPackagePrivate()))
.and(
not(
BytecodeGenerator
.isGroovyMethod()))),
Advice.withCustomMapping()
.bind(MockMethodAdvice.Identifier.class, identifier)
.to(MockMethodAdvice.class))
.method(
isStatic(),
isStatic().and(not(BytecodeGenerator.isGroovyMethod())),
Advice.withCustomMapping()
.bind(MockMethodAdvice.Identifier.class, identifier)
.to(MockMethodAdvice.ForStatic.class))
Expand Down
Expand Up @@ -224,7 +224,7 @@ public <T> Class<? extends T> mockClass(MockFeatures<T> features) {
byteBuddy
.subclass(features.mockedType)
.name(name)
.ignoreAlso(isGroovyMethod())
.ignoreAlso(BytecodeGenerator.isGroovyMethod())
.annotateType(
features.stripAnnotations
? new Annotation[0]
Expand Down Expand Up @@ -293,11 +293,6 @@ private <T> Collection<Class<? super T>> getAllTypes(Class<T> type) {
return supertypes;
}

private static ElementMatcher<MethodDescription> isGroovyMethod() {
return isDeclaredBy(named("groovy.lang.GroovyObjectSupport"))
.or(isAnnotatedWith(named("groovy.transform.Internal")));
}

private boolean isComingFromJDK(Class<?> type) {
// Comes from the manifest entry :
// Implementation-Title: Java Runtime Environment
Expand Down

0 comments on commit 81bc5bd

Please sign in to comment.