Skip to content

Commit 786a17e

Browse files
committed
Add missing tests.
1 parent 5270eb7 commit 786a17e

6 files changed

Lines changed: 129 additions & 0 deletions

File tree

byte-buddy-dep/src/main/java/net/bytebuddy/implementation/bind/annotation/DynamicConstant.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2014 - Present Rafael Winterhalter
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package net.bytebuddy.implementation.bind.annotation;
217

318
import net.bytebuddy.description.annotation.AnnotationDescription;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.bytebuddy.test.precompiled.v11;
2+
3+
import net.bytebuddy.implementation.bind.annotation.DynamicConstant;
4+
import net.bytebuddy.utility.JavaConstant;
5+
6+
import java.lang.invoke.MethodHandles;
7+
8+
public class MethodDelegationDynamicConstant {
9+
10+
public MethodDelegationDynamicConstant foo() {
11+
return null;
12+
}
13+
14+
public static MethodDelegationDynamicConstant intercept(@DynamicConstant(
15+
bootstrapType = JavaConstant.MethodHandle.HandleType.INVOKE_STATIC,
16+
bootstrapName = "constantdynamic",
17+
bootstrapOwner = MethodDelegationDynamicConstant.class,
18+
bootstrapReturnType = MethodDelegationDynamicConstant.class,
19+
bootstrapParameterTypes = {MethodHandles.Lookup.class, String.class, Object[].class}) MethodDelegationDynamicConstant constant) throws Throwable {
20+
return constant;
21+
}
22+
23+
public static MethodDelegationDynamicConstant constantdynamic(MethodHandles.Lookup lookup, String name, Object... args) {
24+
return new MethodDelegationDynamicConstant();
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.bytebuddy.test.precompiled.v7;
2+
3+
import net.bytebuddy.implementation.bind.annotation.DynamicConstant;
4+
import net.bytebuddy.utility.JavaConstant;
5+
6+
import java.lang.invoke.CallSite;
7+
import java.lang.invoke.ConstantCallSite;
8+
import java.lang.invoke.MethodHandles;
9+
10+
public class MethodDelegationDynamicConstant {
11+
12+
public MethodDelegationDynamicConstant foo() {
13+
return null;
14+
}
15+
16+
public static MethodDelegationDynamicConstant intercept(@DynamicConstant(
17+
bootstrapType = JavaConstant.MethodHandle.HandleType.INVOKE_STATIC,
18+
bootstrapName = "invokedynamic",
19+
bootstrapOwner = MethodDelegationDynamicConstant.class,
20+
bootstrapReturnType = CallSite.class,
21+
bootstrapParameterTypes = Object[].class,
22+
invokedynamic = true) MethodDelegationDynamicConstant constant) throws Throwable {
23+
return constant;
24+
}
25+
26+
public static CallSite invokedynamic(Object... args) {
27+
return new ConstantCallSite(MethodHandles.constant(MethodDelegationDynamicConstant.class, new MethodDelegationDynamicConstant()));
28+
}
29+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package net.bytebuddy.implementation;
2+
3+
import net.bytebuddy.ByteBuddy;
4+
import net.bytebuddy.dynamic.DynamicType;
5+
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
6+
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
7+
import net.bytebuddy.implementation.bind.annotation.SuperCall;
8+
import net.bytebuddy.test.utility.CallTraceable;
9+
import net.bytebuddy.test.utility.JavaVersionRule;
10+
import org.hamcrest.CoreMatchers;
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.junit.rules.MethodRule;
14+
15+
import java.io.Serializable;
16+
import java.lang.reflect.Method;
17+
import java.util.concurrent.Callable;
18+
19+
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
20+
import static org.hamcrest.CoreMatchers.instanceOf;
21+
import static org.hamcrest.CoreMatchers.is;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
24+
public class MethodDelegationDynamicConstantTest {
25+
private static final String FOO = "foo", BAR = "bar";
26+
27+
@Rule
28+
public MethodRule javaVersionRule = new JavaVersionRule();
29+
30+
@Test
31+
@JavaVersionRule.Enforce(7)
32+
public void testDynamicConstantInvokedynamic() throws Exception {
33+
Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.v7.MethodDelegationDynamicConstant");
34+
Class<?> type = new ByteBuddy()
35+
.subclass(bootstrap)
36+
.method(isDeclaredBy(bootstrap))
37+
.intercept(MethodDelegation.to(bootstrap))
38+
.make()
39+
.load(bootstrap.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
40+
.getLoaded();
41+
Object instance = type.getDeclaredConstructor().newInstance();
42+
assertThat(type.getMethod(FOO).invoke(instance), instanceOf(bootstrap));
43+
}
44+
45+
@Test
46+
@JavaVersionRule.Enforce(11)
47+
public void testDynamicConstant() throws Exception {
48+
Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.v11.MethodDelegationDynamicConstant");
49+
Class<?> type = new ByteBuddy()
50+
.subclass(bootstrap)
51+
.method(isDeclaredBy(bootstrap))
52+
.intercept(MethodDelegation.to(bootstrap))
53+
.make()
54+
.load(bootstrap.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
55+
.getLoaded();
56+
Object instance = type.getDeclaredConstructor().newInstance();
57+
assertThat(type.getMethod(FOO).invoke(instance), instanceOf(bootstrap));
58+
}
59+
}

0 commit comments

Comments
 (0)