Search in sources :

Example 1 with QualifierInstance

use of org.jboss.weld.resolution.QualifierInstance in project core by weld.

the class Parsers method createQualifierInstance.

/**
 * @param qualifier
 * @param resourceLoader
 * @param beanManager
 * @return the qualifier instance or null
 */
@SuppressWarnings("unchecked")
static QualifierInstance createQualifierInstance(String qualifier, ResourceLoader resourceLoader, BeanManager beanManager) {
    String qualifierType = qualifier.contains("(") ? qualifier.substring(0, qualifier.indexOf("(")) : qualifier;
    Class<?> qualifierClass = tryLoadClass(qualifierType, resourceLoader);
    if (qualifierClass != null) {
        Class<? extends Annotation> qualifierAnnotationClass;
        try {
            qualifierAnnotationClass = (Class<? extends Annotation>) qualifierClass;
        } catch (Exception e) {
            return null;
        }
        if (beanManager.isQualifier(qualifierAnnotationClass)) {
            Map<String, Object> values;
            Method[] qualifierMembers = SecurityActions.getDeclaredMethods(qualifierAnnotationClass);
            if (qualifierMembers.length == 0) {
                values = Collections.emptyMap();
            } else {
                Map<String, String> memberValues = parseMemberValues(qualifier);
                values = new HashMap<>();
                for (Method method : qualifierMembers) {
                    if (!method.isAnnotationPresent(Nonbinding.class)) {
                        String value = memberValues.get(method.getName());
                        if (value == null) {
                            Object defaultValue = method.getDefaultValue();
                            if (defaultValue == null) {
                                return null;
                            }
                            values.put(method.getName(), defaultValue);
                        } else {
                            Object extracted = extractValue(method, value);
                            if (extracted == null) {
                                return null;
                            }
                            values.put(method.getName(), extracted);
                        }
                    }
                }
            }
            return new QualifierInstance(qualifierAnnotationClass, Collections.unmodifiableMap(values));
        }
    }
    return null;
}
Also used : Nonbinding(javax.enterprise.util.Nonbinding) Method(java.lang.reflect.Method) QualifierInstance(org.jboss.weld.resolution.QualifierInstance)

Example 2 with QualifierInstance

use of org.jboss.weld.resolution.QualifierInstance in project core by weld.

the class Bindings method areQualifiersEquivalent.

public static boolean areQualifiersEquivalent(Annotation qualifier1, Annotation qualifier2, MetaAnnotationStore store) {
    checkQualifier(qualifier1, store);
    checkQualifier(qualifier2, store);
    QualifierInstance q1 = QualifierInstance.of(qualifier1, store);
    QualifierInstance q2 = QualifierInstance.of(qualifier2, store);
    return q1.equals(q2);
}
Also used : QualifierInstance(org.jboss.weld.resolution.QualifierInstance)

Example 3 with QualifierInstance

use of org.jboss.weld.resolution.QualifierInstance in project core by weld.

the class DefaultJsonDataProvider method receiveAvailableBeans.

@Override
public String receiveAvailableBeans(int pageIndex, int pageSize, String filters, String representation) {
    Map<String, String> filterValues = Queries.Filters.parseFilters(filters);
    // First validate input
    if (!filterValues.containsKey(BDA_ID)) {
        return getError("Bean deployment archive id (bdaId) must be specified: " + filters);
    }
    // Find the bean deployment archive
    BeanManagerImpl beanManager = probe.getBeanManager(filterValues.get(BDA_ID));
    if (beanManager == null) {
        return getError("Unable to find the bean deployment archive for: " + filterValues.get(BDA_ID));
    }
    Set<Bean<?>> beans = null;
    // Parse required type
    Type requiredType;
    if (filterValues.get(REQUIRED_TYPE) != null) {
        requiredType = Parsers.parseType(filterValues.get(REQUIRED_TYPE), beanManager.getServices().get(ResourceLoader.class));
    } else {
        requiredType = Object.class;
    }
    if (requiredType == null) {
        return getError("Invalid required type: parsing error or the type is not accessible from the specified bean archive!");
    }
    ResolvableBuilder resolvableBuilder = new ResolvableBuilder(requiredType, beanManager);
    // Parse qualifiers
    List<QualifierInstance> qualifierInstances;
    if (filterValues.get(QUALIFIERS) != null) {
        qualifierInstances = Parsers.parseQualifiers(filterValues.get(QUALIFIERS), beanManager.getServices().get(ResourceLoader.class), beanManager);
    } else {
        qualifierInstances = Collections.singletonList(QualifierInstance.DEFAULT);
    }
    for (QualifierInstance qualifierInstance : qualifierInstances) {
        if (qualifierInstance == null) {
            return getError("Invalid qualifiers: parsing error or one of the qualifier types is not accessible from the specified bean archive!");
        }
        resolvableBuilder.addQualifierUnchecked(qualifierInstance);
    }
    Resolvable resolvable = resolvableBuilder.create();
    // Lookup beans
    beans = beanManager.getBeanResolver().resolve(resolvable, false);
    if (Boolean.valueOf(filterValues.get(RESOLVE))) {
        // Apply resolution rules
        beans = beanManager.getBeanResolver().resolve(beans);
    }
    return JsonObjects.createBeansJson(Queries.find(probe.getOrderedBeans(beans), pageIndex, pageSize, null), probe, beanManager, Representation.from(representation));
}
Also used : Type(java.lang.reflect.Type) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) ResolvableBuilder(org.jboss.weld.resolution.ResolvableBuilder) QualifierInstance(org.jboss.weld.resolution.QualifierInstance) Resolvable(org.jboss.weld.resolution.Resolvable) Bean(javax.enterprise.inject.spi.Bean)

Example 4 with QualifierInstance

use of org.jboss.weld.resolution.QualifierInstance in project core by weld.

the class Bindings method areInterceptorBindingsEquivalent.

public static boolean areInterceptorBindingsEquivalent(Annotation qualifier1, Annotation qualifier2, MetaAnnotationStore store) {
    checkInterceptorBinding(qualifier1, store);
    checkInterceptorBinding(qualifier2, store);
    QualifierInstance q1 = QualifierInstance.of(qualifier1, store);
    QualifierInstance q2 = QualifierInstance.of(qualifier2, store);
    return q1.equals(q2);
}
Also used : QualifierInstance(org.jboss.weld.resolution.QualifierInstance)

Example 5 with QualifierInstance

use of org.jboss.weld.resolution.QualifierInstance in project core by weld.

the class MetaAnnotationStore method getQualifierInstances.

/**
 * @param annotations
 * @return the set of qualifier instances, uses caches if possible
 */
public Set<QualifierInstance> getQualifierInstances(final Set<Annotation> annotations) {
    if (annotations == null || annotations.isEmpty()) {
        return Collections.emptySet();
    }
    ImmutableSet.Builder<QualifierInstance> builder = ImmutableSet.builder();
    boolean useSharedCache = true;
    for (Annotation annotation : annotations) {
        if (isCacheAllowed(annotation)) {
            builder.add(qualifierInstanceCache.getValue(annotation));
        } else {
            builder.add(QualifierInstance.of(annotation, this));
            // Don't use shared object cache if there's some qualifier instance which should not be cached
            useSharedCache = false;
        }
    }
    return useSharedCache ? sharedObjectCache.getSharedSet(builder.build()) : builder.build();
}
Also used : ImmutableSet(org.jboss.weld.util.collections.ImmutableSet) QualifierInstance(org.jboss.weld.resolution.QualifierInstance) Annotation(java.lang.annotation.Annotation)

Aggregations

QualifierInstance (org.jboss.weld.resolution.QualifierInstance)5 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 Bean (javax.enterprise.inject.spi.Bean)1 Nonbinding (javax.enterprise.util.Nonbinding)1 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)1 Resolvable (org.jboss.weld.resolution.Resolvable)1 ResolvableBuilder (org.jboss.weld.resolution.ResolvableBuilder)1 ImmutableSet (org.jboss.weld.util.collections.ImmutableSet)1