Ejemplo n.º 1
0
 @SuppressWarnings("unchecked")
 private void assertFailure(Injector injector, Class clazz) {
   try {
     injector.getBinding(clazz);
     fail("Shouldn't have been able to get binding of: " + clazz);
   } catch (ConfigurationException expected) {
     List<Object> sources = Iterables.getOnlyElement(expected.getErrorMessages()).getSources();
     // Assert that the first item in the sources if the key for the class we're looking up,
     // ensuring that each lookup is "new".
     assertEquals(Key.get(clazz).toString(), sources.get(0).toString());
     // Assert that the last item in each lookup contains the InvalidInterface class
     Asserts.assertContains(
         sources.get(sources.size() - 1).toString(), Key.get(InvalidInterface.class).toString());
   }
 }
Ejemplo n.º 2
0
    @Override
    protected void rehash(int newCapacity) {
      // rehash should discard gced keys
      // because otherwise there is a remote probability of
      // having two (Weak|Soft)Keys with accidentally equal hashCodes and different but gced key
      // values
      int oldCapacity = _set.length;
      Object[] oldKeys = _set;
      V[] oldVals = _values;

      _set = new Object[newCapacity];
      _values = (V[]) new Object[newCapacity];

      for (int i = oldCapacity; i-- > 0; ) {
        Object o = oldKeys[i];
        if (o == null || o == REMOVED) continue;
        Key<K> k = (Key<K>) o;
        K key = k.get();
        if (key == null) continue;
        int index = insertionIndex(k);
        if (index < 0) {
          // make 'key' alive till this point to not allow 'o.referent' to be gced
          throwObjectContractViolation(_set[-index - 1], o + "; key: " + key);
        }
        _set[index] = o;
        _values[index] = oldVals[i];
      }
    }
Ejemplo n.º 3
0
 @NotNull
 @Override
 public Navigatable createNavigatable(@NotNull Project project) {
   if (ALTERNATIVE_SOURCE_KEY.get(myFile) != null) {
     return new OpenFileDescriptor(project, getFile(), getLine(), 0);
   }
   return XSourcePositionImpl.doCreateOpenFileDescriptor(project, this);
 }
Ejemplo n.º 4
0
 @NotNull
 @Override
 public VirtualFile getFile() {
   VirtualFile file = ALTERNATIVE_SOURCE_KEY.get(myFile);
   if (file != null) {
     return file;
   }
   return myFile;
 }
Ejemplo n.º 5
0
  @Override
  protected void configure() {
    bind(Lifecycle.class).in(Singleton.class);

    bind(Key.get(ExceptionalCommand.class, StartupStage.class)).to(StartupRegistry.class);
    bind(StartupRegistry.class).in(Singleton.class);

    bind(ShutdownRegistry.class).to(ShutdownRegistryImpl.class);
    bind(Key.get(Command.class, ShutdownStage.class)).to(ShutdownRegistryImpl.class);
    bind(ShutdownRegistryImpl.class).in(Singleton.class);
    bindStartupAction(binder(), ShutdownHookRegistration.class);

    bind(LocalServiceRegistry.class).in(Singleton.class);

    // Ensure that there is at least an empty set for the service runners.
    runnerBinder(binder());

    bindStartupAction(binder(), LocalServiceLauncher.class);
  }
Ejemplo n.º 6
0
 public void testNoImplicitBindingIsCreatedForAnnotatedKeys() {
   try {
     Guice.createInjector().getInstance(Key.get(I.class, Names.named("i")));
     fail();
   } catch (ConfigurationException expected) {
     Asserts.assertContains(
         expected.getMessage(),
         "1) No implementation for " + I.class.getName(),
         "annotated with @" + Named.class.getName() + "(value=i) was bound.",
         "while locating " + I.class.getName(),
         " annotated with @" + Named.class.getName() + "(value=i)");
   }
 }
Ejemplo n.º 7
0
  public void testMapbinderModules() {
    Module module =
        new AbstractModule() {
          @Override
          protected void configure() {
            MapBinder<String, IBaseService> multibinder =
                MapBinder.newMapBinder(
                    binder(), String.class, IBaseService.class, Names.named("abc"));
            multibinder.addBinding("a").to(LoginServiceImpl.class);
            multibinder.addBinding("b").to(LogoutServiceImpl.class);
          }
        };
    Injector injector = Guice.createInjector(module);

    Key<Map<String, IBaseService>> key =
        Key.get(new TypeLiteral<Map<String, IBaseService>>() {}, Names.named("abc"));
    Map<String, IBaseService> abc = injector.getInstance(key);
    abc.get("a").doService("");
    abc.get("b").doService("");
  }
Ejemplo n.º 8
0
  public void testMapbinderModules() {
    Module module =
        new AbstractModule() {
          @Override
          protected void configure() {
            MapBinder<String, IBaseService> multibinder =
                MapBinder.newMapBinder(
                    binder(), String.class, IBaseService.class, Names.named("abc"));
            multibinder.addBinding("a").to(LoginServiceImpl.class);
            //                multibinder.addBinding("b").toInstance("B");

            /*                multibinder = MapBinder.newMapBinder(
                binder(), String.class, String.class, Names.named("abc"));
            multibinder.addBinding("c").toInstance("C");*/
          }
        };
    Injector injector = Guice.createInjector(module);

    Key<Map<String, IBaseService>> key = Key.get(mapOfString, Names.named("abc"));
    Map<String, IBaseService> abc = injector.getInstance(key);
    System.out.println(abc.get("a").doService(""));
    //        Map<String, Set<String>> abcde = injector.getInstance(Key.get(mapOfString));
  }
Ejemplo n.º 9
0
 /** Create a cancellable context that has a deadline. */
 private CancellableContext(
     Context parent, Deadline deadline, ScheduledExecutorService scheduler) {
   super(parent, deriveDeadline(parent, deadline), true);
   if (DEADLINE_KEY.get(this) == deadline) {
     final TimeoutException cause = new TimeoutException("context timed out");
     if (!deadline.isExpired()) {
       // The parent deadline was after the new deadline so we need to install a listener
       // on the new earlier deadline to trigger expiration for this context.
       pendingDeadline =
           deadline.runOnExpiration(
               new Runnable() {
                 @Override
                 public void run() {
                   cancel(cause);
                 }
               },
               scheduler);
     } else {
       // Cancel immediately if the deadline is already expired.
       cancel(cause);
     }
   }
   uncancellableSurrogate = new Context(this, EMPTY_ENTRIES);
 }
Ejemplo n.º 10
0
 /**
  * Returns an instance of the given type with the {@link
  * org.elasticsearch.common.inject.name.Named} annotation value.
  *
  * <p>This method allows you to switch this code <code>
  * injector.getInstance(Key.get(type, Names.named(name)));</code>
  *
  * <p>to the more concise <code>Injectors.getInstance(injector, type, name);</code>
  */
 public static <T> T getInstance(Injector injector, java.lang.Class<T> type, String name) {
   return injector.getInstance(Key.get(type, Names.named(name)));
 }
Ejemplo n.º 11
0
 /**
  * If the parent deadline is before the given deadline there is no need to install the value or
  * listen for its expiration as the parent context will already be listening for it.
  */
 private static Object[][] deriveDeadline(Context parent, Deadline deadline) {
   Deadline parentDeadline = DEADLINE_KEY.get(parent);
   return parentDeadline == null || deadline.isBefore(parentDeadline)
       ? new Object[][] {{DEADLINE_KEY, deadline}}
       : EMPTY_ENTRIES;
 }
Ejemplo n.º 12
0
 /**
  * A context may have an associated {@link Deadline} at which it will be automatically cancelled.
  *
  * @return A {@link io.grpc.Deadline} or {@code null} if no deadline is set.
  */
 @Nullable
 public Deadline getDeadline() {
   return DEADLINE_KEY.get(this);
 }