我想更改类的导入,以便指向不同的包。 Byte Buddy文档没有提供关于如何实现这一点的大量信息。这就是我到目前为止所做的:
public class ProxyPlugin implements net.bytebuddy.build.Plugin { public DynamicType.Builder apply(DynamicType.Builder builder, TypeDescription typeDescription) { return builder.name(typeDescription.getPackage().getName() + ".proxy." + typeDescription.getSimpleName()); } public boolean matches(TypeDescription typeDefinitions) { return true; } }
我的目标是更改某些包前缀名称,以便它们附加“.proxy”。请注意,我只需要更改方法签名,因为目标是接口。
答案 0 :(得分:1)
我找到了解决方案。事实证明,Byte Buddy有一个名为 ClassRemapper 的便利类来实现我想要的目标:
public class ProxyPlugin implements net.bytebuddy.build.Plugin { public DynamicType.Builder apply(DynamicType.Builder builder, TypeDescription typeDescription) { DynamicType.Builder proxy = builder.name(typeDescription.getPackage().getName() + ".proxy." + typeDescription.getSimpleName()); proxy = proxy.visit(new AsmVisitorWrapper() { public int mergeWriter(int flags) { return 0; } public int mergeReader(int flags) { return 0; } public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVisitor, int writerFlags, int readerFlags) { return new ClassRemapper(classVisitor, new Remapper() { @Override public String map(String typeName) { if (typeName.startsWith("org/example/api") && !typeName.contains("/proxy/")) { return typeName.substring(0, typeName.lastIndexOf("/") + 1) + "proxy" + typeName.substring(typeName.lastIndexOf("/")); } else { return typeName; } } }); } }); return proxy; } public boolean matches(TypeDescription typeDescription) { return true; } }