@Override public BindingBuilder<T> to(Class<? super T> type) { Objects.requireNonNull(type); _key = Key.of(type); return this; }
@Override public <T> Provider<T> provider(Injector manager, Key<T> key) { Class<?> rawClass = key.rawClass(); return null; }
static <T> InjectionPoint<T> of(Key<T> key) { return new InjectionPointImpl<>(key, key.type(), key.rawClass().getSimpleName(), key.annotations(), key.rawClass()); }
/** * 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())); }
/** * 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])); }
/** * 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(); }
/** * 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; } }
private <T> WebBuilder view(ViewRender<T> view, Key key) { _views.add(new ViewRefRender(view, (Class) key.type(), 0)); return this; }
@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; } }
/** * 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; }
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; }
BindingAmp<T> find(Key<T> key) { for (BindingAmp<T> binding : _list) { if (key.isAssignableFrom(binding.key())) { return binding; } } return null; }
/** * 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); }
/** * 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()); } }
/** * 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())); }
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; }
/** * 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); }
BindingBuilderImpl(InjectorBuilder builder, Class<T> type) { Objects.requireNonNull(builder); _builder = builder; Objects.requireNonNull(type); _key = Key.of(type); _impl = type; }
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; }
sb.append("["); Type type = type(); if (type instanceof Class<?>) { sb.append(((Class<?>) type).getSimpleName());