2

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Héctor
  • 24,444
  • 35
  • 132
  • 243
  • Byte Buddy can redefine classes instead of subclassing them. If you manage to hook into the class loading, you might be able to do that. As far as I know, Android Annotations are created at compile time. The easiest would be to unfinalize the classes. – Rafael Winterhalter Jan 08 '16 at 12:10

0 Answers0