2017-05-18 12 views
0

私は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に入れるのを避けることができますか?

答えて

0

これが意味するものなのかどうかはわかりませんが、にはContextFunctionを入れることができます。 Eclipseがオブジェクトを探すと、オブジェクトがContextFunctionであることがわかり、computeメソッドを呼び出します。

これは、コンテキスト機能が要求するすべてのオブジェクトのキーを知る必要があることを意味します。グレッグ-449により示唆されるように続いて

+0

おかげで...私はそのコンテキスト機能は、特定のキーにバインドされて気づきませんでした、それは異なるキーのための工場のようなものだと思った。 –

+0

'compute'メソッドにはキーが与えられていますが、私がコンテキスト関数を設定することを知っている唯一の方法は、扱うキーを知っていることです。(これは十分に文書化されていないので、 –

+0

ありがとう、実際の解決策を得ました...幸いにも、Guiceはそのバインディングをリストすることができます –

0

は、特定のキーの下の関数を入れた後、動作しているようです:返信用

import org.eclipse.e4.core.contexts.ContextFunction; 
import org.eclipse.e4.core.contexts.IEclipseContext; 

import com.google.inject.Injector; 
import com.google.inject.Key; 

/** 
* Links guice and e4 RI 
*/ 
public class GuiceRCPBridgeFunction extends ContextFunction 
{ 

    public static void link(Injector injector, IEclipseContext context) 
    { 
     GuiceRCPBridgeFunction function = new GuiceRCPBridgeFunction(injector); 
     // add this function for every injector binding: 
     for(Key <?> key : injector.getBindings().keySet()) 
     { 
      String contextKey = key.getTypeLiteral().toString(); 
      Class classKey = function.loadClass(contextKey); 
      context.set(classKey, function); 
     } 
    } 

    private Injector injector; 

    public GuiceRCPBridgeFunction(Injector injector) 
    { 
     this.injector = injector; 

    } 

    @Override 
    public Object compute(IEclipseContext context, String contextKey) 
    { 
     // convert string key to type: 
     Class classKey = loadClass(contextKey); 
     // get instance from the injector: 
     Object instance = injector.getInstance(classKey); 
     // cache the instance in the eclipse context: 
     context.set(classKey, instance); 

     return instance; 
    } 

    protected Class <?> loadClass(String className) 
    { 
     Class<?> guiceKey = null; 
     try { 
      guiceKey = injector.getClass().getClassLoader().loadClass(className); 
     } 
     catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } 

     return guiceKey; 
    } 
} 
関連する問題