|
|
Re: How to pass object to the Xtext plugin [message #1279123 is a reply to message #1278753] |
Fri, 28 March 2014 09:01   |
Marcus Höpfner Messages: 53 Registered: February 2014 |
Member |
|
|
Hi,
in my Xtext plugin I need access to SOMETHING which comes from the outside.
As far as I understand Google Guice I would write a method in the RuntimeModule which returns SOMETHING and which is annotated with @Provides.
But how to pass an instance of SOMETHING to the RuntimeModule. I thought through constructor. But then I need to change a bunch of calls to runtime module and I need to pass SOMETHING to some methods of the plugin's Activator.
So I was wondering whether this is the right way whenever Xtext grammar plugins have a dependency to objects from outside.
Did you get my point?
Here is some coding snippet of the RuntimeModule if I would change it:
/*
* generated by Xtext
*/
package com.sap.bw.qd.formula;
import org.eclipse.xtext.conversion.IValueConverterService;
import com.google.inject.Provides;
import com.sap.bw.qd.formula.valueconverter.ValueConverterService;
import com.sap.bw.qd.model.query.Query;
/**
* Use this class to register components to be used at runtime / without the
* Equinox extension registry.
*/
public class QueryFormulaRuntimeModule extends AbstractQueryFormulaRuntimeModule {
private Query query;
public QueryFormulaRuntimeModule(Query query) {
super();
this.query = query;
}
@Override
public Class<? extends IValueConverterService> bindIValueConverterService() {
return ValueConverterService.class;
}
@Provides
Query provideQuery() {
return query;
}
}
And the plugin's activator (also already changed). You can see that I need to pass Query through some methods.
/*
* generated by Xtext
*/
package com.sap.bw.qd.formula.ui.internal;
import java.util.Collections;
import java.util.Map;
import org.apache.log4j.Logger;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.ui.shared.SharedStateModule;
import org.eclipse.xtext.util.Modules2;
import org.osgi.framework.BundleContext;
import com.google.common.collect.Maps;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.sap.bw.qd.model.query.Query;
/**
* This class was generated. Customizations should only happen in a newly
* introduced subclass.
*/
public class QueryFormulaActivator extends AbstractUIPlugin {
public static final String COM_SAP_BW_QD_FORMULA_QUERYFORMULA = "com.sap.bw.qd.formula.QueryFormula";
private static final Logger logger = Logger.getLogger(QueryFormulaActivator.class);
private static QueryFormulaActivator INSTANCE;
private Map<String, Injector> injectors = Collections.synchronizedMap(Maps.<String, Injector> newHashMapWithExpectedSize(1));
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
INSTANCE = this;
}
@Override
public void stop(BundleContext context) throws Exception {
injectors.clear();
INSTANCE = null;
super.stop(context);
}
public static QueryFormulaActivator getInstance() {
return INSTANCE;
}
public Injector getInjector(String language, Query query) {
synchronized (injectors) {
Injector injector = injectors.get(language);
if (injector == null) {
injectors.put(language, injector = createInjector(language, query));
}
return injector;
}
}
protected Injector createInjector(String language, Query query) {
try {
Module runtimeModule = getRuntimeModule(language, query);
Module sharedStateModule = getSharedStateModule();
Module uiModule = getUiModule(language);
Module mergedModule = Modules2.mixin(runtimeModule, sharedStateModule, uiModule);
return Guice.createInjector(mergedModule);
} catch (Exception e) {
logger.error("Failed to create injector for " + language);
logger.error(e.getMessage(), e);
throw new RuntimeException("Failed to create injector for " + language, e);
}
}
protected Module getRuntimeModule(String grammar, Query query) {
if (COM_SAP_BW_QD_FORMULA_QUERYFORMULA.equals(grammar)) {
return new com.sap.bw.qd.formula.QueryFormulaRuntimeModule(query);
}
throw new IllegalArgumentException(grammar);
}
protected Module getUiModule(String grammar) {
if (COM_SAP_BW_QD_FORMULA_QUERYFORMULA.equals(grammar)) {
return new com.sap.bw.qd.formula.ui.QueryFormulaUiModule(this);
}
throw new IllegalArgumentException(grammar);
}
protected Module getSharedStateModule() {
return new SharedStateModule();
}
}
And additionally I would need to adapt:
- QueryFormulaStandaloneSetupGenerated
- QueryFormulaExecutableExtensionFactory
- QueryFormulaUiInjectorProvider
Because the either call Activator.createInjector or they construct an instance of the RuntimeModule.
Thanks, Marcus
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02873 seconds