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 committed Jan 12, 2022
1 parent cb948d2 commit 43ac1c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 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 @@ -387,6 +387,7 @@ public byte[] transform(
// ,ClassFileLocator.ForClassLoader.ofSystemLoader()
// )
)
.ignoreAlso(BytecodeGenerator.isGroovyMethod())
// Note: The VM erases parameter meta data from the provided class file
// (bug). We just add this information manually.
.visit(new ParameterWritingVisitorWrapper(classBeingRedefined))
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 43ac1c8

Please sign in to comment.