Skip to content

Commit 300ca12

Browse files
committed
Add possibility to install visitor also in member substitution.
1 parent 7ba4302 commit 300ca12

File tree

1 file changed

+111
-12
lines changed

1 file changed

+111
-12
lines changed

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

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5657,38 +5657,63 @@ class ForDynamicInvocation implements Dispatcher {
56575657
*/
56585658
private final BootstrapArgumentResolver resolver;
56595659

5660+
/**
5661+
* A visitor to apply to the parameter types prior to resolving the {@code MethodType}
5662+
* that is passed to the bootstrap method. The supplied types might not be available
5663+
* to the instrumented type what might make it necessary to camouflage them to avoid
5664+
* class loading errors. The actual type should then rather be passed in a different
5665+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
5666+
*/
5667+
private final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor;
5668+
56605669
/**
56615670
* Creates a dispatcher for a dynamic method invocation.
56625671
*
56635672
* @param bootstrapMethod The bootstrap method.
56645673
* @param delegate The delegation method.
56655674
* @param resolver A resolver for supplying arguments to the bootstrap method.
5675+
* @param visitor A visitor to apply to the parameter types prior to resolving the {@code MethodType}
5676+
* that is passed to the bootstrap method. The supplied types might not be available
5677+
* to the instrumented type what might make it necessary to camouflage them to avoid
5678+
* class loading errors. The actual type should then rather be passed in a different
5679+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
56665680
*/
5667-
protected ForDynamicInvocation(MethodDescription.InDefinedShape bootstrapMethod, MethodDescription.InDefinedShape delegate, BootstrapArgumentResolver resolver) {
5681+
protected ForDynamicInvocation(MethodDescription.InDefinedShape bootstrapMethod,
5682+
MethodDescription.InDefinedShape delegate,
5683+
BootstrapArgumentResolver resolver,
5684+
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
56685685
this.bootstrapMethod = bootstrapMethod;
56695686
this.delegate = delegate;
56705687
this.resolver = resolver;
5688+
this.visitor = visitor;
56715689
}
56725690

56735691
/**
56745692
* Creates a dispatcher factory for a dynamic method invocation.
56755693
*
56765694
* @param bootstrapMethod The bootstrap method.
56775695
* @param resolverFactory A resolver for supplying arguments to the bootstrap method.
5696+
* @param visitor A visitor to apply to the parameter types prior to resolving the {@code MethodType}
5697+
* that is passed to the bootstrap method. The supplied types might not be available
5698+
* to the instrumented type what might make it necessary to camouflage them to avoid
5699+
* class loading errors. The actual type should then rather be passed in a different
5700+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
56785701
* @return An appropriate dispatcher factory.
56795702
*/
5680-
protected static Dispatcher.Factory of(MethodDescription.InDefinedShape bootstrapMethod, BootstrapArgumentResolver.Factory resolverFactory) {
5703+
protected static Dispatcher.Factory of(MethodDescription.InDefinedShape bootstrapMethod,
5704+
BootstrapArgumentResolver.Factory resolverFactory,
5705+
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
56815706
if (!bootstrapMethod.isInvokeBootstrap()) {
56825707
throw new IllegalStateException("Not a bootstrap method: " + bootstrapMethod);
56835708
}
5684-
return new ForDynamicInvocation.Factory(bootstrapMethod, resolverFactory);
5709+
return new ForDynamicInvocation.Factory(bootstrapMethod, resolverFactory, visitor);
56855710
}
56865711

56875712
/**
56885713
* {@inheritDoc}
56895714
*/
56905715
public Dispatcher.Resolved resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod) {
5691-
return new ForDynamicInvocation.Resolved(bootstrapMethod, delegate, resolver.resolve(instrumentedType, instrumentedMethod));
5716+
return new ForDynamicInvocation.Resolved(bootstrapMethod, delegate, resolver.resolve(instrumentedType, instrumentedMethod), visitor);
56925717
}
56935718

56945719
/**
@@ -5712,17 +5737,37 @@ protected static class Resolved implements Dispatcher.Resolved {
57125737
*/
57135738
private final BootstrapArgumentResolver.Resolved resolver;
57145739

5740+
/**
5741+
* A visitor to apply to the parameter types prior to resolving the {@code MethodType}
5742+
* that is passed to the bootstrap method. The supplied types might not be available
5743+
* to the instrumented type what might make it necessary to camouflage them to avoid
5744+
* class loading errors. The actual type should then rather be passed in a different
5745+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
5746+
*/
5747+
private final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor;
5748+
57155749
/**
57165750
* Creates a resolved dispatcher of a dynamic method dispatcher.
57175751
*
57185752
* @param bootstrapMethod The bootstrap method.
57195753
* @param delegate The delegation target.
57205754
* @param resolver The bootstrap argument resolver to use.
5721-
*/
5722-
protected Resolved(MethodDescription.InDefinedShape bootstrapMethod, MethodDescription.InDefinedShape delegate, BootstrapArgumentResolver.Resolved resolver) {
5755+
* @param visitor A visitor to apply to the parameter types prior to resolving
5756+
* the {@code MethodType} that is passed to the bootstrap method.
5757+
* The supplied types might not be available to the instrumented
5758+
* type what might make it necessary to camouflage them to avoid
5759+
* class loading errors. The actual type should then rather be
5760+
* passed in a different format by the supplied
5761+
* {@link Advice.BootstrapArgumentResolver}.
5762+
*/
5763+
protected Resolved(MethodDescription.InDefinedShape bootstrapMethod,
5764+
MethodDescription.InDefinedShape delegate,
5765+
BootstrapArgumentResolver.Resolved resolver,
5766+
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
57235767
this.bootstrapMethod = bootstrapMethod;
57245768
this.delegate = delegate;
57255769
this.resolver = resolver;
5770+
this.visitor = visitor;
57265771
}
57275772

57285773
/**
@@ -5741,8 +5786,8 @@ public StackManipulation apply(TypeDescription receiver, ByteCodeElement.Member
57415786
throw new IllegalArgumentException(bootstrapMethod + " is not accepting advice bootstrap arguments: " + constants);
57425787
}
57435788
return MethodInvocation.invoke(bootstrapMethod).dynamic(delegate.getInternalName(),
5744-
delegate.getReturnType().asErasure(),
5745-
delegate.getParameters().asTypeList().asErasures(),
5789+
delegate.getReturnType().accept(visitor).asErasure(),
5790+
delegate.getParameters().asTypeList().accept(visitor).asErasures(),
57465791
constants);
57475792
}
57485793
}
@@ -5763,22 +5808,40 @@ protected static class Factory implements Dispatcher.Factory {
57635808
*/
57645809
private final BootstrapArgumentResolver.Factory resolverFactory;
57655810

5811+
/**
5812+
*
5813+
* A visitor to apply to the parameter types prior to resolving the {@code MethodType}
5814+
* that is passed to the bootstrap method. The supplied types might not be available
5815+
* to the instrumented type what might make it necessary to camouflage them to avoid
5816+
* class loading errors. The actual type should then rather be passed in a different
5817+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
5818+
*/
5819+
private final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor;
5820+
57665821
/**
57675822
* Creates a new factory for a dispatcher using a dynamic method invocation.
57685823
*
57695824
* @param bootstrapMethod The bootstrap method.
57705825
* @param resolverFactory A factory for a bootstrap argument resolver.
5771-
*/
5772-
protected Factory(MethodDescription.InDefinedShape bootstrapMethod, BootstrapArgumentResolver.Factory resolverFactory) {
5826+
* @param visitor A visitor to apply to the parameter types prior to resolving the {@code MethodType}
5827+
* that is passed to the bootstrap method. The supplied types might not be available
5828+
* to the instrumented type what might make it necessary to camouflage them to avoid
5829+
* class loading errors. The actual type should then rather be passed in a different
5830+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
5831+
*/
5832+
protected Factory(MethodDescription.InDefinedShape bootstrapMethod,
5833+
BootstrapArgumentResolver.Factory resolverFactory,
5834+
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
57735835
this.bootstrapMethod = bootstrapMethod;
57745836
this.resolverFactory = resolverFactory;
5837+
this.visitor = visitor;
57755838
}
57765839

57775840
/**
57785841
* {@inheritDoc}
57795842
*/
57805843
public Dispatcher make(MethodDescription.InDefinedShape delegate) {
5781-
return new ForDynamicInvocation(bootstrapMethod, delegate, resolverFactory.make(delegate));
5844+
return new ForDynamicInvocation(bootstrapMethod, delegate, resolverFactory.make(delegate), visitor);
57825845
}
57835846
}
57845847
}
@@ -6422,6 +6485,24 @@ public WithCustomMapping bootstrap(Method method, BootstrapArgumentResolver.Fact
64226485
return bootstrap(new MethodDescription.ForLoadedMethod(method), resolverFactory);
64236486
}
64246487

6488+
/**
6489+
* Defines the supplied method as a dynamic invocation bootstrap target for delegating advice methods.
6490+
*
6491+
* @param method The bootstrap method or constructor.
6492+
* @param resolverFactory A factory for resolving the arguments to the bootstrap method.
6493+
* @param visitor A visitor to apply to the parameter types prior to resolving the {@code MethodType}
6494+
* that is passed to the bootstrap method. The supplied types might not be available
6495+
* to the instrumented type what might make it necessary to camouflage them to avoid
6496+
* class loading errors. The actual type should then rather be passed in a different
6497+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
6498+
* @return A new builder for a delegation within a member substitution that uses the supplied method for bootstrapping.
6499+
*/
6500+
public WithCustomMapping bootstrap(Method method,
6501+
BootstrapArgumentResolver.Factory resolverFactory,
6502+
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
6503+
return bootstrap(new MethodDescription.ForLoadedMethod(method), resolverFactory, visitor);
6504+
}
6505+
64256506
/**
64266507
* Defines the supplied method description as a dynamic invocation bootstrap target for delegating advice methods. The bootstrap
64276508
* method arguments are:
@@ -6453,7 +6534,25 @@ public WithCustomMapping bootstrap(MethodDescription.InDefinedShape bootstrap) {
64536534
* @return A new builder for a delegation within a member substitution that uses the supplied method or constructor for bootstrapping.
64546535
*/
64556536
public WithCustomMapping bootstrap(MethodDescription.InDefinedShape bootstrap, BootstrapArgumentResolver.Factory resolverFactory) {
6456-
return new WithCustomMapping(Dispatcher.ForDynamicInvocation.of(bootstrap, resolverFactory), offsetMappings);
6537+
return bootstrap(bootstrap, resolverFactory, TypeDescription.Generic.Visitor.NoOp.INSTANCE);
6538+
}
6539+
6540+
/**
6541+
* Defines the supplied method description as a dynamic invocation bootstrap target for delegating advice methods.
6542+
*
6543+
* @param bootstrap The bootstrap method or constructor.
6544+
* @param resolverFactory A factory for resolving the arguments to the bootstrap method.
6545+
* @param visitor A visitor to apply to the parameter types prior to resolving the {@code MethodType}
6546+
* that is passed to the bootstrap method. The supplied types might not be available
6547+
* to the instrumented type what might make it necessary to camouflage them to avoid
6548+
* class loading errors. The actual type should then rather be passed in a different
6549+
* format by the supplied {@link Advice.BootstrapArgumentResolver}.
6550+
* @return A new builder for a delegation within a member substitution that uses the supplied method or constructor for bootstrapping.
6551+
*/
6552+
public WithCustomMapping bootstrap(MethodDescription.InDefinedShape bootstrap,
6553+
BootstrapArgumentResolver.Factory resolverFactory,
6554+
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
6555+
return new WithCustomMapping(Dispatcher.ForDynamicInvocation.of(bootstrap, resolverFactory, visitor), offsetMappings);
64576556
}
64586557

64596558
/**

0 commit comments

Comments
 (0)