Skip to content

Commit

Permalink
Correct tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 29, 2025
1 parent 45f72d9 commit a64a77b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4726,6 +4726,11 @@ class ForDynamicConstant implements OffsetMapping {
*/
private final List<JavaConstant> arguments;

/**
* {@code true} if invokedynamic should be used to bind the constant.
*/
private final boolean invokedynamic;

/**
* Creates an offset mapping for a dynamic constant.
*
Expand All @@ -4737,6 +4742,7 @@ class ForDynamicConstant implements OffsetMapping {
* @param bootstrapReturnType The return type of the boostrap method.
* @param bootstrapParameterTypes The parameter types of the boostrap method.
* @param arguments The constant arguments to the bootstrap method.
* @param invokedynamic {@code true} if invokedynamic should be used to bind the constant.
*/
public ForDynamicConstant(String name,
TypeDescription typeDescription,
Expand All @@ -4745,7 +4751,8 @@ public ForDynamicConstant(String name,
String bootstrapName,
TypeDescription bootstrapReturnType,
List<? extends TypeDescription> bootstrapParameterTypes,
List<JavaConstant> arguments) {
List<JavaConstant> arguments,
boolean invokedynamic) {
this.name = name;
this.typeDescription = typeDescription;
this.bootstrapType = bootstrapType;
Expand All @@ -4754,6 +4761,7 @@ public ForDynamicConstant(String name,
this.bootstrapReturnType = bootstrapReturnType;
this.bootstrapParameterTypes = bootstrapParameterTypes;
this.arguments = arguments;
this.invokedynamic = invokedynamic;
}

/**
Expand All @@ -4763,15 +4771,26 @@ public Resolved resolve(Assigner assigner,
Assigner.Typing typing,
TypeDescription instrumentedType,
MethodDescription instrumentedMethod) {
return new Resolved.ForStackManipulation(new JavaConstantValue(new JavaConstant.Dynamic(
name,
typeDescription,
new JavaConstant.MethodHandle(bootstrapType,
bootstrapOwner.represents(void.class) ? instrumentedType : bootstrapOwner,
bootstrapName,
bootstrapReturnType,
bootstrapParameterTypes),
arguments)));
if (invokedynamic) {
return new Resolved.ForStackManipulation(new Invokedynamic(name,
JavaConstant.MethodType.of(typeDescription),
new JavaConstant.MethodHandle(bootstrapType,
bootstrapOwner.represents(void.class) ? instrumentedType : bootstrapOwner,
bootstrapName,
bootstrapReturnType,
bootstrapParameterTypes),
arguments));
} else {
return new Resolved.ForStackManipulation(new JavaConstantValue(new JavaConstant.Dynamic(
name,
typeDescription,
new JavaConstant.MethodHandle(bootstrapType,
bootstrapOwner.represents(void.class) ? instrumentedType : bootstrapOwner,
bootstrapName,
bootstrapReturnType,
bootstrapParameterTypes),
arguments)));
}
}

/**
Expand Down Expand Up @@ -4814,6 +4833,11 @@ protected enum Factory implements OffsetMapping.Factory<DynamicConstant> {
*/
private static final MethodDescription.InDefinedShape BOOTSTRAP_PARAMETER_TYPES;

/**
* The {@link DynamicConstant#invokedynamic()} method.
*/
private static final MethodDescription.InDefinedShape INVOKEDYNAMIC;

/*
* Resolves all annotation properties.
*/
Expand All @@ -4825,6 +4849,7 @@ protected enum Factory implements OffsetMapping.Factory<DynamicConstant> {
BOOTSTRAP_NAME = methods.filter(named("bootstrapName")).getOnly();
BOOTSTRAP_RETURN_TYPE = methods.filter(named("bootstrapReturnType")).getOnly();
BOOTSTRAP_PARAMETER_TYPES = methods.filter(named("bootstrapParameterTypes")).getOnly();
INVOKEDYNAMIC = methods.filter(named("invokedynamic")).getOnly();
}

/**
Expand Down Expand Up @@ -4853,7 +4878,8 @@ public OffsetMapping make(ParameterDescription.InDefinedShape target, Annotation
annotation.getValue(BOOTSTRAP_NAME).resolve(String.class),
annotation.getValue(BOOTSTRAP_RETURN_TYPE).resolve(TypeDescription.class),
Arrays.asList(annotation.getValue(BOOTSTRAP_PARAMETER_TYPES).resolve(TypeDescription[].class)),
Collections.<JavaConstant>emptyList());
Collections.<JavaConstant>emptyList(),
annotation.getValue(INVOKEDYNAMIC).resolve(Boolean.class));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public static MemberSubstitutionDynamicConstant intercept(@MemberSubstitution.Dy
bootstrapName = "invokedynamic",
bootstrapOwner = MemberSubstitutionDynamicConstant.class,
bootstrapReturnType = CallSite.class,
bootstrapParameterTypes = Object[].class) MemberSubstitutionDynamicConstant constant) throws Throwable {
bootstrapParameterTypes = Object[].class,
invokedynamic = true) MemberSubstitutionDynamicConstant constant) throws Throwable {
return constant;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ public void testDynamicConstantInvokedynamic() throws Exception {
.visit(MemberSubstitution.strict()
.field(named(FOO))
.replaceWithChain(MemberSubstitution.Substitution.Chain.Step.ForDelegation.to(bootstrap.getMethod("intercept", bootstrap)))
.on(named(RUN)))
.on(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getDeclaredConstructor().newInstance();
assertThat(type.getDeclaredMethod(RUN).invoke(instance), instanceOf(bootstrap));
assertThat(type.getDeclaredMethod(FOO).invoke(instance), instanceOf(type));
}

@Test
Expand All @@ -338,12 +338,12 @@ public void testDynamicConstant() throws Exception {
.visit(MemberSubstitution.strict()
.field(named(FOO))
.replaceWithChain(MemberSubstitution.Substitution.Chain.Step.ForDelegation.to(bootstrap.getMethod("intercept", bootstrap)))
.on(named(RUN)))
.on(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getDeclaredConstructor().newInstance();
assertThat(type.getDeclaredMethod(RUN).invoke(instance), instanceOf(bootstrap));
assertThat(type.getDeclaredMethod(FOO).invoke(instance), instanceOf(type));
}

@Test
Expand Down
Binary file not shown.

0 comments on commit a64a77b

Please sign in to comment.