@Nullable @Override public <T> T tryGetInstance(Annotation qualifier, Class<T> type) throws InjectionException { return tryGetInstance(Qualifiers.match(qualifier), type); }
@Override public <T> T getInstance(Class<T> type) throws InjectionException { T obj = tryGetInstance(Qualifiers.matchDefault(), type); if (obj == null) { throw new ResolutionException("no resolution available for " + type); } else { return obj; } }
@Override @SuppressWarnings("rawtypes") public Binding set(@Nonnull Class<? extends Annotation> param) { Preconditions.checkNotNull(param); // Parameter annotation appears on the alias target Class<? extends Annotation> real = Qualifiers.resolveAliases(param); final Parameter annot = real.getAnnotation(Parameter.class); if (annot == null) { throw new IllegalArgumentException(param.toString() + "has no Parameter annotation"); } Class<?> type = annot.value(); Binding<?> binding; if (type.equals(File.class)) { binding = LenskitBindingImpl.wrap(bind(File.class), new StringToFileConversion()); } else { binding = bind(annot.value()); } return binding.withQualifier(param); }
@Override public Binding<T> unqualified() { QualifierMatcher q = Qualifiers.matchNone(); return new BindingImpl<T>(context, sourceType, excludeTypes, q, cachePolicy, fixed); }
public AnnotationHelper(Annotation... annots) { attrs = new HashMap<Class<? extends Annotation>, Annotation>(); Annotation foundQualifier = null; for (Annotation a: annots) { if (a.annotationType().getAnnotation(Attribute.class) != null) { // a is an attribute attrs.put(a.annotationType(), a); } if (foundQualifier == null && Qualifiers.isQualifier(a.annotationType())) { // a is a qualifier foundQualifier = a; } } qualifier = foundQualifier; }
@Override public Binding<T> withAnyQualifier() { QualifierMatcher q = Qualifiers.matchAny(); return new BindingImpl<T>(context, sourceType, excludeTypes, q, cachePolicy, fixed); }
/** * @param annotType Annotation type class to match; {@code null} to match only the lack of a * qualifier. * @return A QualifierMatcher that matches any annotation of the given class * type. */ public static QualifierMatcher match(Class<? extends Annotation> annotType) { if (annotType == null) { return matchNone(); } else { return new AnnotationClassMatcher(annotType); } }
@SuppressWarnings("unchecked") public static void isQualifier(Class<?> type) { if (!Annotation.class.isAssignableFrom(type)) { throw new InvalidBindingException(type, "Type is not an Annotation"); } if (!Qualifiers.isQualifier((Class<? extends Annotation>) type)) { throw new InvalidBindingException(type, "Annotation is not annotated with @Qualifier"); } }
public <T> T tryGetInstance(Class<? extends Annotation> qual, Class<T> type) throws InjectionException { return tryGetInstance(Qualifiers.match(qual), type); }
/** * Get the component of a particular type, if one is already instantiated. This is useful to extract pre-built * models from serialized recommender engines, for example. * @param type The required component type. * @param <T> The required component type. * @return The component instance, or {@code null} if no instance can be retreived (either because no such * component is configured, or it is not yet instantiated). */ @Nullable public <T> T getComponent(Class<T> type) { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node == null) { return null; } Satisfaction sat = node.getLabel().getSatisfaction(); if (sat instanceof InstanceSatisfaction) { return type.cast(((InstanceSatisfaction) sat).getInstance()); } else { return null; } }
@Override public boolean apply(Annotation q) { // We test if the alias-resolved types match. Class<? extends Annotation> qtype = (q == null ? null : q.annotationType()); if (qtype == null) { return false; } else { Class<? extends Annotation> qact = resolveAliases(qtype); return actual.equals(qact); } }
/** * @param annot Annotation instance to match, or {@code null} to match only the lack of a qualifier. * @return A QualifierMatcher that matches annotations equaling annot */ public static QualifierMatcher match(@Nonnull Annotation annot) { if (annot == null) { return matchNone(); } else if (annot.annotationType().getDeclaredMethods().length == 0) { logger.debug("using type matcher for nullary annotation {}", annot); // Instances of the same nullary annotation are all equal to each other, so just do // type checking. This makes aliasing work with annotation value matchers, b/c we // do not allow aliases to have parameters. The matcher still has value priority. return new AnnotationClassMatcher(annot.annotationType(), DefaultMatcherPriority.MATCH_VALUE); } else { return new AnnotationMatcher(annot); } }
@Override public <T> T getInstance(Annotation qualifier, Class<T> type) throws InjectionException { T obj = tryGetInstance(Qualifiers.match(qualifier), type); if (obj == null) { throw new ResolutionException("no resolution available for " + type + " with qualifier " + qualifier); } else { return obj; } }
@Nullable public <T> T tryGetInstance(Class<T> type) throws InjectionException { Desire d = Desires.create(null, type, true); DAGEdge<Component, Dependency> e = graph.getOutgoingEdgeWithLabel(l -> l.hasInitialDesire(d)); if (e != null) { return type.cast(instantiator.instantiate(e.getTail())); } else { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node != null) { return type.cast(instantiator.instantiate(node)); } else { return null; } } }
public AnnotationClassMatcher(Class<? extends Annotation> type, DefaultMatcherPriority prio) { super(prio); Preconditions.notNull("type", type); Preconditions.isQualifier(type); this.type = type; // find the actual type to match (resolving aliases) actual = resolveAliases(type); }
@Override public Binding<T> withQualifier(@Nonnull Class<? extends Annotation> qualifier) { QualifierMatcher q = Qualifiers.match(qualifier); return new BindingImpl<T>(context, sourceType, excludeTypes, q, cachePolicy, fixed); }
/** * Create an unanchored ReflectionContextElementMatcher that matches the given type * with the default qualifier matcher. * * @param type The type to match * @throws NullPointerException if type is null */ public TypeElementMatcher(Class<?> type) { this(type, Qualifiers.matchDefault()); }
@Override @SuppressWarnings("rawtypes") public Binding set(@Nonnull Class<? extends Annotation> param) { Preconditions.checkNotNull(param); // Parameter annotation appears on the alias target Class<? extends Annotation> real = Qualifiers.resolveAliases(param); final Parameter annot = real.getAnnotation(Parameter.class); if (annot == null) { throw new IllegalArgumentException(param.toString() + "has no Parameter annotation"); } Class<?> type = annot.value(); Binding<?> binding; if (type.equals(File.class)) { binding = LenskitBindingImpl.wrap(bind(File.class), new StringToFileConversion()); } else { binding = bind(annot.value()); } return binding.withQualifier(param); }
@Override public Context at(@Nullable Annotation annot, Class<?> type) { return in(Qualifiers.match(annot), type, true); }
@Override public Context within(Class<?> type) { return in(Qualifiers.matchDefault(), type, false); }