我想更改类的导入,以便它们指向不同的包。 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;
}
}
我的目标是改变some包前缀名称,以便它们附加“.proxy”。请注意,我只需要更改方法签名,因为目标是接口。
我找到了解决方案。结果 Byte Buddy 有一个名为的便利类类重映射器达到我想要的效果:
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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)