@Override public <T> void onProvision(ProvisionInvocation<T> provision) { final T instance = provision.provision(); if (instance == null) return; List<Method> methods = new LinkedList<>(); Class c = instance.getClass(); // find PostConstruct methods in class hierarchy, the one from parent class being first in list // so that we invoke them before derived class one. This isn't specified in JSR-250 but implemented // this way in Spring and what most developers would expect to happen. final Set<Class> interfaces = ClassUtils.getAllInterfacesAsSet(instance); while (c != Object.class) { Arrays.stream(c.getDeclaredMethods()) .map(m -> getMethodAndInterfaceDeclarations(m, interfaces)) .flatMap(Collection::stream) .filter(m -> m.getAnnotation(PostConstruct.class) != null) .findFirst() .ifPresent(method -> methods.add(0, method)); c = c.getSuperclass(); } for (Method postConstruct : methods) { try { postConstruct.setAccessible(true); postConstruct.invoke(instance); } catch (final Exception e) { throw new RuntimeException(String.format("@PostConstruct %s", postConstruct), e); } } } }
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { Class<?> clazz = provision.getBinding().getKey().getTypeLiteral().getRawType(); mapper.mapConfig(provision.provision(), config, new IoCContainer() { @Override public <S> S getInstance(String name, Class<S> type) {
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { final T injectee = provision.provision(); if (injectee == null) { return; Object source = provision.getBinding().getSource(); if(source != null && source.toString().contains("spring-guice")) { return; preDestroyMonitor.register(injectee, provision.getBinding(), actions.preDestroyActions);
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { LOG.trace("Found new binding {}", provision.getBinding()); final T result = provision.provision(); if (result == null) { return; } Class<?> klass = result.getClass(); // Loop over the class and superclasses do { for (final Method m : klass.getDeclaredMethods()) { // Inspect declared methods for @OnStage final OnStage onStage = m.getAnnotation(OnStage.class); if (onStage == null) { continue; } LOG.trace("Will invoke {} on {}", m, onStage.value()); LifecycleInvocation invocation = new LifecycleInvocation(new LifecycleStage(onStage.value()), result, m); if (lifecycle != null) { // If the lifecycle is available, register now addListener(invocation); } else { // Otherwise, do it later, when the lifecycle is injected Preconditions.checkState(foundInvocations != null, "Injection after lifecycle start!"); foundInvocations.add(invocation); } } klass = klass.getSuperclass(); } while (klass != null); }
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { T instance = provision.provision(); if (instance != null) { Binding<T> binding = provision.getBinding(); LifecycleManager manager = lifecycleManager.get(); if (manager != null) { Key<T> bindingKey = binding.getKey(); LOGGER.trace("provisioning instance of {}", bindingKey); TypeLiteral<T> bindingType = bindingKey.getTypeLiteral(); for (LifecycleListener listener : manager.getListeners()) { listener.objectInjected(bindingType, instance); } try { LifecycleMethods methods = lifecycleMethods.get(instance.getClass()); if (methods.hasLifecycleAnnotations()) { manager.add(instance, binding, methods); } } catch (ExecutionException e) { // caching problem throw new RuntimeException(e); } catch (Throwable e) { // unknown problem will abort injector start up throw new Error(e); } } } }
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { final Key<?> key = provision.getBinding().getKey(); if (metrics == null) { LOG.debug("LifecycleProvisionListener not initialized yet : {} source={}", key, provision.getBinding().getSource()); return; } if (doneLoading) { return; } // Instantiate the type and pass to the metrics. This time captured will // include invoking any lifecycle events. metrics.push(key); try { provision.provision(); } finally { metrics.pop(); } }
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { writeString(beforeFormatter.format(provision)); indent += indentAmount; try { provision.provision(); writeString(afterFormatter.format(provision)); } catch (Throwable t) { writeString(errorFormatter.format(provision, t)); throw t; } finally { indent -= indentAmount; } }
@Override public <T> void onProvision(final ProvisionInvocation<T> provision) { final T serviceInstance; try { serviceInstance = provision.provision(); } catch (Throwable t) { // If we encounter any errors during provisioning, immediately stop the graph build throw serviceInitErrorHandler.createProvisionException(provision, t); } final IServiceGraphVertex<T> vertex = createGraphVertex(provision.getBinding().getKey(), serviceInstance); serviceGraph.addVertex(vertex); if (serviceScanMatcher.matches(provision.getBinding())) { provision.getBinding().acceptTargetVisitor(this); } }
public ProvisionException createProvisionException( final ProvisionInvocation<?> provision, final Throwable t ) { // On any unexpected provision exception, ensure that we get the full stack trace into the log. // We do this by separating each exception in the causal chain into a separate message. Guice, // by convention, will print the individual causes in a messages list. Unfortunately, we have to // do this because Guice will swallow stack traces for only a single exception. final List<Message> messages = Throwables.getCausalChain(t).stream() .map(cause -> new Message( cause.getClass().getSimpleName() + " during provision of " + provision.getBinding().getKey().getTypeLiteral(), cause )) .collect(Collectors.toList()); return new ProvisionException(messages); } }
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { final T object = provision.provision(); if (object instanceof ApplicationMetadata) { final ApplicationMetadata metadata = (ApplicationMetadata) object; final Injector inj = this.injector.get(); String name; try { name = inj.getInstance(Key.get(String.class, DefaultApplicationName.class)); } catch (Throwable exception) { name = null; } if (!Strings.isNullOrEmpty(name)) { setName(metadata, name); } } }
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { if (!initialized.getAndSet(true) && !context.isActive()) { context.delayedRefresh(); } provision.provision(); } }
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { final T object = provision.provision(); final ProgressBarConfig cfg = this.commandConfig.get(); if (cfg.getEnable() && object instanceof Logger) { final Logger logger = (Logger) object; logger.setLevel(cfg.getLevel().toLog4j()); } }
@Override public <T> void onProvision(final ProvisionInvocation<T> provision) { try { provision.provision(); } catch (Throwable t) { throw createProvisionException(provision, t); } }
@Override public <T> String format(ProvisionInvocation<T> provision, Throwable t) { return String.format("Error creating '%s'. %s", provision.getBinding().getKey().toString(), t.getMessage()); } };
@Override public <T> void onProvision(final ProvisionInvocation<T> provision) { final Class type = provision.getBinding().getKey().getTypeLiteral().getRawType(); if (contextDebugService == null) { created.add(type); } else { checkType(type); } }
@Override public <T> void onProvision(final ProvisionInvocation<T> provision) { final Object monitorComponent = provision.provision(); if (Tracer.class.isAssignableFrom(monitorComponent.getClass())) { registerTracer((Tracer)monitorComponent); } }
@Override public <T> void onProvision(ProvisionInvocation<T> provisionInvocation) { T provision = provisionInvocation.provision(); if (provision instanceof AutoCloseable) { lifecycleManager.registerAutoCloseable(((AutoCloseable) provision)); } } }
@Override public <A> void onProvision(ProvisionInvocation<A> provision) { A provisioned = provision.provision(); Set<ConstraintViolation<A>> constraintViolations = validator.validate(provisioned); if (!constraintViolations.isEmpty()) { throw new VerboseConstraintViolationException(constraintViolations); } } }
@Override public <T> String format(ProvisionInvocation<T> provision) { return provision.getBinding().getKey().toString(); } };
@Override public <T> void onProvision(ProvisionInvocation<T> provision) { final T injectee = provision.provision(); if (injectee != null && injectee instanceof LifecycleListener) { if (manager == null) { pendingLifecycleListeners.add((LifecycleListener) injectee); } else { manager.addListener((LifecycleListener) injectee); } } } }