我正在实现一个使加载的类隐式实现接口的代理。由代理实现的接口中的方法以自定义方式调用原始类方法,该方法带有特殊注释。但是事实证明,在x
中,已加载的类仍然不存在。但是,拦截器内部的代码是在实际加载该类之后执行的。因此,拦截器中的代码可以与类方法一起使用。我可以使用AgentBuilder.Transformer
对象调用这些方法吗?
MethodDescription
我可能可以通过public class CustomTransformer implements AgentBuilder.Transformer {
public static class InstantiateInterceptor {
private final Collection<MethodDescription> methodDescription;
InstantiateInterceptor(Collection<MethodDescription> methodDescription) {
this.methodDescription = methodDescription;
}
public Object intercept(@This Object self) {
//
// Call method by descriptions somewhere here
//
return null;
}
}
@Override
public DynamicType.Builder<?> transform(
DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module) {
try {
List<MethodDescription> cacheableMethods =
typeDescription.getDeclaredMethods().stream()
.filter(t -> t.getDeclaredAnnotations()
.isAnnotationPresent(EnhanceMarker.class))
.collect(Collectors.toList());
return builder
.implement(HasField.class)
.define(HasField.class.getMethod("value"))
.intercept(MethodDelegation
.to(new InstantiateInterceptor(cacheableMethods)));
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
}
return builder;
}
}
对象的名称来获取该方法。但是它需要参数类型列表,而且我只能获取@This
类对象的列表,并且它不返回参数类型。
答案 0 :(得分:0)
如果该方法在@This
实例上可用,那么为什么不只反射而不调用它呢?还是生成此方法?在这种情况下,请在接口中定义签名,实现该接口并在该接口上读取@This
实例。
如果您要调用超级方法,则可以使用由@Super
注释的实例执行相同的操作,其中Byte Buddy创建了适当的代理。