Skip to content

Commit

Permalink
Fixes #1717: configure the MethodVisitor for Java 11+ compatibility (#…
Browse files Browse the repository at this point in the history
…1718)

By using Byte Buddy's supported ASM API version, the visitor will always be
compatible with the currently supported bytecode, including when using the
experimental feature.

[ci maven-central-release]
  • Loading branch information
fpavageau authored and TimvdLippe committed May 24, 2019
1 parent 71603d4 commit d86b9a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.ClassVisitor;
import net.bytebuddy.jar.asm.MethodVisitor;
import net.bytebuddy.jar.asm.Opcodes;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.utility.OpenedClassReader;
Expand Down Expand Up @@ -311,7 +310,7 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
private static class MethodParameterStrippingMethodVisitor extends MethodVisitor {

public MethodParameterStrippingMethodVisitor(MethodVisitor mv) {
super(Opcodes.ASM5, mv);
super(OpenedClassReader.ASM_API, mv);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.StubMethod;
import net.bytebuddy.utility.JavaConstant;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.creation.MockSettingsImpl;
Expand All @@ -25,9 +27,11 @@
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;

import static net.bytebuddy.ClassFileVersion.JAVA_V8;
import static net.bytebuddy.ClassFileVersion.JAVA_V11;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
Expand Down Expand Up @@ -291,6 +295,25 @@ public void test_parameters_retention() throws Exception {
.getOnly().getParameters().getOnly().getName()).isEqualTo("bar");
}

@Test
public void test_constant_dynamic_compatibility() throws Exception {
assumeTrue(ClassFileVersion.ofThisVm().isAtLeast(JAVA_V11));

Class<?> typeWithCondy = new ByteBuddy()
.subclass(Callable.class)
.method(named("call"))
.intercept(FixedValue.value(JavaConstant.Dynamic.ofNullConstant()))
.make()
.load(null)
.getLoaded();

MockCreationSettings<?> settings = settingsFor(typeWithCondy);
@SuppressWarnings("unchecked")
Object proxy = mockMaker.createMock(settings, new MockHandlerImpl(settings));

assertThat(proxy.getClass()).isEqualTo(typeWithCondy);
}

@Test
public void test_clear_mock_clears_handler() {
MockCreationSettings<GenericSubClass> settings = settingsFor(GenericSubClass.class);
Expand Down

0 comments on commit d86b9a3

Please sign in to comment.