Search in sources :

Example 1 with Unqualified

use of org.glassfish.hk2.api.Unqualified in project glassfish-hk2 by eclipse-ee4j.

the class ServiceLocatorImpl method internalGetService.

@SuppressWarnings("unchecked")
private <T> T internalGetService(Type contractOrImpl, String name, Unqualified unqualified, boolean calledFromSecondChanceResolveMethod, Annotation... qualifiers) {
    checkState();
    Class<?> rawType = ReflectionHelper.getRawClass(contractOrImpl);
    if (rawType != null && (Provider.class.equals(rawType) || IterableProvider.class.equals(rawType))) {
        boolean isIterable = IterableProvider.class.equals(rawType);
        Type requiredType = ReflectionHelper.getFirstTypeArgument(contractOrImpl);
        HashSet<Annotation> requiredQualifiers = new HashSet<Annotation>();
        for (Annotation qualifier : qualifiers) {
            requiredQualifiers.add(qualifier);
        }
        InjecteeImpl injectee = new InjecteeImpl(requiredType);
        injectee.setRequiredQualifiers(requiredQualifiers);
        injectee.setUnqualified(unqualified);
        IterableProviderImpl<?> retVal = new IterableProviderImpl<Object>(this, requiredType, requiredQualifiers, unqualified, injectee, isIterable);
        return (T) retVal;
    }
    ActiveDescriptor<T> ad = internalGetDescriptor(null, contractOrImpl, name, unqualified, false, calledFromSecondChanceResolveMethod, qualifiers);
    if (ad == null)
        return null;
    T retVal = Utilities.createService(ad, null, this, null, rawType);
    return retVal;
}
Also used : Annotation(java.lang.annotation.Annotation) IterableProvider(org.glassfish.hk2.api.IterableProvider) Provider(jakarta.inject.Provider) IterableProvider(org.glassfish.hk2.api.IterableProvider) Type(java.lang.reflect.Type) ErrorType(org.glassfish.hk2.api.ErrorType) ParameterizedType(java.lang.reflect.ParameterizedType) InjecteeImpl(org.glassfish.hk2.utilities.InjecteeImpl) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with Unqualified

use of org.glassfish.hk2.api.Unqualified in project glassfish-hk2 by eclipse-ee4j.

the class DefaultUnqualifiedTest method testIterableProviderReturnsAllEvenIfUnqualified.

/**
 * Tests that injection of IterableProvider does NOT have the
 * default Unqualified applied to it
 */
// @org.junit.Ignore
@Test
public void testIterableProviderReturnsAllEvenIfUnqualified() {
    ServiceLocator locator = LocatorHelper.getServiceLocator(BoardGameImpl.class, DollImpl.class, TrainImpl.class, ChildsRoom.class);
    locator.setDefaultUnqualified(new UnqualifiedImpl());
    ChildsRoom childsRoom = locator.getService(ChildsRoom.class);
    IterableProvider<Toy> allToys = childsRoom.getAllToys();
    Assert.assertEquals(3, allToys.getSize());
    int lcv = 0;
    for (Toy toy : allToys) {
        switch(lcv) {
            case 0:
                Assert.assertEquals(BoardGameImpl.class, toy.getClass());
                break;
            case 1:
                Assert.assertEquals(TrainImpl.class, toy.getClass());
                break;
            case 2:
                Assert.assertEquals(DollImpl.class, toy.getClass());
                break;
            default:
                Assert.fail("Should not get here");
        }
        lcv++;
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) UnqualifiedImpl(org.glassfish.hk2.utilities.UnqualifiedImpl) Test(org.junit.Test)

Example 3 with Unqualified

use of org.glassfish.hk2.api.Unqualified in project glassfish-hk2 by eclipse-ee4j.

the class Utilities method getParamInformation.

private static AnnotationInformation getParamInformation(Annotation[] memberAnnotations) {
    boolean useDefault = true;
    Set<Annotation> qualifiers = null;
    boolean optional = false;
    boolean self = false;
    Unqualified unqualified = null;
    for (Annotation anno : memberAnnotations) {
        if (ReflectionHelper.isAnnotationAQualifier(anno)) {
            if (qualifiers == null)
                qualifiers = new HashSet<Annotation>();
            qualifiers.add(anno);
            useDefault = false;
        } else if (Optional.class.equals(anno.annotationType())) {
            optional = true;
            useDefault = false;
        } else if (Self.class.equals(anno.annotationType())) {
            self = true;
            useDefault = false;
        } else if (Unqualified.class.equals(anno.annotationType())) {
            unqualified = (Unqualified) anno;
            useDefault = false;
        }
    }
    if (useDefault)
        return DEFAULT_ANNOTATION_INFORMATION;
    if (qualifiers == null)
        qualifiers = DEFAULT_ANNOTATION_INFORMATION.qualifiers;
    return new AnnotationInformation(qualifiers, optional, self, unqualified);
}
Also used : Optional(org.jvnet.hk2.annotations.Optional) Unqualified(org.glassfish.hk2.api.Unqualified) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with Unqualified

use of org.glassfish.hk2.api.Unqualified in project glassfish-hk2 by eclipse-ee4j.

the class ServiceLocatorImpl method internalGetDescriptor.

@SuppressWarnings("unchecked")
private <T> ActiveDescriptor<T> internalGetDescriptor(Injectee onBehalfOf, Type contractOrImpl, String name, Unqualified unqualified, boolean isIterable, boolean calledFromSecondChanceResolveMethod, Annotation... qualifiers) throws MultiException {
    if (contractOrImpl == null)
        throw new IllegalArgumentException();
    Class<?> rawClass = ReflectionHelper.getRawClass(contractOrImpl);
    // Can't be a TypeVariable or Wildcard
    if (rawClass == null)
        return null;
    Utilities.checkLookupType(rawClass);
    rawClass = Utilities.translatePrimitiveType(rawClass);
    name = getName(name, qualifiers);
    NarrowResults results = null;
    LinkedList<ErrorService> currentErrorHandlers = null;
    ImmediateResults immediate = null;
    unqualified = getEffectiveUnqualified(unqualified, isIterable, qualifiers);
    final CacheKey cacheKey = new CacheKey(contractOrImpl, name, unqualified, qualifiers);
    final Filter filter = new UnqualifiedIndexedFilter(rawClass.getName(), name, unqualified);
    final IgdCacheKey igdCacheKey = new IgdCacheKey(cacheKey, name, onBehalfOf, contractOrImpl, rawClass, qualifiers, filter);
    rLock.lock();
    try {
        final IgdValue value = igdCache.compute(igdCacheKey);
        final boolean freshOne = value.freshnessKeeper.compareAndSet(1, 2);
        if (!freshOne) {
            immediate = narrow(// locator
            this, // candidates
            null, // requiredType
            contractOrImpl, // name
            name, // onBehalfOf
            onBehalfOf, // onlyOne
            true, // doValidation
            true, // cachedResults
            value.results, // filter
            filter, // qualifiers
            qualifiers);
            results = immediate.getTimelessResults();
        } else {
            results = value.results;
            immediate = value.immediate;
        }
        if (!results.getErrors().isEmpty()) {
            currentErrorHandlers = new LinkedList<ErrorService>(errorHandlers);
        }
    } finally {
        rLock.unlock();
    }
    if (currentErrorHandlers != null) {
        // Do this next call OUTSIDE of the lock
        Utilities.handleErrors(results, currentErrorHandlers);
    }
    // Must do validation here in order to allow for caching
    ActiveDescriptor<T> postValidateResult = immediate.getImmediateResults().isEmpty() ? null : (ActiveDescriptor<T>) immediate.getImmediateResults().get(0);
    // See https://java.net/jira/browse/HK2-170
    if (!calledFromSecondChanceResolveMethod && postValidateResult == null) {
        final Injectee injectee;
        if (onBehalfOf == null) {
            final HashSet<Annotation> requiredQualifiers = new HashSet<Annotation>();
            if (qualifiers != null && qualifiers.length > 0) {
                for (final Annotation qualifier : qualifiers) {
                    if (qualifier != null) {
                        requiredQualifiers.add(qualifier);
                    }
                }
            }
            final InjecteeImpl injecteeImpl = new InjecteeImpl(contractOrImpl);
            injecteeImpl.setRequiredQualifiers(requiredQualifiers);
            injecteeImpl.setUnqualified(unqualified);
            injectee = injecteeImpl;
        } else {
            injectee = onBehalfOf;
        }
        postValidateResult = (ActiveDescriptor<T>) secondChanceResolve(injectee);
    }
    return postValidateResult;
}
Also used : Annotation(java.lang.annotation.Annotation) ErrorService(org.glassfish.hk2.api.ErrorService) RethrowErrorService(org.glassfish.hk2.utilities.RethrowErrorService) Injectee(org.glassfish.hk2.api.Injectee) Filter(org.glassfish.hk2.api.Filter) CacheKeyFilter(org.glassfish.hk2.utilities.cache.CacheKeyFilter) IndexedFilter(org.glassfish.hk2.api.IndexedFilter) InjecteeImpl(org.glassfish.hk2.utilities.InjecteeImpl) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with Unqualified

use of org.glassfish.hk2.api.Unqualified in project glassfish-hk2 by eclipse-ee4j.

the class ServiceLocatorImpl method getUnqualifiedServiceHandle.

/* package */
<T> ServiceHandle<T> getUnqualifiedServiceHandle(Type contractOrImpl, Unqualified unqualified, boolean isIterable, Annotation... qualifiers) throws MultiException {
    checkState();
    ActiveDescriptor<T> ad = internalGetDescriptor(null, contractOrImpl, null, unqualified, isIterable, qualifiers);
    if (ad == null)
        return null;
    return getServiceHandle(ad, new InjecteeImpl(contractOrImpl));
}
Also used : InjecteeImpl(org.glassfish.hk2.utilities.InjecteeImpl)

Aggregations

InjecteeImpl (org.glassfish.hk2.utilities.InjecteeImpl)6 Annotation (java.lang.annotation.Annotation)5 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 Type (java.lang.reflect.Type)3 Unqualified (org.glassfish.hk2.api.Unqualified)3 ErrorService (org.glassfish.hk2.api.ErrorService)2 Filter (org.glassfish.hk2.api.Filter)2 IndexedFilter (org.glassfish.hk2.api.IndexedFilter)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2 RethrowErrorService (org.glassfish.hk2.utilities.RethrowErrorService)2 UnqualifiedImpl (org.glassfish.hk2.utilities.UnqualifiedImpl)2 CacheKeyFilter (org.glassfish.hk2.utilities.cache.CacheKeyFilter)2 Test (org.junit.Test)2 Provider (jakarta.inject.Provider)1 Executable (java.lang.reflect.Executable)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 LinkedList (java.util.LinkedList)1 ErrorType (org.glassfish.hk2.api.ErrorType)1 Injectee (org.glassfish.hk2.api.Injectee)1