private Injector buildServiceInjector(final LifecycleInjectorBuilder lifecycleInjectorBuilder) { // 1. Startup the Governator LifecycleManager final LifecycleInjector lifecycleInjector = lifecycleInjectorBuilder.build(); final LifecycleManager lifecycleManager = lifecycleInjector.getLifecycleManager(); try { lifecycleManager.start(); } catch (Exception e) { getLogger().error("Error during LifecycleManager start", e); throw new RuntimeException(e); } // 2. Ensure that we shut everything down properly Runtime.getRuntime().addShutdownHook(new Thread(() -> { getLogger().info("Shutting down {} application.", getName()); lifecycleManager.close(); })); // 3. Grab the Guice injector from which we can get service references return lifecycleInjector.createInjector(); } }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if(annotations.size() == 0) { return false; } LifecycleInjector lifecycleInjector = LifecycleInjector .builder() .withModules(new TorchCompilerModule(this, roundEnv)) .build(); try { lifecycleInjector.getLifecycleManager().start(); } catch (Exception e) { throw new RuntimeException(e); } Injector injector = lifecycleInjector.createInjector(); TorchAnnotationProcessor processor = injector.getInstance(TorchAnnotationProcessor.class); boolean success = processor.process(annotations); lifecycleInjector.getLifecycleManager().close(); return success; }