Injection for Instrumented class files

25 views
Skip to first unread message

Nick Freeman

unread,
Nov 25, 2014, 3:33:50 PM11/25/14
to sil...@googlegroups.com
My top level class (the one that needs all the dependencies injected in to it) is instrumented by a third party library. 

I have the following:

bind(Implementation.class).toConstructor();

but this does not work because Implementation is a different Implementation.class at runtime.

What I would like to specify is something like

bind(Interface.class).toConstructor();

which is to say allow anything that implements this interface to be constructed through Silk.

Or I guess it could be anything that has the same fully qualified class name.

What is the best way to implement something like this?




Thanks,

Nick

jan

unread,
Nov 26, 2014, 2:25:28 AM11/26/14
to sil...@googlegroups.com
Hi Nick,

the best way is not to use instrumentation or at least to not let such classes escape a well defined small bounded context.

However, either I misunderstand what you want to do or you misunderstand a injection container.
You talk about a instrumented class, what sounds like some tool does create such instrumented instances what means the container does not do it so no binding would help as the container doesn't even construct this instances.
Is it right that you get instances (not from the container) and you want inject into them? Or do you want to do the instrumentation yourself, say when container should inject you interface you want it to create you implementation, inject dependencies and also instrument (decorate) that implementation for your instrumentation? In that case you will need to write a Supplier and bind the interface to that.

bind(Interface.class).to(MyInstrumentingSupplier.class);

class MyInstrumentingSupplier implements Supplier<Implementation> {

  Implementation supply(Dependency<? super Implementation> dependency, Injector injector) {
    // use injector to supply your dependencies to your constructor and wrap impl in proxy that is returned
  }
}

Best Regards,

Jan

Jan Bernitt

unread,
Nov 26, 2014, 6:45:32 AM11/26/14
to sil...@googlegroups.com
I came to think of a simplification for my example. You can bind

bind(Interface.class).to(MyInstrumentingSupplier.class);
bind
(Implementation.class).toConstructor();

Than it becomes simpler in the Supplier as you can just resolve the Implementation directly.

class MyInstrumentingSupplier implements Supplier<Implementation> {
 

  Implementation supply(Dependency<? super Implementation> dependency, Injector injector) {
    Implementation impl = injector.resolve(Dependency.dependency(Implementation.class);
    return proxyOf(impl);

  }


}

Something like that. This is a slightly simplified version that does not forward the dependency stack correctly but you might not need that. If so use dependency.typed(Implementation.class) instead of building a fresh Dependency.
See examples here: https://github.com/jbee/silk/blob/master/src/core/se/jbee/inject/bootstrap/SuppliedBy.java

Best
Jan

(hopefully this has not been resend several times)
Reply all
Reply to author
Forward
0 new messages