blob: 2e659b6f64c682348a224decb38f71cb928d1407 [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2018 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Arnault Lapitre (CEA LIST) arnault.lapitre@cea.fr
* - Initial API and implementation
*
******************************************************************************
* Generated by Xtext 2.25.0
*****************************************************************************/
package org.eclipse.efm.formalml.xtext.tests;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.eclipse.efm.formalml.xtext.FormalMLRuntimeModule;
import org.eclipse.efm.formalml.xtext.FormalMLStandaloneSetup;
import org.eclipse.xtext.testing.GlobalRegistries;
import org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento;
import org.eclipse.xtext.testing.IInjectorProvider;
import org.eclipse.xtext.testing.IRegistryConfigurator;
public class FormalMLInjectorProvider implements IInjectorProvider, IRegistryConfigurator {
protected GlobalStateMemento stateBeforeInjectorCreation;
protected GlobalStateMemento stateAfterInjectorCreation;
protected Injector injector;
static {
GlobalRegistries.initializeDefaults();
}
@Override
public Injector getInjector() {
if (injector == null) {
this.injector = internalCreateInjector();
stateAfterInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
}
return injector;
}
protected Injector internalCreateInjector() {
return new FormalMLStandaloneSetup() {
@Override
public Injector createInjector() {
return Guice.createInjector(createRuntimeModule());
}
}.createInjectorAndDoEMFRegistration();
}
protected FormalMLRuntimeModule createRuntimeModule() {
// make it work also with Maven/Tycho and OSGI
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=493672
return new FormalMLRuntimeModule() {
@Override
public ClassLoader bindClassLoaderToInstance() {
return FormalMLInjectorProvider.class
.getClassLoader();
}
};
}
@Override
public void restoreRegistry() {
stateBeforeInjectorCreation.restoreGlobalState();
stateBeforeInjectorCreation = null;
}
@Override
public void setupRegistry() {
stateBeforeInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
if (injector == null) {
getInjector();
}
stateAfterInjectorCreation.restoreGlobalState();
}
}