23 package org.matsim.core.controler;
25 import java.lang.annotation.Annotation;
28 import com.google.inject.spi.LinkedKeyBinding;
29 import org.apache.logging.log4j.Level;
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
39 import com.google.inject.Binder;
40 import com.google.inject.Binding;
41 import com.google.inject.Guice;
42 import com.google.inject.Key;
43 import com.google.inject.Module;
44 import com.google.inject.internal.BindingImpl;
45 import com.google.inject.multibindings.MapBinder;
46 import com.google.inject.name.Names;
47 import com.google.inject.spi.DefaultElementVisitor;
48 import com.google.inject.spi.Element;
49 import com.google.inject.spi.Elements;
50 import com.google.inject.util.Modules;
59 com.google.inject.
Injector bootstrapInjector = Guice.createInjector(
new Module() {
61 public void configure(Binder binder) {
62 binder.requireExplicitBindings();
69 List<com.google.inject.Module> guiceModules =
new ArrayList<>();
70 for (Module module : modules) {
71 bootstrapInjector.injectMembers(module);
72 guiceModules.add(module);
74 com.google.inject.Injector realInjector = bootstrapInjector.createChildInjector(
insertMapBindings(guiceModules));
79 public static void printInjector(com.google.inject.Injector injector, Logger log) {
80 Level level = Level.DEBUG ;
81 log.log(level,
"=== printInjector start ===") ;
82 for (Map.Entry<Key<?>, Binding<?>> entry : injector.getBindings().entrySet()) {
83 if ( entry.getKey().toString().contains(
"type=org.matsim") ) {
84 Annotation annotation = entry.getKey().getAnnotation();
85 log.log( level, entry.getKey().getTypeLiteral() +
" " + (annotation != null ? annotation.toString() :
""));
86 log.log(level,
" --> provider: " + entry.getValue().getProvider());
87 log.log(level,
" --> source: " + entry.getValue().getSource() );
88 if ( entry.getValue() instanceof BindingImpl ) {
89 log.log( level,
" --> scope: " + ((BindingImpl<?>)entry.getValue()).getScoping() ) ;
91 if ( entry.getValue() instanceof LinkedKeyBinding) {
92 log.log( level,
" --> target: " + ((LinkedKeyBinding) entry.getValue()).getLinkedKey() ) ;
94 log.log(level,
" ==full==> " + entry.getValue() );
99 log.log(level,
"=== printInjector end ===") ;
106 return Modules.combine(Modules.combine(guiceModules), routingModuleBindings, travelTimeBindings, travelDisutilityFactoryBindings);
110 final Set<String> modes =
new HashSet<>();
111 for (Element element : Elements.getElements(guiceModules)) {
112 element.acceptVisitor(
new DefaultElementVisitor<Object>() {
114 public <T> Object visit(Binding<T> binding) {
115 if (binding.getKey().getTypeLiteral().getRawType().equals(aClass)) {
116 if (binding.getKey().getAnnotation() instanceof com.google.inject.name.Named) {
117 modes.add(((com.google.inject.name.Named) binding.getKey().getAnnotation()).value());
126 protected void configure() {
127 MapBinder<String, T> routingModuleMultibinder = MapBinder.newMapBinder(binder(), String.class, aClass);
128 for (String mode : modes) {
129 routingModuleMultibinder.addBinding(mode).to(Key.get(aClass, Names.named(mode)));
142 final Collection<Module> theModules =
new ArrayList<>();
145 public void install(){
152 theModules.addAll( Arrays.asList( modules ) );
static void printInjector(com.google.inject.Injector injector, Logger log)
static com.google.inject.Injector createMinimalMatsimInjector(Config config, Scenario scenario, Module... modules)
static final Logger logger
static com.google.inject.Injector createInjector(final Config config, Module... modules)
static Module insertMapBindings(List< Module > guiceModules)
static< T > com.google.inject.AbstractModule createMapBindingsForType(List< Module > guiceModules, final Class< T > aClass)