Search in sources :

Example 1 with Annotations

use of com.google.inject.internal.Annotations in project guice by google.

the class InjectionPoint method forConstructorOf.

/**
 * Returns a new injection point for the injectable constructor of {@code type}.
 *
 * <p>If {@code atInjectRequired} is true, the constructor must be annotated with {@code @Inject}.
 * If {@code atInjectRequired} is false, either a {@code @Inject} annotated constructor or a
 * non-private no arg constructor is required to be defined by the class corresponding to {@code
 * type}.
 *
 * @param type a concrete type with exactly one constructor annotated {@code @Inject}, or a
 *     no-arguments constructor that is not private.
 * @param atInjectRequired whether the constructor must be annotated with {@code Inject}.
 * @throws ConfigurationException if there is no injectable constructor, more than one injectable
 *     constructor, or if parameters of the injectable constructor are malformed, such as a
 *     parameter with multiple binding annotations.
 * @since 5.0
 */
public static InjectionPoint forConstructorOf(TypeLiteral<?> type, boolean atInjectRequired) {
    Class<?> rawType = getRawType(type.getType());
    Errors errors = new Errors(rawType);
    List<Constructor<?>> atInjectConstructors = Arrays.stream(rawType.getDeclaredConstructors()).filter(constructor -> constructor.isAnnotationPresent(Inject.class) || constructor.isAnnotationPresent(javax.inject.Inject.class)).collect(Collectors.toList());
    Constructor<?> injectableConstructor = null;
    atInjectConstructors.stream().filter(constructor -> constructor.isAnnotationPresent(Inject.class)).filter(constructor -> constructor.getAnnotation(Inject.class).optional()).forEach(errors::optionalConstructor);
    if (atInjectConstructors.size() > 1) {
        errors.tooManyConstructors(rawType);
    } else {
        injectableConstructor = Iterables.getOnlyElement(atInjectConstructors, null);
        if (injectableConstructor != null) {
            checkForMisplacedBindingAnnotations(injectableConstructor, errors);
        }
    }
    if (atInjectRequired && injectableConstructor == null) {
        errors.atInjectRequired(type);
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    if (injectableConstructor != null) {
        return new InjectionPoint(type, injectableConstructor);
    }
    // If no annotated constructor is found, look for a no-arg constructor instead.
    try {
        Constructor<?> noArgConstructor = rawType.getDeclaredConstructor();
        // Disallow private constructors on non-private classes (unless they have @Inject)
        if (Modifier.isPrivate(noArgConstructor.getModifiers()) && !Modifier.isPrivate(rawType.getModifiers())) {
            errors.missingConstructor(type);
            throw new ConfigurationException(errors.getMessages());
        }
        checkForMisplacedBindingAnnotations(noArgConstructor, errors);
        return new InjectionPoint(type, noArgConstructor);
    } catch (NoSuchMethodException e) {
        errors.missingConstructor(type);
        throw new ConfigurationException(errors.getMessages());
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) MoreTypes.getRawType(com.google.inject.internal.MoreTypes.getRawType) Inject(com.google.inject.Inject) Annotations(com.google.inject.internal.Annotations) Key(com.google.inject.Key) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ErrorsException(com.google.inject.internal.ErrorsException) KotlinSupport(com.google.inject.internal.KotlinSupport) Map(java.util.Map) Method(java.lang.reflect.Method) DeclaredMembers(com.google.inject.internal.DeclaredMembers) Nullability(com.google.inject.internal.Nullability) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectArrays(com.google.common.collect.ObjectArrays) Iterator(java.util.Iterator) Classes(com.google.inject.internal.util.Classes) Predicate(java.util.function.Predicate) Member(java.lang.reflect.Member) AnnotatedType(java.lang.reflect.AnnotatedType) ConfigurationException(com.google.inject.ConfigurationException) Set(java.util.Set) Field(java.lang.reflect.Field) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Errors(com.google.inject.internal.Errors) List(java.util.List) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) TypeLiteral(com.google.inject.TypeLiteral) Collections(java.util.Collections) AnnotatedElement(java.lang.reflect.AnnotatedElement) Inject(com.google.inject.Inject) Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException) Constructor(java.lang.reflect.Constructor)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 ObjectArrays (com.google.common.collect.ObjectArrays)1 ConfigurationException (com.google.inject.ConfigurationException)1 Inject (com.google.inject.Inject)1 Key (com.google.inject.Key)1 TypeLiteral (com.google.inject.TypeLiteral)1 Annotations (com.google.inject.internal.Annotations)1 DeclaredMembers (com.google.inject.internal.DeclaredMembers)1 Errors (com.google.inject.internal.Errors)1 ErrorsException (com.google.inject.internal.ErrorsException)1 KotlinSupport (com.google.inject.internal.KotlinSupport)1 MoreTypes.getRawType (com.google.inject.internal.MoreTypes.getRawType)1 Nullability (com.google.inject.internal.Nullability)1 Classes (com.google.inject.internal.util.Classes)1 Annotation (java.lang.annotation.Annotation)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 AnnotatedType (java.lang.reflect.AnnotatedType)1