Skip to content

Commit a64a77b

Browse files
committed
Correct tests.
1 parent 45f72d9 commit a64a77b

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/asm/MemberSubstitution.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4726,6 +4726,11 @@ class ForDynamicConstant implements OffsetMapping {
47264726
*/
47274727
private final List<JavaConstant> arguments;
47284728

4729+
/**
4730+
* {@code true} if invokedynamic should be used to bind the constant.
4731+
*/
4732+
private final boolean invokedynamic;
4733+
47294734
/**
47304735
* Creates an offset mapping for a dynamic constant.
47314736
*
@@ -4737,6 +4742,7 @@ class ForDynamicConstant implements OffsetMapping {
47374742
* @param bootstrapReturnType The return type of the boostrap method.
47384743
* @param bootstrapParameterTypes The parameter types of the boostrap method.
47394744
* @param arguments The constant arguments to the bootstrap method.
4745+
* @param invokedynamic {@code true} if invokedynamic should be used to bind the constant.
47404746
*/
47414747
public ForDynamicConstant(String name,
47424748
TypeDescription typeDescription,
@@ -4745,7 +4751,8 @@ public ForDynamicConstant(String name,
47454751
String bootstrapName,
47464752
TypeDescription bootstrapReturnType,
47474753
List<? extends TypeDescription> bootstrapParameterTypes,
4748-
List<JavaConstant> arguments) {
4754+
List<JavaConstant> arguments,
4755+
boolean invokedynamic) {
47494756
this.name = name;
47504757
this.typeDescription = typeDescription;
47514758
this.bootstrapType = bootstrapType;
@@ -4754,6 +4761,7 @@ public ForDynamicConstant(String name,
47544761
this.bootstrapReturnType = bootstrapReturnType;
47554762
this.bootstrapParameterTypes = bootstrapParameterTypes;
47564763
this.arguments = arguments;
4764+
this.invokedynamic = invokedynamic;
47574765
}
47584766

47594767
/**
@@ -4763,15 +4771,26 @@ public Resolved resolve(Assigner assigner,
47634771
Assigner.Typing typing,
47644772
TypeDescription instrumentedType,
47654773
MethodDescription instrumentedMethod) {
4766-
return new Resolved.ForStackManipulation(new JavaConstantValue(new JavaConstant.Dynamic(
4767-
name,
4768-
typeDescription,
4769-
new JavaConstant.MethodHandle(bootstrapType,
4770-
bootstrapOwner.represents(void.class) ? instrumentedType : bootstrapOwner,
4771-
bootstrapName,
4772-
bootstrapReturnType,
4773-
bootstrapParameterTypes),
4774-
arguments)));
4774+
if (invokedynamic) {
4775+
return new Resolved.ForStackManipulation(new Invokedynamic(name,
4776+
JavaConstant.MethodType.of(typeDescription),
4777+
new JavaConstant.MethodHandle(bootstrapType,
4778+
bootstrapOwner.represents(void.class) ? instrumentedType : bootstrapOwner,
4779+
bootstrapName,
4780+
bootstrapReturnType,
4781+
bootstrapParameterTypes),
4782+
arguments));
4783+
} else {
4784+
return new Resolved.ForStackManipulation(new JavaConstantValue(new JavaConstant.Dynamic(
4785+
name,
4786+
typeDescription,
4787+
new JavaConstant.MethodHandle(bootstrapType,
4788+
bootstrapOwner.represents(void.class) ? instrumentedType : bootstrapOwner,
4789+
bootstrapName,
4790+
bootstrapReturnType,
4791+
bootstrapParameterTypes),
4792+
arguments)));
4793+
}
47754794
}
47764795

47774796
/**
@@ -4814,6 +4833,11 @@ protected enum Factory implements OffsetMapping.Factory<DynamicConstant> {
48144833
*/
48154834
private static final MethodDescription.InDefinedShape BOOTSTRAP_PARAMETER_TYPES;
48164835

4836+
/**
4837+
* The {@link DynamicConstant#invokedynamic()} method.
4838+
*/
4839+
private static final MethodDescription.InDefinedShape INVOKEDYNAMIC;
4840+
48174841
/*
48184842
* Resolves all annotation properties.
48194843
*/
@@ -4825,6 +4849,7 @@ protected enum Factory implements OffsetMapping.Factory<DynamicConstant> {
48254849
BOOTSTRAP_NAME = methods.filter(named("bootstrapName")).getOnly();
48264850
BOOTSTRAP_RETURN_TYPE = methods.filter(named("bootstrapReturnType")).getOnly();
48274851
BOOTSTRAP_PARAMETER_TYPES = methods.filter(named("bootstrapParameterTypes")).getOnly();
4852+
INVOKEDYNAMIC = methods.filter(named("invokedynamic")).getOnly();
48284853
}
48294854

48304855
/**
@@ -4853,7 +4878,8 @@ public OffsetMapping make(ParameterDescription.InDefinedShape target, Annotation
48534878
annotation.getValue(BOOTSTRAP_NAME).resolve(String.class),
48544879
annotation.getValue(BOOTSTRAP_RETURN_TYPE).resolve(TypeDescription.class),
48554880
Arrays.asList(annotation.getValue(BOOTSTRAP_PARAMETER_TYPES).resolve(TypeDescription[].class)),
4856-
Collections.<JavaConstant>emptyList());
4881+
Collections.<JavaConstant>emptyList(),
4882+
annotation.getValue(INVOKEDYNAMIC).resolve(Boolean.class));
48574883
}
48584884
}
48594885
}

byte-buddy-dep/src/test/java-7/net/bytebuddy/test/precompiled/v7/MemberSubstitutionDynamicConstant.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static MemberSubstitutionDynamicConstant intercept(@MemberSubstitution.Dy
2020
bootstrapName = "invokedynamic",
2121
bootstrapOwner = MemberSubstitutionDynamicConstant.class,
2222
bootstrapReturnType = CallSite.class,
23-
bootstrapParameterTypes = Object[].class) MemberSubstitutionDynamicConstant constant) throws Throwable {
23+
bootstrapParameterTypes = Object[].class,
24+
invokedynamic = true) MemberSubstitutionDynamicConstant constant) throws Throwable {
2425
return constant;
2526
}
2627

byte-buddy-dep/src/test/java/net/bytebuddy/asm/MemberSubstitutionChainWithAnnotationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,12 @@ public void testDynamicConstantInvokedynamic() throws Exception {
321321
.visit(MemberSubstitution.strict()
322322
.field(named(FOO))
323323
.replaceWithChain(MemberSubstitution.Substitution.Chain.Step.ForDelegation.to(bootstrap.getMethod("intercept", bootstrap)))
324-
.on(named(RUN)))
324+
.on(named(FOO)))
325325
.make()
326326
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
327327
.getLoaded();
328328
Object instance = type.getDeclaredConstructor().newInstance();
329-
assertThat(type.getDeclaredMethod(RUN).invoke(instance), instanceOf(bootstrap));
329+
assertThat(type.getDeclaredMethod(FOO).invoke(instance), instanceOf(type));
330330
}
331331

332332
@Test
@@ -338,12 +338,12 @@ public void testDynamicConstant() throws Exception {
338338
.visit(MemberSubstitution.strict()
339339
.field(named(FOO))
340340
.replaceWithChain(MemberSubstitution.Substitution.Chain.Step.ForDelegation.to(bootstrap.getMethod("intercept", bootstrap)))
341-
.on(named(RUN)))
341+
.on(named(FOO)))
342342
.make()
343343
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
344344
.getLoaded();
345345
Object instance = type.getDeclaredConstructor().newInstance();
346-
assertThat(type.getDeclaredMethod(RUN).invoke(instance), instanceOf(bootstrap));
346+
assertThat(type.getDeclaredMethod(FOO).invoke(instance), instanceOf(type));
347347
}
348348

349349
@Test

0 commit comments

Comments
 (0)