Skip to content

Commit

Permalink
Add missing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 29, 2025
1 parent 5270eb7 commit 786a17e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2014 - Present Rafael Winterhalter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.bytebuddy.implementation.bind.annotation;

import net.bytebuddy.description.annotation.AnnotationDescription;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.bytebuddy.test.precompiled.v11;

import net.bytebuddy.implementation.bind.annotation.DynamicConstant;
import net.bytebuddy.utility.JavaConstant;

import java.lang.invoke.MethodHandles;

public class MethodDelegationDynamicConstant {

public MethodDelegationDynamicConstant foo() {
return null;
}

public static MethodDelegationDynamicConstant intercept(@DynamicConstant(
bootstrapType = JavaConstant.MethodHandle.HandleType.INVOKE_STATIC,
bootstrapName = "constantdynamic",
bootstrapOwner = MethodDelegationDynamicConstant.class,
bootstrapReturnType = MethodDelegationDynamicConstant.class,
bootstrapParameterTypes = {MethodHandles.Lookup.class, String.class, Object[].class}) MethodDelegationDynamicConstant constant) throws Throwable {
return constant;
}

public static MethodDelegationDynamicConstant constantdynamic(MethodHandles.Lookup lookup, String name, Object... args) {
return new MethodDelegationDynamicConstant();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.bytebuddy.test.precompiled.v7;

import net.bytebuddy.implementation.bind.annotation.DynamicConstant;
import net.bytebuddy.utility.JavaConstant;

import java.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandles;

public class MethodDelegationDynamicConstant {

public MethodDelegationDynamicConstant foo() {
return null;
}

public static MethodDelegationDynamicConstant intercept(@DynamicConstant(
bootstrapType = JavaConstant.MethodHandle.HandleType.INVOKE_STATIC,
bootstrapName = "invokedynamic",
bootstrapOwner = MethodDelegationDynamicConstant.class,
bootstrapReturnType = CallSite.class,
bootstrapParameterTypes = Object[].class,
invokedynamic = true) MethodDelegationDynamicConstant constant) throws Throwable {
return constant;
}

public static CallSite invokedynamic(Object... args) {
return new ConstantCallSite(MethodHandles.constant(MethodDelegationDynamicConstant.class, new MethodDelegationDynamicConstant()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.bytebuddy.implementation;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.test.utility.CallTraceable;
import net.bytebuddy.test.utility.JavaVersionRule;
import org.hamcrest.CoreMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;

import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class MethodDelegationDynamicConstantTest {
private static final String FOO = "foo", BAR = "bar";

@Rule
public MethodRule javaVersionRule = new JavaVersionRule();

@Test
@JavaVersionRule.Enforce(7)
public void testDynamicConstantInvokedynamic() throws Exception {
Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.v7.MethodDelegationDynamicConstant");
Class<?> type = new ByteBuddy()
.subclass(bootstrap)
.method(isDeclaredBy(bootstrap))
.intercept(MethodDelegation.to(bootstrap))
.make()
.load(bootstrap.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getDeclaredConstructor().newInstance();
assertThat(type.getMethod(FOO).invoke(instance), instanceOf(bootstrap));
}

@Test
@JavaVersionRule.Enforce(11)
public void testDynamicConstant() throws Exception {
Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.v11.MethodDelegationDynamicConstant");
Class<?> type = new ByteBuddy()
.subclass(bootstrap)
.method(isDeclaredBy(bootstrap))
.intercept(MethodDelegation.to(bootstrap))
.make()
.load(bootstrap.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getDeclaredConstructor().newInstance();
assertThat(type.getMethod(FOO).invoke(instance), instanceOf(bootstrap));
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit 786a17e

Please sign in to comment.