예제 #1
1
  private void extractClassIntoMaps(Class type, boolean foundName, Supplier supplier) {

    if (type == null) {
      return;
    }
    String named = null;

    Class superClass = type.getSuperclass();

    Class[] superTypes = type.getInterfaces();

    for (Class superType : superTypes) {
      this.supplierTypeMap.put(superType, supplier);
    }

    while (superClass != Object.class) {
      this.supplierTypeMap.put(superClass, supplier);

      if (!foundName) {
        named = NamedUtils.namedValueForClass(superClass);
        if (named != null) {
          supplierNameMap.put(named, new ProviderInfo(named, type, supplier, null));
          foundName = true;
        }
      }

      superTypes = type.getInterfaces();
      for (Class superType : superTypes) {
        this.supplierTypeMap.put(superType, supplier);
      }
      superClass = superClass.getSuperclass();
    }
  }
예제 #2
0
  private void supplierExtraction(ProviderInfo[] suppliers) {
    for (ProviderInfo providerInfo : suppliers) {

      Class<?> type = providerInfo.type();
      /* Get type from value. */
      if (type == null && providerInfo.value() != null) {
        type = providerInfo.value().getClass();
      }
      String named = providerInfo.name();
      Supplier supplier = providerInfo.supplier();

      if (supplier == null) {
        supplier = createSupplier(type, providerInfo.value());
      }

      if (type != null) {

        supplierTypeMap.put(type, supplier);

        /* Named passed in overrides name in class annotation @Named. */
        if (named == null) {

          named = namedValueForClass(type);
        }
      }

      extractClassIntoMaps(type, named != null, supplier);
      if (named != null) {
        supplierNameMap.put(named, new ProviderInfo(named, type, supplier, null));
      }
    }
  }
예제 #3
0
  @Override
  public <T> Supplier<T> getSupplier(final Class<T> type, final String name) {

    ProviderInfo nullInfo = null;

    try {
      Set<ProviderInfo> set = Sets.set(supplierNameMap.getAll(name));

      for (ProviderInfo info : set) {

        if (info.type() == null) {
          nullInfo = info;
          continue;
        }
        if (type.isAssignableFrom(info.type())) {
          return (Supplier<T>) info.supplier();
        }
      }

      return (Supplier<T>)
          (nullInfo != null
              ? nullInfo.supplier()
              : new Supplier<T>() {
                @Override
                public T get() {
                  return null;
                }
              });

    } catch (Exception e) {
      Exceptions.handle(e);
      return null;
    }
  }
예제 #4
0
 @Override
 public Object get(String name) {
   return supplierNameMap.get(name).supplier().get();
 }
예제 #5
0
 @Override
 public Iterable<String> names() {
   return supplierNameMap.keySet();
 }
예제 #6
0
 @Override
 public Iterable<Object> values() {
   return supplierNameMap.values();
 }
예제 #7
0
 @Override
 public boolean has(String name) {
   return supplierNameMap.containsKey(name);
 }