以编程方式添加Eclipse 4 RCP上下文功能

时间:2017-05-18 01:34:38

标签: eclipse dependency-injection guice e4

我正在尝试将Guice 4注入器与Eclipse 4 RCP DI机制联系起来。 我想要做的是创建一个ContextFunction,它将在Guice注入器中查找值并在IEclipseContext中绑定它们,如下所示:

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;
    }
}

我想知道在创建Injecter之后是否有办法附加此函数,以便我可以避免将Injecter本身放入IEclipseContext中?

2 个答案:

答案 0 :(得分:0)

不确定这是不是您的意思,但您可以使用它将获得的对象的密钥在ContextFunction中放置IEclipseContext。当Eclipse查找对象时,它将看到该对象是ContextFunction并调用compute方法。

这意味着您必须知道要求上下文功能的所有对象的键。

答案 1 :(得分:0)

按照greg-449:

的建议,将函数放在特定键下后似乎可以正常工作
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;
    }
}