私はGuice 4 injectorをEclipse 4 RCP DIメカニズムと橋渡ししようとしています。Eclipse 4 RCPコンテキスト関数をプログラムで追加する
import org.eclipse.e4.core.contexts.ContextFunction;
import org.eclipse.e4.core.contexts.IEclipseContext;
import com.google.inject.Injector;
public class GuiceRCPBridgeFunction extends ContextFunction
{
private Injector injector;
public GuiceRCPBridgeFunction(Injector injector)
{
this.injector = injector;
}
@Override
public Object compute(IEclipseContext context, String contextKey)
{
// convert string key to type:
Class<?> guiceKey = null;
try {
guiceKey = injector.getClass().getClassLoader().loadClass(contextKey);
}
catch (ClassNotFoundException e) { throw new RuntimeException(e); }
// get instance from the injector:
Object instance = injector.getInstance(guiceKey);
// cache the instance in the eclipse context:
context.set(contextKey, instance);
return instance;
}
}
は、私は方法が添付するかどうかを知りたい: 私は何をしたいのは、このような何かをGuiceのインジェクタ内の値について調べることになる、ContextFunctionを作成し、IEclipseContextでそれらをバインドすることですこの関数はインジェクタが作成された後で、インジェクタ自体をIEclipseContextに入れるのを避けることができますか?
おかげで...私はそのコンテキスト機能は、特定のキーにバインドされて気づきませんでした、それは異なるキーのための工場のようなものだと思った。 –
'compute'メソッドにはキーが与えられていますが、私がコンテキスト関数を設定することを知っている唯一の方法は、扱うキーを知っていることです。(これは十分に文書化されていないので、 –
ありがとう、実際の解決策を得ました...幸いにも、Guiceはそのバインディングをリストすることができます –