Search in sources :

Example 1 with Scope

use of javax.inject.Scope in project jersey by jersey.

the class ComponentBag method modelFor.

/**
     * Create a contract provider for a given component class.
     *
     * @param componentClass  component class to create contract provider model for.
     * @param defaultPriority default component priority. If {@value ContractProvider#NO_PRIORITY},
     *                        the value from the component class {@link javax.annotation.Priority} annotation will be used
     *                        (if any).
     * @param contractMap     map of contracts and their binding priorities. If {@code null}, the contracts will
     *                        gathered by introspecting the component class. Content of the contract map
     *                        may be modified during the registration processing.
     * @param modelEnhancer   custom contract provider model enhancer.
     * @return contract provider model for the class.
     */
private static ContractProvider modelFor(final Class<?> componentClass, final int defaultPriority, final Map<Class<?>, Integer> contractMap, final Inflector<ContractProvider.Builder, ContractProvider> modelEnhancer) {
    Map<Class<?>, Integer> contracts = contractMap;
    if (contracts == null) {
        // introspect
        contracts = asMap(Providers.getProviderContracts(componentClass));
    } else {
        // filter custom contracts
        final Iterator<Class<?>> it = contracts.keySet().iterator();
        while (it.hasNext()) {
            final Class<?> contract = it.next();
            if (contract == null) {
                it.remove();
                continue;
            }
            boolean failed = false;
            if (!Providers.isSupportedContract(contract)) {
                Errors.error(LocalizationMessages.CONTRACT_NOT_SUPPORTED(contract, componentClass), Severity.WARNING);
                failed = true;
            }
            if (!contract.isAssignableFrom(componentClass)) {
                Errors.error(LocalizationMessages.CONTRACT_NOT_ASSIGNABLE(contract, componentClass), Severity.WARNING);
                failed = true;
            }
            if (failed) {
                it.remove();
            }
        }
    }
    final ContractProvider.Builder builder = ContractProvider.builder(componentClass).addContracts(contracts).defaultPriority(defaultPriority);
    // Process annotations (priority, name bindings, scope)
    final boolean useAnnotationPriority = defaultPriority == ContractProvider.NO_PRIORITY;
    for (Annotation annotation : componentClass.getAnnotations()) {
        if (annotation instanceof Priority) {
            if (useAnnotationPriority) {
                builder.defaultPriority(((Priority) annotation).value());
            }
        } else {
            for (Annotation metaAnnotation : annotation.annotationType().getAnnotations()) {
                if (metaAnnotation instanceof NameBinding) {
                    builder.addNameBinding(annotation.annotationType());
                }
                if (metaAnnotation instanceof Scope) {
                    builder.scope(annotation.annotationType());
                }
            }
        }
    }
    return modelEnhancer.apply(builder);
}
Also used : Scope(javax.inject.Scope) ContractProvider(org.glassfish.jersey.model.ContractProvider) Priority(javax.annotation.Priority) NameBinding(javax.ws.rs.NameBinding) Annotation(java.lang.annotation.Annotation)

Example 2 with Scope

use of javax.inject.Scope in project kernel by exoplatform.

the class ContainerUtil method getScope.

/**
 * Gives the scope defined for the given class
 * @param clazz the class for which we want the scope
 * @param ignoreExplicit indicates whether the explicit scope must be ignored
 * @return a class representing the annotation type of the scope
 * @throws DefinitionException in case the definition of the scope is not correct
 */
public static Class<? extends Annotation> getScope(Class<?> clazz, boolean ignoreExplicit) throws DefinitionException {
    Annotation[] annotations = clazz.getAnnotations();
    Class<? extends Annotation> scope = null;
    Class<? extends Annotation> defaultScope = null;
    boolean hasStereotype = false;
    for (int i = 0; i < annotations.length; i++) {
        Annotation annotation = annotations[i];
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (!ignoreExplicit && (annotationType.isAnnotationPresent(Scope.class) || annotationType.isAnnotationPresent(NormalScope.class))) {
            if (scope != null) {
                throw new DefinitionException("You cannot set several scopes to the class " + clazz.getName());
            }
            scope = annotationType;
        } else if (annotationType.isAnnotationPresent(Stereotype.class)) {
            hasStereotype = true;
            Annotation[] stereotypeAnnotations = annotationType.getAnnotations();
            for (int j = 0; j < stereotypeAnnotations.length; j++) {
                Annotation stereotypeAnnotation = stereotypeAnnotations[j];
                Class<? extends Annotation> stereotypeAnnotationType = stereotypeAnnotation.annotationType();
                if (stereotypeAnnotationType.isAnnotationPresent(Scope.class) || stereotypeAnnotationType.isAnnotationPresent(NormalScope.class)) {
                    if (defaultScope != null && !defaultScope.equals(stereotypeAnnotationType)) {
                        throw new DefinitionException("The class " + clazz.getName() + " has stereotypes with different default scope");
                    }
                    defaultScope = stereotypeAnnotationType;
                }
            }
        }
    }
    if (scope != null)
        return scope;
    if (defaultScope != null)
        return defaultScope;
    if (hasStereotype) {
        throw new DefinitionException("The class " + clazz.getName() + " has at least one stereotype but doesn't have any scope, please set an explicit scope");
    }
    return null;
}
Also used : NormalScope(javax.enterprise.context.NormalScope) Scope(javax.inject.Scope) NormalScope(javax.enterprise.context.NormalScope) Stereotype(javax.enterprise.inject.Stereotype) DefinitionException(org.exoplatform.container.context.DefinitionException) Annotation(java.lang.annotation.Annotation)

Example 3 with Scope

use of javax.inject.Scope in project guice by google.

the class DaggerMethodScanner method configureBindsKey.

private <T> void configureBindsKey(Binder binder, Method method, Key<T> key) {
    // the Dagger processor already validates the assignability of these two keys. parameterKey()
    // has no way to infer the correct type parameter, so we use rawtypes instead.
    @SuppressWarnings({ "unchecked", "rawtypes" }) ScopedBindingBuilder scopedBindingBuilder = binder.bind((Key) processMultibindingAnnotations(binder, method, key)).to(parameterKey(method.getParameters()[0]));
    getAnnotatedAnnotation(method, Scope.class).ifPresent(scope -> scopedBindingBuilder.in(scope.annotationType()));
}
Also used : Scope(javax.inject.Scope) ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) MapKey(dagger.MapKey) Key(com.google.inject.Key) Keys.parameterKey(com.google.inject.daggeradapter.Keys.parameterKey)

Aggregations

Scope (javax.inject.Scope)3 Annotation (java.lang.annotation.Annotation)2 Key (com.google.inject.Key)1 ScopedBindingBuilder (com.google.inject.binder.ScopedBindingBuilder)1 Keys.parameterKey (com.google.inject.daggeradapter.Keys.parameterKey)1 MapKey (dagger.MapKey)1 Priority (javax.annotation.Priority)1 NormalScope (javax.enterprise.context.NormalScope)1 Stereotype (javax.enterprise.inject.Stereotype)1 NameBinding (javax.ws.rs.NameBinding)1 DefinitionException (org.exoplatform.container.context.DefinitionException)1 ContractProvider (org.glassfish.jersey.model.ContractProvider)1