I'm trying to use ByteBuddy in my AndroidAnnotations-based project.
I have a class MyService
that is an EBean
(AA) with some injected dependencies:
@EBean
public class MyService {
@Bean
SomeRepository repo;
@Log
public void doSomething() { //do something that uses "repo" }
}
I want log something before execute any method of this class annotated with @Log
.
I can get new instance of MyService
this way:
MyService service = MyService_.getInstance(context); //MyService_ is generated by AndroidAnnotations
and all @Bean
dependencies are injected. Everything ok.
Now, using ByteBuddy, I need create a dynamic subclass to intercept annotated methods:
Class<? extends MyService> dynamic = byteBuddy
.subclass(MyService_.class)
.method(ElementMatchers.isAnnotatedWith(Log.class))
.intercept(MethodDelegation.to(new LogInterceptor()))
.make()
.load(getClassLoader(), new AndroidClassLoadingStrategy(getDir()))
.getLoaded();
I need subclass MyService_
instead of MyService
beacuse it uses the Context
to inject dependencies. If I would subclass MyService
, repo
will be always null
.
The problem is that auto-generated class MyService_
is final
, so I can't subclass it. getInstance
method is static and constructor is private but I could obtain an instance by reflection so it is not a problem.
Any idea? Has anybody integrated ByteBuddy with AndroidAnnotations?
I would appreciate any help, thanks.