我正在尝试使用Byte Buddy
生成一个非常简单的代码。
我有一个POJO类,其中一些字段用@SecureAttribute
注释,对于这些字段,我想覆盖getter实现并将调用重定向到SecurityService.getSecureValue()
实现。
原创课程:
public class Properties {
@SecureAttribute
protected String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
所需代理:
public class PropertiesProxy {
private SecurityService securityService;
public void setSecurityService(SecurityService var1) {
this.securityService = var1;
}
public SecurityService getSecurityService() {
return this.securityService;
}
@Override
public String getPassword() {
return securityService.getSecureValue(password);
}
}
发送字段很容易,但覆盖方法变得复杂。我找到了一些与我的任务相关的样本,我尝试应用但似乎没有得到所需的结果。
所以我的主要问题是:如何跟踪和调试代码生成器?我学到的第一件事就是将类打印到文件中:
DynamicType.Unloaded<?> unloadedType = byteBuddy.make();
unloadedType.saveIn(new File("d:/temp/bytebuddy"));
这给了我一个输出,其中添加了额外的字段,但没有看到getter覆盖(从.class文件中反汇编):
public class PropertiesImpl$ByteBuddy$OLlyZYNY extends PropertiesImpl {
private SecurityService securityService;
public void setSecurityService(SecurityService var1) {
this.securityService = var1;
}
public SecurityService getSecurityService() {
return this.securityService;
}
public PropertiesImpl$ByteBuddy$OLlyZYNY() {
}
}
在这里,我并不完全了解如何查找错误。这是否意味着我使用了完全错误的方法实现,Byte Buddy
只是简单地跳过了它?或者我错了ElementMatchers?是否有一些痕迹或其他什么能让我知道如何修复我的代码?
当前实施:
private Class<?> wrapProperties() throws IOException {
DynamicType.Builder<?> byteBuddy = new ByteBuddy()
.subclass(PropertiesImpl.class)
.defineProperty("securityService", SecurityService.class);
Arrays.stream(PropertiesImpl.class.getDeclaredFields())
.filter(item -> item.getAnnotation(SecureAttribute.class) != null)
.forEach(item -> byteBuddy
.method(ElementMatchers.named(getGetterBeanName(item)))
.intercept(new GetterWrapperImplementation(item)));
DynamicType.Unloaded<?> unloadedType = byteBuddy.make();
unloadedType.saveIn(new File("d:/temp/bytebuddy"));
Class<?> wrapperClass = unloadedType.load(PropertiesImpl.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
return wrapperClass;
}
public static class GetterWrapperImplementation implements Implementation {
public static final TypeDescription SS_TYPE;
public static final MethodDescription SS_GET_SECURE_VALUE;
private final Field filed;
static {
try {
SS_TYPE = new TypeDescription.ForLoadedType(SecurityService.class);
SS_GET_SECURE_VALUE = new MethodDescription.ForLoadedMethod(SecurityService.class.getDeclaredMethod("getSecureValue", String.class));
}
catch (final NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e);
}
}
public GetterWrapperImplementation(Field filed) {
this.filed = filed;
}
@Override
public InstrumentedType prepare(final InstrumentedType instrumentedType) {
return instrumentedType;
}
@Override
public ByteCodeAppender appender(final Target implementationTarget) {
final TypeDescription thisType = implementationTarget.getInstrumentedType();
return new ByteCodeAppender.Simple(Arrays.asList(
TypeCreation.of(SS_TYPE),
// get securityService field
MethodVariableAccess.loadThis(),
FieldAccess.forField(thisType.getDeclaredFields()
.filter(ElementMatchers.named("securityService"))
.getOnly()
).read(),
// get secure field
MethodVariableAccess.loadThis(),
FieldAccess.forField(thisType.getDeclaredFields()
.filter(ElementMatchers.named(filed.getName()))
.getOnly()
).read(),
MethodInvocation.invoke(SS_GET_SECURE_VALUE),
MethodReturn.of(TypeDescription.STRING)
));
}
}
我所知道的事实是ByteCodeAppender appender(final Target implementationTarget)
内的断点没有被击中,但又不确定如何解释它。
感谢。
答案 0 :(得分:2)
Byte Buddy DSL是不可变的。这意味着你总是要打电话:
builder = builder.method(...).intercept(...);
由于这个原因,你的forEach没有达到预期效果。
至于您的实现,您可以在字段上使用MethodCall并将其他字段定义为参数。