Search in sources :

Example 1 with Assisted

use of com.google.inject.assistedinject.Assisted in project google-gin by gwtplus.

the class FactoryBinding method injectConstructorHasMatchingParams.

/**
 * Matching logic for {@literal @}{@link Inject} constructor and method
 * parameters.
 *
 * This returns true if all assisted parameters required by the constructor
 * are provided by the factory method.
 */
private boolean injectConstructorHasMatchingParams(TypeLiteral<?> type, Constructor<?> constructor, List<Key<?>> paramList, Errors errors) throws ErrorsException {
    List<TypeLiteral<?>> params = type.getParameterTypes(constructor);
    Annotation[][] paramAnnotations = constructor.getParameterAnnotations();
    int p = 0;
    for (TypeLiteral<?> param : params) {
        Key<?> paramKey = getKey(param, constructor, paramAnnotations[p++], errors);
        if (paramKey.getAnnotationType() == Assisted.class && !paramList.contains(paramKey)) {
            return false;
        }
    }
    return true;
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) Assisted(com.google.inject.assistedinject.Assisted) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 2 with Assisted

use of com.google.inject.assistedinject.Assisted in project google-gin by gwtplus.

the class FactoryBinding method extractConstructorParameters.

/**
 * Matches constructor parameters to method parameters for injection and
 * records remaining parameters as required keys.
 */
private String[] extractConstructorParameters(Key<?> factoryKey, TypeLiteral<?> implementation, Constructor constructor, List<Key<?>> methodParams, Errors errors, Set<Dependency> dependencyCollector) throws ErrorsException {
    // Get parameters with annotations.
    List<TypeLiteral<?>> ctorParams = implementation.getParameterTypes(constructor);
    Annotation[][] ctorParamAnnotations = constructor.getParameterAnnotations();
    int p = 0;
    String[] parameterNames = new String[ctorParams.size()];
    Set<Key<?>> keySet = new LinkedHashSet<Key<?>>();
    for (TypeLiteral<?> ctorParam : ctorParams) {
        Key<?> ctorParamKey = getKey(ctorParam, constructor, ctorParamAnnotations[p], errors);
        if (ctorParamKey.getAnnotationType() == Assisted.class) {
            if (!keySet.add(ctorParamKey)) {
                errors.addMessage(PrettyPrinter.format("%s has more than one parameter of type %s annotated with @Assisted(\"%s\").  " + "Please specify a unique value with the annotation to avoid confusion.", implementation, ctorParamKey.getTypeLiteral().getType(), ((Assisted) ctorParamKey.getAnnotation()).value()));
            }
            int location = methodParams.indexOf(ctorParamKey);
            // This should never happen since the constructor was already checked
            // in #[inject]constructorHasMatchingParams(..).
            Preconditions.checkState(location != -1);
            parameterNames[p] = ReflectUtil.formatParameterName(location);
        } else {
            dependencyCollector.add(new Dependency(factoryKey, ctorParamKey, false, true, constructor.toString()));
        }
        p++;
    }
    return parameterNames;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InjectionPoint(com.google.inject.spi.InjectionPoint) TypeLiteral(com.google.inject.TypeLiteral) Assisted(com.google.inject.assistedinject.Assisted) Key(com.google.inject.Key) Annotations.getKey(com.google.inject.internal.Annotations.getKey)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)2 Assisted (com.google.inject.assistedinject.Assisted)2 InjectionPoint (com.google.inject.spi.InjectionPoint)2 Key (com.google.inject.Key)1 Annotations.getKey (com.google.inject.internal.Annotations.getKey)1 LinkedHashSet (java.util.LinkedHashSet)1