private static void setFieldContainerInstance(InspectionData data, Injector injector) throws Throwable { // no container instance for static data. if (!data.isStatic) { if (data.lookupClass == null) { // if the container had an enclosing class and it's not static, make sure the lookup // points to the enclosing class. if (data.actualClass.getEnclosingClass() != null && !Modifier.isStatic(data.actualClass.getModifiers())) { data.lookupClass = data.actualClass.getEnclosingClass(); } } // check if this is an enclosed class if (data.lookupClass == null) { Binding<?> binding = injector.getBinding(data.actualClass); validateBindingIsSingleton(injector, binding); data.fieldContainerInstance = binding.getProvider().get(); } else { // inner classes must be annotated properly Binding<?> binding = injector.getBinding(data.lookupClass); validateBindingIsSingleton(injector, binding); Object lookupObj = binding.getProvider().get(); // If this was an enclosed class, validate that it has InspectableContainer. if (data.actualClass.getEnclosingClass() != null && !Modifier.isStatic(data.actualClass.getModifiers())) { if (data.actualClass.getAnnotation(InspectableContainer.class) == null) { throw new InspectionException("container must be annotated with InspectableContainer"); } Constructor[] constructors = data.actualClass.getDeclaredConstructors(); if (constructors.length != 1) { throw new InspectionException("wrong constructors length: " + constructors.length); } Class[] parameters = constructors[0].getParameterTypes(); if (parameters.length != 1 || !data.lookupClass.isAssignableFrom(parameters[0])) { throw new InspectionException("wrong parameter count or type for constructor"); } constructors[0].setAccessible(true); data.fieldContainerInstance = constructors[0].newInstance(lookupObj); } else { data.fieldContainerInstance = lookupObj; } } } }
private Emitter findEmitter(String emitterType, List<Binding<Emitter>> emitterBindings) { for (Binding<Emitter> binding : emitterBindings) { if (Names.named(emitterType).equals(binding.getKey().getAnnotation())) { return binding.getProvider().get(); } } return null; }
private ContextListenerDefinition[] collectContextListenerDefinitions(Injector injector) { List<ContextListenerDefinition> contextListeners = new ArrayList<ContextListenerDefinition>(); TypeLiteral<ContextListenerDefinition> LISTENER_DEFS = TypeLiteral.get(ContextListenerDefinition.class); for (Binding<ContextListenerDefinition> entry : injector.findBindingsByType(LISTENER_DEFS)) { contextListeners.add(entry.getProvider().get()); } // Convert to a fixed size array for speed. return contextListeners.toArray(new ContextListenerDefinition[contextListeners.size()]); }
private HttpSessionListenerDefinition[] collectListenerDefinitions(Injector injector) { List<HttpSessionListenerDefinition> sessionListeners = new ArrayList<HttpSessionListenerDefinition>(); TypeLiteral<HttpSessionListenerDefinition> LISTENER_DEFS = TypeLiteral.get(HttpSessionListenerDefinition.class); for (Binding<HttpSessionListenerDefinition> entry : injector.findBindingsByType(LISTENER_DEFS)) { sessionListeners.add(entry.getProvider().get()); } // Convert to a fixed size array for speed. return sessionListeners.toArray(new HttpSessionListenerDefinition[sessionListeners.size()]); }
@Override public void close() { if (isClosed()) { throw new ClosedInjectorException(this); } for (Key<?> key : delegate.getAllBindings().keySet()) { try { com.google.inject.Binding<?> binding = delegate.getExistingBinding(key); if (!Scopes.isSingleton(binding)) { continue; } invokeAnnotatedMethod(binding.getProvider().get(), PreDestroy.class); } catch (ProvisionException pe) { if (!(pe.getCause() instanceof TypeNotFoundException)) { pe.printStackTrace(); } } } synchronized (lock) { closed = true; } }
@Override public Object provide() { return binding.getProvider().get(); }
private Module create( final Logger LOG, final LifecycleManager manager, final Collection<Module> loadedModules, final List<Module> rootModules, final boolean isBootstrap, final Module bootstrapModule) throws Exception { LOG.info("Creating {} injector", isBootstrap ? "bootstrap" : "main"); // Populate all the bootstrap state from the main module final List<Element> elements = Elements.getElements(Stage.DEVELOPMENT, rootModules); final Set<Key<?>> keys = ElementsEx.getAllInjectionKeys(elements); final List<String> moduleNames = ElementsEx.getAllSourceModules(elements); final Injector injector = Guice.createInjector( stage, new LifecycleModule(), new AbstractModule() { @Override protected void configure() { bind(LifecycleManager.class).toInstance(manager); requestInjection(manager); } }, Modules.override( new DefaultModule() { @Provides public AutoContext getContext() { return new AutoContext() { @Override public boolean hasProfile(String profile) { return profiles.contains(profile); } @Override public boolean hasModule(String className) { return moduleNames.contains(className); } @Override public boolean hasBinding(Key<?> key) { return keys.contains(key); } }; } }) .with(bootstrapModule)); PropertySource propertySource = injector.getInstance(PropertySource.class); // Iterate through all loaded modules and filter out any modules that // have failed the condition check. Also, keep track of any override modules // for already installed modules. final List<Module> overrideModules = new ArrayList<>(); final List<Module> moreModules = new ArrayList<>(); for (Module module : loadedModules) { if (!isEnabled(propertySource, module.getClass().getName())) { LOG.info("Ignoring module {}", module.getClass().getName()); continue; } Bootstrap bs = module.getClass().getAnnotation(Bootstrap.class); if (isBootstrap == (bs != null) && evaluateConditions(LOG, injector, module)) { OverrideModule override = module.getClass().getAnnotation(OverrideModule.class); if (override != null) { if (moduleNames.contains(override.value().getName())) { LOG.info(" Adding override module {}", module.getClass().getSimpleName()); overrideModules.add(module); } } else { LOG.info(" Adding conditional module {}", module.getClass().getSimpleName()); moreModules.add(module); } } } final List<Module> extModules = new ArrayList<>(); List<Binding<ModuleProvider>> moduleProviders = injector.findBindingsByType(TypeLiteral.get(ModuleProvider.class)); for (Binding<ModuleProvider> binding : moduleProviders) { Module module = binding.getProvider().get().get(); LOG.debug("Adding exposed bootstrap module {}", module.getClass().getName()); extModules.add(module); } LOG.debug("Root Modules : " + rootModules); LOG.debug("More Modules : " + moreModules); LOG.debug("Override Modules : " + overrideModules); LOG.debug("Ext Modules : " + extModules); LOG.debug("Created {} injector", isBootstrap ? "bootstrap" : "main"); Module m = Modules.override( new AbstractModule() { @Override protected void configure() { install(Modules.combine(rootModules)); install(Modules.combine(moreModules)); } }) .with(Modules.override(overrideModules).with(Modules.combine(extModules))); return m; }