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;
}
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++;
}
}
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);
}
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;
}
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));
}
Aggregations