- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想拦截对我的一个类的一些方法调用,但这些类没有默认构造函数。
给定以下类,我将如何设置 Byte Buddy 以同时创建一个公共(public)无参数构造函数来创建生成的类?
public class GetLoggedInUsersSaga extends AbstractSpaceSingleEventSaga {
private final UserSessionRepository userSessionRepository;
@Inject
public GetLoggedInUsersSaga(final UserSessionRepository userSessionRepository) {
this.userSessionRepository = userSessionRepository;
}
@StartsSaga
public void handle(final GetLoggedInUsersRequest request) {
// this is the method in want to intercept
}
}
编辑:具体用例是简化单元测试设置。
目前我们总是要写这样的东西:
@Test
public void someTest() {
// Given
// When
GetLoggedInUsersRequest request = new GetLoggedInUsersRequest();
setMessageForContext(request); // <-- always do this before calling handle
sut.handle(request);
// Then
}
我认为在@Before 方法中创建一个自动为您设置上下文的代理会很好。
@Before
public void before() {
sut = new GetLoggedInUsersSaga(someDependency);
sut = intercept(sut);
}
@Test
public void someTest() {
// Given
// When
GetLoggedInUsersRequest request = new GetLoggedInUsersRequest();
sut.handle(request);
// Then
}
我玩了一下,但不幸的是我没有让它工作..
public <SAGA extends Saga> SAGA intercept(final SAGA sagaUnderTest) throws NoSuchMethodException, IllegalAccessException, InstantiationException {
return (SAGA) new ByteBuddy()
.subclass(sagaUnderTest.getClass())
.defineConstructor(Collections.<Class<?>>emptyList(), Visibility.PUBLIC)
.intercept(MethodCall.invokeSuper())
.method(ElementMatchers.isAnnotatedWith(StartsSaga.class))
.intercept(
MethodDelegation.to(
new Object() {
@RuntimeType
public Object intercept(
@SuperCall Callable<?> c,
@Origin Method m,
@AllArguments Object[] a) throws Exception {
setMessageForContext((Message) a[0]);
return c.call();
}
}))
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded()
.newInstance();
}
不幸的是现在我明白了(可能是因为 ctor 调用仍然没有正确设置)
java.lang.IllegalStateException: Cannot invoke public com.frequentis.ps.account.service.audit.GetLoggedInUsersSaga$ByteBuddy$zSZuwhtR() as a super method
这是正确的方法吗?
我应该在这里使用 byte buddy 还是有更简单/其他的方法?
最佳答案
你不能在没有任何字节码的情况下定义构造函数。这将是一个抽象构造函数,这在 Java 中是非法的。我将在未来版本的 javadoc 中添加更精确的描述。感谢您提醒我注意这一点。
您需要定义任何构造函数都需要的 super 方法调用:
DynamicType.Builder builder = ...
builder = builder
.defineConstructor(Collections.<Class<?>>emptyList(), Visibility.PUBLIC)
.intercept(MethodCall
.invoke(superClass.getDeclaredConstructor())
.onSuper())
至于你是否应该在这里使用 Byte Buddy:我不能从我看到的小代码中告诉你。您应该问的问题:考虑到代码量和遵循代码的复杂性,它是否使我的代码更容易?如果 Byte Buddy 使您的代码更易于使用(和运行),请使用它。如果没有,请不要使用它。
关于java - 如何使用 Byte Buddy 创建默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264748/
在使用指南阅读和理解 linux 内核时- http://www.johnchukwuma.com/training/UnderstandingTheLinuxKernel3rdEdition.pdf
我们正在尝试这种叫做:buddy.works的服务,以取代我们的jenkins管道,用于较小的项目。它提供了用于创建管道的gui,但是我们在访问子目录时遇到了问题。 Resolving host 52
我正在尝试为 springboot 应用程序构建自定义代理。这就是我的代理 premain 的样子 .with(new AgentBuilder.Initializat
我正在尝试使用 Byte Buddy 生成一个非常简单的代码. 我有一个 POJO 类,其中一些字段用 @SecureAttribute 注释,对于这些字段,我想重写 getter 实现并将调用重定向
我已经阅读并理解了二进制伙伴内存分配方法背后的概念,并且我正在尝试将其应用到 C 语言中,但在真正开始之前,我有一些特定于实现的问题。 https://drive.google.com/file/d/
我正在尝试围绕某些类图中的类生成运行时包装器,但我不知道当图中存在循环时如何处理这种情况。想象一下,有一个类 A 有一个类型 B 的字段,但类型 B 有一个类型 A 的字段。我想生成类 A' 和 B'
我看到术语“Buddy 类”被用作“如何向另一个文件中的部分类添加注释”等问题的“答案”,但这些答案假设我知道 Buddy 类 是什么,并且代码示例假设我理解这是如何工作的/为什么工作。 对于 C#
假设我有一个方法m: public void m() { String foo = "foo"; int bar = 0; doSomething(foo, bar); } 我想使用 By
我正在尝试解决人才伙伴的这个问题。 http://www.talentbuddy.co/challenge/52a9121cc8a6c2dc91481f8d5233cc274af0110af382f4
我正在尝试生成具有循环类依赖性的类,类似于这个问题:Byte Buddy - Handling cyclic references in generated classes 作为一个最小的例子,我想生
有必要描述一下这个类的结构 class A{ private List listA; } 尝试了解决方案:Byte-buddy: generate classes with cyclic
我正在尝试实例化一个没有空参数构造函数的类(而且它的直接父类也没有空参数构造函数) Class newClass = new ByteBuddy(); .subclass(Bu
即使我提供了实际的实例,Byte Buddy 似乎也只喜欢将公共(public)类作为拦截器实现;我经常发现自己想做这样的事情: import static MethodDelegation.to;
我正在尝试使用反射来检查给定类的属性是否设置了 ReadOnly 属性。我使用的类是 MVC View 模型(使用部分“伙伴”类作为元数据。 public partial class Account
我正在尝试使用 Java 代理向类添加方法。但它给出了如下错误。 java.lang.VerifyError: Local variable table overflow Exception Deta
是否可以使用 bytebuddy 创建一个全新的类和方法? 我看到的所有示例都使用现有类或拦截现有方法来修改它们。 是否有通过bytebuddy构造一个类并动态添加一些方法并返回该类的实例的示例? 最
几天来我一直在寻找“如何在运行时向方法添加注释”的答案,并找到了这个名为 Byte Buddy 的很棒的工具,使用了它,但仍然无法按我的需要使其工作到。我确定它必须能够从这个问题中做到这一点 Can
我正在尝试使用 ByteBuddy 实现 Profiler。我目前正在努力有效地为我正在分析的方法创建适当的签名。 这是我当前实现的要点:https://gist.github.com/felixba
我正在尝试使用 Byte Buddy 设置一个类,该类实现给定实例的所有接口(interface)并将所有调用转发给该实例,这是我目前的代码: import static net.bytebuddy.
我是 Byte Buddy 的新手,我正在尝试使用它来创建对对象执行 getter 方法的接口(interface)的实现。我的界面如下所示: public interface Executor {
我是一名优秀的程序员,十分优秀!