io.baratine.inject.Key类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(118)

本文整理了Java中io.baratine.inject.Key类的一些代码示例,展示了Key类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Key类的具体详情如下:
包路径:io.baratine.inject.Key
类名称:Key

Key介绍

[英]Key is used for binding beans and services to injection points.

Key is defined by it's associated type and annotations.
[中]键用于将bean和服务绑定到注入点。
键由其关联的类型和注释定义。

代码示例

代码示例来源:origin: baratine/baratine

@Override
public BindingBuilder<T> to(Class<? super T> type)
{
 Objects.requireNonNull(type);
 _key = Key.of(type);
 return this;
}

代码示例来源:origin: baratine/baratine

@Override
public <T> Provider<T> provider(Injector manager, Key<T> key)
{
 Class<?> rawClass = key.rawClass();
 return null;
}

代码示例来源:origin: baratine/baratine

static <T> InjectionPoint<T> of(Key<T> key)
{
 return new InjectionPointImpl<>(key,
   key.type(),
   key.rawClass().getSimpleName(),
   key.annotations(),
   key.rawClass());
}

代码示例来源:origin: baratine/baratine

/**
 * Builds Key from field's generic type and field's qualifying annotations.
 * A qualifying annotation is marked with a {@code @Qualifier}.
 *
 * @param field field for deriving type and annotation from
 * @param <T>   target type
 * @return instance of a Key
 */
public static <T> Key<T> of(Field field)
{
 return new Key(field.getGenericType(),
         qualifiers(field.getAnnotations()));
}

代码示例来源:origin: baratine/baratine

/**
 * Returns an object producer.
 */
private <T> BindingAmp<T> findObjectBinding(Key<T> key)
{
 Objects.requireNonNull(key);
 if (key.qualifiers().length != 1) {
  throw new IllegalArgumentException();
 }
 return (BindingAmp) findBinding(Key.of(Object.class,
                     key.qualifiers()[0]));
}

代码示例来源:origin: baratine/baratine

/**
 * default provider
 */
private <T> Provider<T> createProvider(Key<T> key)
{
 Class<T> type = (Class<T>) key.rawClass();
 if (Provider.class.equals(type)) {
  TypeRef subType = TypeRef.of(key.type()).to(Provider.class).param(0);
  Key<Object> subkey = Key.of(subType.type(), key.annotationTypes());
  return (Provider) new ProviderProvider(key, -10, provider(subkey));
 }
 else if (Optional.class.equals(type)) {
  TypeRef subType = TypeRef.of(key.type()).to(Optional.class).param(0);
  Key<Object> subkey = Key.of(subType.type(), key.annotationTypes());
  return (Provider) new ProviderOptional(key, -10, provider(subkey));
 }
 else if (type.isInterface() || Modifier.isAbstract(type.getModifiers())) {
  return new ProviderNull(key, -10000, new InjectScopeSingleton());
 }
 int priority = -10;
 // auto-provider is factory
 InjectScope<T> scope = findScope(type);
 BindingAmp<T> binding = new ProviderConstructor<>(this, key, priority, scope, type);
 binding.bind();
 return binding.provider();
}

代码示例来源:origin: baratine/baratine

/**
 * Creates a new instance for a given key.
 */
@Override
public <T> T instance(Key<T> key)
{
 Objects.requireNonNull(key);
 Class<T> type = (Class) key.rawClass();
 if (type.equals(Provider.class)) {
  TypeRef typeRef = TypeRef.of(key.type());
  TypeRef param = typeRef.param(0);
  return (T) provider(Key.of(param.type()));
 }
 Provider<T> provider = provider(key);
 if (provider != null) {
  return provider.get();
 }
 else {
  return null;
 }
}

代码示例来源:origin: baratine/baratine

private <T> WebBuilder view(ViewRender<T> view, Key key)
{
 _views.add(new ViewRefRender(view, (Class) key.type(), 0));
 return this;
}

代码示例来源:origin: baratine/baratine

@Override
public <T> Provider<T> provider(Injector manager, Key<T> key)
{
 Class<T> rawClass = key.rawClass();
 Service service = rawClass.getAnnotation(Service.class);
 if (service == null) {
  return null;
 }
 if (key.isAnnotationPresent(ServiceImpl.class)) {
  return null;
 }
 String address = getManager().address(rawClass);
 if (address != null && ! address.isEmpty()) {
  T proxy = getManager().service(address).as(rawClass);
  return ()->proxy;
 }
 else {
  return null;
 }
}

代码示例来源:origin: baratine/baratine

/**
 * Tests if key is assignable. Key is considered assignable if annotation types
 * match, Type matches and annotation instances match.
 *
 * @param key test key
 * @return true if assignable, false otherwise
 */
public boolean isAssignableFrom(Key<? super T> key)
{
 Objects.requireNonNull(key);
 for (Class<? extends Annotation> annType : _annTypes) {
  if (! containsType(annType, key._annTypes)) {
   return false;
  }
 }
 if (_type instanceof ParameterizedType) {
  if (! (key._type instanceof ParameterizedType)) {
   return false;
  }
  if (! isAssignableFrom((ParameterizedType) _type,
              (ParameterizedType) key._type)) {
   return false;
  }
 }
 if (_anns.length > 0 && key._anns.length > 0) {
  return isAssignableFrom(_anns, key._anns);
 }
 return true;
}

代码示例来源:origin: baratine/baratine

private Service metaService(Key<?> key)
{
 for (Annotation ann : key.annotations()) {
  Service service = ann.annotationType().getAnnotation(Service.class);
  if (service != null) {
   return service;
  }
 }
 for (Class<?> annType : key.annotationTypes()) {
  Service service = annType.getAnnotation(Service.class);
  if (service != null) {
   return service;
  }
 }
 return null;
}

代码示例来源:origin: baratine/baratine

BindingAmp<T> find(Key<T> key)
{
 for (BindingAmp<T> binding : _list) {
  if (key.isAssignableFrom(binding.key())) {
   return binding;
  }
 }
 return null;
}

代码示例来源:origin: baratine/baratine

/**
 * Builds Key from Type and annotation types
 *
 * @param type     target type
 * @param annTypes associated annotations
 * @param <T>      type
 * @return instance of a Key
 */
public static <T> Key<T> of(Type type, Class<? extends Annotation> []annTypes)
{
 return new Key<>(type, annTypes);
}

代码示例来源:origin: baratine/baratine

/**
 * Returns raw class of associated type
 *
 * @return raw class
 */
public Class<T> rawClass()
{
 Type type = type();
 if (type instanceof Class) {
  return (Class) type;
 }
 else if (type instanceof ParameterizedType) {
  ParameterizedType pType = (ParameterizedType) type;
  return (Class) pType.getRawType();
 }
 else {
  throw new UnsupportedOperationException(type + " " + type.getClass().getName());
 }
}

代码示例来源:origin: baratine/baratine

/**
 * Builds Key from method's generic return type and method's qualifying
 * annotations. A qualifying annotation is marked with a {@code @Qualifier}.
 *
 * @param method method for deriving return type and annotations from
 * @param <T>    target type
 * @return instance of a Key
 */
public static <T> Key<T> of(Method method)
{
 return new Key(method.getGenericReturnType(),
         qualifiers(method.getAnnotations()));
}

代码示例来源:origin: baratine/baratine

public List<Binding<T>> bindings(Key<T> key)
{
 List<Binding<T>> bindings = new ArrayList<>();
 for (BindingAmp<T> binding : _list) {
  if (key.isAssignableFrom(binding.key())) {
   bindings.add(binding);
  }
 }
 return bindings;
}

代码示例来源:origin: baratine/baratine

/**
 * Builds key from a Class
 *
 * @param type target class
 * @param <T>  target type
 * @return instance of a Key
 */
public static <T> Key<T> of(Class<T> type)
{
 return new Key<>(type);
}

代码示例来源:origin: baratine/baratine

BindingBuilderImpl(InjectorBuilder builder,
          Class<T> type)
{
 Objects.requireNonNull(builder);
 _builder = builder;
 Objects.requireNonNull(type);
 _key = Key.of(type);
 _impl = type;
}

代码示例来源:origin: baratine/baratine

private boolean isAssignableFrom(ParameterizedType typeA,
                 ParameterizedType typeB)
{
 Type []paramA = typeA.getActualTypeArguments();
 Type []paramB = typeB.getActualTypeArguments();
 if (paramA.length != paramB.length) {
  return false;
 }
 for (int i = 0; i < paramA.length; i++) {
  Class<?> classA = rawClass(paramA[i]);
  Class<?> classB = rawClass(paramB[i]);
  if (! classA.equals(classB)
    && ! classA.equals(Object.class)
    && ! classB.equals(Object.class)) {
   return false;
  }
 }
 return true;
}

代码示例来源:origin: baratine/baratine

sb.append("[");
Type type = type();
if (type instanceof Class<?>) {
 sb.append(((Class<?>) type).getSimpleName());

相关文章