Skip to content

Commit cd3b6ca

Browse files
committed
Don't generate visibility bridge for package private default implementations
1 parent 0eefb0f commit cd3b6ca

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

modules/mockk-agent/src/jvmMain/kotlin/io/mockk/proxy/jvm/transformation/InliningClassTransformer.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import net.bytebuddy.asm.AsmVisitorWrapper
1515
import net.bytebuddy.description.ModifierReviewable.OfByteCodeElement
1616
import net.bytebuddy.description.method.MethodDescription
1717
import net.bytebuddy.dynamic.ClassFileLocator.Simple.of
18+
import net.bytebuddy.dynamic.VisibilityBridgeStrategy
1819
import net.bytebuddy.matcher.ElementMatchers.*
1920
import java.io.File
2021
import java.lang.instrument.ClassFileTransformer
@@ -71,7 +72,9 @@ internal class InliningClassTransformer(
7172
?: return classfileBuffer
7273

7374
try {
74-
val builder = byteBuddy.redefine(classBeingRedefined, of(classBeingRedefined.name, classfileBuffer))
75+
val builder = byteBuddy
76+
.with(VisibilityBridgeStrategy { not(isDefaultMethod()).matches(it) })
77+
.redefine(classBeingRedefined, of(classBeingRedefined.name, classfileBuffer))
7578
.visit(FixParameterNamesVisitor(classBeingRedefined))
7679

7780
val type = builder

modules/mockk-agent/src/jvmTest/java/io/mockk/proxy/JvmMockKProxyMakerTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,25 @@ public void openClassAnnotationProxy() throws Exception {
411411
checkProxyHandlerCalled(1, proxy, "i", 1, 1);
412412
}
413413

414+
interface InterfaceWithDefault {
415+
default void foo(Runnable d) {
416+
d.run();
417+
}
418+
}
419+
420+
static public class ClassImplementingPackagePrivateInterfaceWithDefaultMethod implements InterfaceWithDefault {
421+
}
422+
423+
@Test
424+
public void classImplementingPackagePrivateInterfaceWithDefaultMethodProxy() {
425+
ClassImplementingPackagePrivateInterfaceWithDefaultMethod proxy = makeProxy(ClassImplementingPackagePrivateInterfaceWithDefaultMethod.class);
426+
427+
proxy.foo(() -> { executed[0] = true; });
428+
429+
assertFalse(executed[0]);
430+
checkProxyHandlerCalled(1, proxy, "foo", 1, 1);
431+
}
432+
414433
private void checkProxyHandlerCalled(int nTimes, Object proxy, String methodName) {
415434
checkProxyHandlerCalled(nTimes, proxy, methodName, 0, 0);
416435
}

test-modules/client-tests/src/jvmTest/java/io/mockk/core/ClassImplementingInterfaceWithDefaultMethod.java

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.mockk.core;
2+
3+
4+
interface InterfaceWithDefault {
5+
default int foo() {
6+
return 10;
7+
}
8+
}
9+
10+
public class ClassImplementingPackagePrivateInterfaceWithDefaultMethod implements InterfaceWithDefault {
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.mockk.impl
22

3-
import io.mockk.core.ClassImplementingInterfaceWithDefaultMethod
3+
import io.mockk.core.ClassImplementingPackagePrivateInterfaceWithDefaultMethod
44
import io.mockk.every
55
import io.mockk.mockk
66
import org.junit.jupiter.api.Assertions.assertEquals
@@ -9,9 +9,9 @@ import org.junit.jupiter.api.Test
99

1010
class DefaultMethodTest {
1111
@Test
12-
fun `should mock ClassImplementingInterfaceWithDefaultMethod class`() {
13-
val tableImpl = mockk<ClassImplementingInterfaceWithDefaultMethod>()
14-
every { tableImpl.foo() } returns 12
15-
assertEquals(12, tableImpl.toString())
12+
fun `should mock ClassImplementingPackagePrivateInterfaceWithDefaultMethod class`() {
13+
val obj = mockk<ClassImplementingPackagePrivateInterfaceWithDefaultMethod>()
14+
every { obj.foo() } returns 12
15+
assertEquals(12, obj.foo())
1616
}
1717
}

0 commit comments

Comments
 (0)