@Override public Object resolve(Injectee injectee, ServiceHandle root) { InjecteeImpl injecteeWrapper = new InjecteeImpl(); injecteeWrapper.setRequiredType(injectee.getRequiredType()); injecteeWrapper.setParent(injectee.getParent()); injecteeWrapper.setRequiredQualifiers(injectee.getRequiredQualifiers()); injecteeWrapper.setOptional(injectee.isOptional()); injecteeWrapper.setPosition(injectee.getPosition()); injecteeWrapper.setFactory(ReflectionHelper.isSubClassOf(injectee.getRequiredType(), Factory.class)); injecteeWrapper.setInjecteeDescriptor(new ForeignDescriptorImpl(injectee.getInjecteeDescriptor())); Object instance = jerseyResolver.resolve(injecteeWrapper); if (injecteeWrapper.isFactory()) { return asFactory(instance); } else { return instance; } }
private CacheKey(Injectee injectee) { this.injectee = injectee; this.hash = Objects.hash(injectee.getInjecteeClass(), injectee.getInjecteeDescriptor(), injectee.getParent(), injectee.getRequiredQualifiers(), injectee.getRequiredType(), injectee.getPosition()); }
/* package */ FactoryCreator(ServiceLocator locator, ActiveDescriptor<?> factoryDescriptor) { this.locator = locator; this.factoryDescriptor = factoryDescriptor; if (!factoryDescriptor.isReified()) { factoryDescriptor = locator.reifyDescriptor(factoryDescriptor); } InstantiationServiceImpl found = null; for (Injectee factoryInjectee : factoryDescriptor.getInjectees()) { if (InstantiationService.class.equals(factoryInjectee.getRequiredType())) { found = locator.getService(InstantiationServiceImpl.class); break; } } // Will ONLY be non-null if the factory has injected the InstantiationService instantiationService = found; }
checkState(); Type requiredType = injectee.getRequiredType(); Class<?> rawType = ReflectionHelper.getRawClass(requiredType); if (rawType == null) { throw new MultiException(new IllegalArgumentException( "Invalid injectee with required type of " + injectee.getRequiredType() + " passed to getInjecteeDescriptor")); (ReflectionHelper.getFirstTypeArgument(requiredType)), injectee.getRequiredQualifiers(), injectee.getUnqualified(), injectee, isIterable); ReflectionHelper.getFirstTypeArgument(requiredType), injectee.getRequiredQualifiers()); Set<Annotation> qualifiersAsSet = injectee.getRequiredQualifiers(); String name = ReflectionHelper.getNameFromAllQualifiers(qualifiersAsSet, injectee.getParent()); return internalGetDescriptor(injectee, requiredType, name, injectee.getUnqualified(), false, calledFromSecondChanceResolveMethod, qualifiers);
checkParentage(from, into); ServiceLocatorUtilities.addClasses(into, BridgingInjectionResolver.class); into.getService(BridgingInjectionResolver.class).setRemoteServiceLocator(from); if (a.getLocatorId() == b.getLocatorId()) { throw new IllegalStateException("Locator " + originalA + " is a child of or is the same as locator " + b); if (injectee.isOptional()) { return null; throw new MultiException(new UnsatisfiedDependencyException(injectee)); return _remoteServiceLocator.getService(ad, root, injectee);
@Override public boolean justInTimeResolution(Injectee injectee) { if (injectee == null || injectee.isOptional()) return false; Class configBeanType; try { configBeanType = (Class) injectee.getRequiredType(); } catch (Exception ex) { return false; } if (!ConfigExtension.class.isAssignableFrom(configBeanType) && !DomainExtension.class.isAssignableFrom(configBeanType)) { return false; } if (!isInjectionSupported(configBeanType)) return false; if (domain == null) { return false; } if (ConfigExtension.class.isAssignableFrom(configBeanType)) { if (config == null) { config = locator.getService(Config.class, ServerEnvironmentImpl.DEFAULT_INSTANCE_NAME); } ConfigBeanProxy pr = config.getExtensionByType(configBeanType); return pr != null; } else if (DomainExtension.class.isAssignableFrom(configBeanType)) { ConfigBeanProxy pr = domain.getExtensionByType(configBeanType); return pr != null; } return false; }
@Override public Injectee apply(CacheKey cacheKey) { Injectee injectee = cacheKey.getInjectee(); if (injectee.getParent() != null) { if (Field.class.isAssignableFrom(injectee.getParent().getClass())) { Field f = (Field) injectee.getParent(); if (foreignRequestScopedComponents.get().contains(f.getDeclaringClass())) { final Class<?> clazz = f.getType(); if (serviceLocator.getServiceHandle(clazz).getActiveDescriptor().getScopeAnnotation() == RequestScoped.class) { final AbstractActiveDescriptor<Object> descriptor = BuilderHelper.activeLink(clazz) .to(clazz) .in(RequestScoped.class) .build(); return new DescriptorOverridingInjectee(injectee, descriptor); } } } } return injectee; } });
@Override public Object resolve(Injectee injectee, ServiceHandle<?> root) { Type requiredType = injectee.getRequiredType(); boolean isHk2Factory = ReflectionHelper.isSubClassOf(requiredType, Factory.class); Injectee newInjectee; if (isHk2Factory) { newInjectee = getFactoryInjectee(injectee, ReflectionHelper.getTypeArgument(requiredType, 0)); } else { newInjectee = foreignRequestScopedInjecteeCache.apply(new CacheKey(injectee)); } ActiveDescriptor<?> ad = descriptorCache.apply(new CacheKey(newInjectee)); if (ad != null) { final ServiceHandle handle = serviceLocator.getServiceHandle(ad, newInjectee); if (isHk2Factory) { return asFactory(handle); } else { return handle.getService(); } } return null; }
if (!root.isReified()) { root = (ActiveDescriptor<T>) locator.reifyDescriptor(root, injectee); context = locator.resolveContext(root.getScopeAnnotation()); if (injectee != null && injectee.isOptional()) return null; " in scope " + root.getScope() + " an error occured while locating the context"); me.addError(addMe); MultiException me = new MultiException(th); me.addError(addMe); throw me;
@Override public Object resolve(final Injectee injectee, final ServiceHandle<?> root) { if (!injectee.getRequiredType().equals(EntityManagerFactory.class)) { return null; } final PersistenceUnit annotation = injectee.getParent().getAnnotation(PersistenceUnit.class); final String unitName = annotation.unitName(); if (!persistenceUnits.containsKey(unitName)) { throw new ContainerException(LocalizationMessages.PERSISTENCE_UNIT_NOT_CONFIGURED(unitName)); } return Proxy.newProxyInstance( this.getClass().getClassLoader(), new Class[] {EntityManagerFactory.class}, new ThreadLocalNamedInvoker<EntityManagerFactory>(persistenceUnits.get(unitName))); }
public Object resolve(Injectee injectee, ServiceHandle<?> root) { AnnotatedElement annotated = injectee.getParent(); Annotation[] annotations; if (annotated.getClass().equals(Constructor.class)) { annotations = ((Constructor) annotated).getParameterAnnotations()[injectee.getPosition()]; } else { annotations = annotated.getDeclaredAnnotations(); Class componentClass = injectee.getInjecteeClass(); Type genericType = injectee.getRequiredType(); boolean isHk2Factory = ReflectionHelper.isSubClassOf(genericType, Factory.class);
ClassLoader loader; if (injectee != null) { AnnotatedElement parent = injectee.getParent(); MultiException me = new MultiException(th); me.addError(th2); throw new MultiException(th);
if (!injectee.isSelf()) continue; Class<?> requiredRawClass = ReflectionHelper.getRawClass(injectee.getRequiredType()); if (requiredRawClass == null || !(ActiveDescriptor.class.equals(requiredRawClass))) { collector.addThrowable(new IllegalArgumentException("Injection point " + injectee + if (injectee.isOptional()) { collector.addThrowable(new IllegalArgumentException("Injection point " + injectee + " is marked both @Optional and @Self")); if (!injectee.getRequiredQualifiers().isEmpty()) { collector.addThrowable(new IllegalArgumentException("Injection point " + injectee + " is marked @Self but has other qualifiers"));
@Override public Object resolve(Injectee injectee, ServiceHandle<?> root) { final String factoryName = ReflectionHelper.getNameFromAllQualifiers( injectee.getRequiredQualifiers(), injectee.getParent()); final ConnectionSource connectionSource = persistenceSessionManager.get().getPersistenceContext( new PersistenceContextKey(new PersistenceContextFactoryKey(factoryName)), ConnectionSource.class, injectee.getInjecteeDescriptor().getScope()); return connectionSource; }
@SuppressWarnings("unchecked") @Override public <T> T getService(ActiveDescriptor<T> activeDescriptor, ServiceHandle<?> root, Injectee originalRequest) throws MultiException { checkState(); Type contractOrImpl = (originalRequest == null) ? null : originalRequest.getRequiredType(); Class<?> rawClass = ReflectionHelper.getRawClass(contractOrImpl); if (root == null) { ServiceHandleImpl<T> tmpRoot = new ServiceHandleImpl<T>(this, activeDescriptor, originalRequest); return Utilities.createService(activeDescriptor, originalRequest, this, tmpRoot, rawClass); } ServiceHandleImpl<?> rootImpl = (ServiceHandleImpl<?>) root; ServiceHandleImpl<T> subHandle = internalGetServiceHandle(activeDescriptor, contractOrImpl, originalRequest); if (PerLookup.class.equals(activeDescriptor.getScopeAnnotation())) { rootImpl.addSubHandle(subHandle); } rootImpl.pushInjectee(originalRequest); try { return subHandle.getService((ServiceHandle<T>) root); } finally { rootImpl.popInjectee(); } }
@Override public Object resolve(Injectee injectee, ServiceHandle<?> root) { ActiveDescriptor<?> ad = locator.getInjecteeDescriptor(injectee); if (ad == null) { if (injectee.isOptional()) return null; throw new MultiException(new UnsatisfiedDependencyException(injectee)); } return locator.getService(ad, root, injectee); }
/** * @see org.glassfish.hk2.api.InjectionResolver#resolve(org.glassfish.hk2.api.Injectee, * org.glassfish.hk2.api.ServiceHandle) */ @Override public Object resolve(Injectee arg0, ServiceHandle<?> arg1) { Type requiredType = arg0.getRequiredType(); return this.serviceLocator.getService( this.getServiceCosumer((Class<?>) requiredType, arg0.getInjecteeClass(), arg0.getRequiredQualifiers())) .getService(); }
/** * NOTE: There can be only one {@link Annotation} that is a {@link Qualifier} or {@link BindingAnnotation}. * They're the same but HK2 does not know about {@link BindingAnnotation}. * * @see Qualifier * @see BindingAnnotation * @see javax.inject.Named * @see com.google.inject.name.Named */ private static Set<Annotation> getQualifiers(Injectee injectee) { // JSR 330's @Qualifier Set<Annotation> qualifiers = injectee.getRequiredQualifiers(); if (!qualifiers.isEmpty()) { return qualifiers; } AnnotatedElement element = injectee.getParent(); int position = injectee.getPosition(); // Guice's @BindingAnnotation is the same as @Qualifier Annotation annotation = getBindingAnnotation(element, position); if (annotation != null) { return Collections.singleton(annotation); } return Collections.emptySet(); }
private boolean hasEncodedAnnotation(Injectee injectee) { AnnotatedElement element = injectee.getParent(); final boolean isConstructor = element instanceof Constructor; final boolean isMethod = element instanceof Method; // if injectee is method or constructor, check its parameters if (isConstructor || isMethod) { Annotation[] annotations; if (isMethod) { annotations = ((Method) element).getParameterAnnotations()[injectee.getPosition()]; } else { annotations = ((Constructor) element).getParameterAnnotations()[injectee.getPosition()]; } for (Annotation annotation : annotations) { if (annotation.annotationType().equals(Encoded.class)) { return true; } } } // check injectee itself (method, constructor or field) if (element.isAnnotationPresent(Encoded.class)) { return true; } // check class which contains injectee Class<?> clazz = injectee.getInjecteeClass(); return clazz.isAnnotationPresent(Encoded.class); }
/** * Returns {@code true} if the {@link Injectee} has a Jersey SPI {@link Contract}. * * @see Contract */ public static boolean isContract(Injectee injectee) { Type type = injectee.getRequiredType(); return type instanceof Class && ((Class<?>)type).isAnnotationPresent(Contract.class); }