Java Code Examples for org.elasticsearch.common.inject.Inject#optional()

The following examples show how to use org.elasticsearch.common.inject.Inject#optional() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InjectionPoint.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
InjectionPoint(TypeLiteral<?> type, Field field) {
    this.member = field;

    Inject inject = field.getAnnotation(Inject.class);
    this.optional = inject.optional();

    Annotation[] annotations = field.getAnnotations();

    Errors errors = new Errors(field);
    Key<?> key = null;
    try {
        key = Annotations.getKey(type.getFieldType(field), field, annotations, errors);
    } catch (ErrorsException e) {
        errors.merge(e.getErrors());
    }
    errors.throwConfigurationExceptionIfErrorsExist();

    this.dependencies = Collections.<Dependency<?>>singletonList(
        newDependency(key, Nullability.allowsNull(annotations), -1));
}
 
Example 2
Source File: InjectionPoint.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private static <M extends Member & AnnotatedElement> void addInjectorsForMembers(
        TypeLiteral<?> typeLiteral, Factory<M> factory, boolean statics,
        Collection<InjectionPoint> injectionPoints, Errors errors) {
    for (M member : factory.getMembers(getRawType(typeLiteral.getType()))) {
        if (isStatic(member) != statics) {
            continue;
        }

        Inject inject = member.getAnnotation(Inject.class);
        if (inject == null) {
            continue;
        }

        try {
            injectionPoints.add(factory.create(typeLiteral, member, errors));
        } catch (ConfigurationException ignorable) {
            if (!inject.optional()) {
                errors.merge(ignorable.getErrorMessages());
            }
        }
    }
}
 
Example 3
Source File: InjectionPoint.java    From crate with Apache License 2.0 6 votes vote down vote up
InjectionPoint(TypeLiteral<?> type, Field field) {
    this.member = field;

    Inject inject = field.getAnnotation(Inject.class);
    this.optional = inject.optional();

    Annotation[] annotations = field.getAnnotations();

    Errors errors = new Errors(field);
    Key<?> key = null;
    try {
        key = Annotations.getKey(type.getFieldType(field), field, annotations, errors);
    } catch (ErrorsException e) {
        errors.merge(e.getErrors());
    }
    errors.throwConfigurationExceptionIfErrorsExist();

    this.dependencies = Collections.<Dependency<?>>singletonList(
        newDependency(key, Nullability.allowsNull(annotations), -1));
}
 
Example 4
Source File: InjectionPoint.java    From crate with Apache License 2.0 6 votes vote down vote up
private static <M extends Member & AnnotatedElement> void addInjectorsForMembers(
        TypeLiteral<?> typeLiteral, Factory<M> factory, boolean statics,
        Collection<InjectionPoint> injectionPoints, Errors errors) {
    for (M member : factory.getMembers(getRawType(typeLiteral.getType()))) {
        if (isStatic(member) != statics) {
            continue;
        }

        Inject inject = member.getAnnotation(Inject.class);
        if (inject == null) {
            continue;
        }

        try {
            injectionPoints.add(factory.create(typeLiteral, member, errors));
        } catch (ConfigurationException ignorable) {
            if (!inject.optional()) {
                errors.merge(ignorable.getErrorMessages());
            }
        }
    }
}
 
Example 5
Source File: InjectionPoint.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
InjectionPoint(TypeLiteral<?> type, Method method) {
    this.member = method;

    Inject inject = method.getAnnotation(Inject.class);
    this.optional = inject.optional();

    this.dependencies = forMember(method, type, method.getParameterAnnotations());
}
 
Example 6
Source File: InjectionPoint.java    From crate with Apache License 2.0 5 votes vote down vote up
InjectionPoint(TypeLiteral<?> type, Method method) {
    this.member = method;

    Inject inject = method.getAnnotation(Inject.class);
    this.optional = inject.optional();

    this.dependencies = forMember(method, type, method.getParameterAnnotations());
}
 
Example 7
Source File: InjectionPoint.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new injection point for the injectable constructor of {@code type}.
 *
 * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject},
 *             or a no-arguments constructor that is not private.
 * @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.
 */
public static InjectionPoint forConstructorOf(TypeLiteral<?> type) {
    Class<?> rawType = getRawType(type.getType());
    Errors errors = new Errors(rawType);

    Constructor<?> injectableConstructor = null;
    for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
        Inject inject = constructor.getAnnotation(Inject.class);
        if (inject != null) {
            if (inject.optional()) {
                errors.optionalConstructor(constructor);
            }

            if (injectableConstructor != null) {
                errors.tooManyConstructors(rawType);
            }

            injectableConstructor = constructor;
            checkForMisplacedBindingAnnotations(injectableConstructor, errors);
        }
    }

    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(rawType);
            throw new ConfigurationException(errors.getMessages());
        }

        checkForMisplacedBindingAnnotations(noArgConstructor, errors);
        return new InjectionPoint(type, noArgConstructor);
    } catch (NoSuchMethodException e) {
        errors.missingConstructor(rawType);
        throw new ConfigurationException(errors.getMessages());
    }
}
 
Example 8
Source File: InjectionPoint.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new injection point for the injectable constructor of {@code type}.
 *
 * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject},
 *             or a no-arguments constructor that is not private.
 * @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.
 */
public static InjectionPoint forConstructorOf(TypeLiteral<?> type) {
    Class<?> rawType = getRawType(type.getType());
    Errors errors = new Errors(rawType);

    Constructor<?> injectableConstructor = null;
    for (Constructor<?> constructor : rawType.getConstructors()) {
        Inject inject = constructor.getAnnotation(Inject.class);
        if (inject != null) {
            if (inject.optional()) {
                errors.optionalConstructor(constructor);
            }

            if (injectableConstructor != null) {
                errors.tooManyConstructors(rawType);
            }

            injectableConstructor = constructor;
            checkForMisplacedBindingAnnotations(injectableConstructor, errors);
        }
    }

    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.getConstructor();

        // Disallow private constructors on non-private classes (unless they have @Inject)
        if (Modifier.isPrivate(noArgConstructor.getModifiers())
                && !Modifier.isPrivate(rawType.getModifiers())) {
            errors.missingConstructor(rawType);
            throw new ConfigurationException(errors.getMessages());
        }

        checkForMisplacedBindingAnnotations(noArgConstructor, errors);
        return new InjectionPoint(type, noArgConstructor);
    } catch (NoSuchMethodException e) {
        errors.missingConstructor(rawType);
        throw new ConfigurationException(errors.getMessages());
    }
}