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(); } }
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)); } } }
@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; } }
@Override public Object get(String name) { return supplierNameMap.get(name).supplier().get(); }
@Override public Iterable<String> names() { return supplierNameMap.keySet(); }
@Override public Iterable<Object> values() { return supplierNameMap.values(); }
@Override public boolean has(String name) { return supplierNameMap.containsKey(name); }