@Override @SuppressWarnings("unchecked") public Object resolve(Injectee injectee) { 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(); if (injectee.isFactory()) { targetGenericType = ReflectionHelper.getTypeArgument(genericType, 0); } else { if (injectee.isFactory()) { return (Supplier<Object>) () -> valueProvider.apply(request.get()); } else {
private RequiredTypeOverridingInjectee(Injectee injectee, Type requiredType) { setFactory(injectee.isFactory()); setInjecteeClass(injectee.getInjecteeClass()); setInjecteeDescriptor(injectee.getInjecteeDescriptor()); setOptional(injectee.isOptional()); setParent(injectee.getParent()); setPosition(injectee.getPosition()); setRequiredQualifiers(injectee.getRequiredQualifiers()); setRequiredType(requiredType); } }
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); }
private DependencyDescriptor createSpringDependencyDescriptor(final Injectee injectee) { AnnotatedElement annotatedElement = injectee.getParent(); if (annotatedElement.getClass().isAssignableFrom(Field.class)) { return new DependencyDescriptor((Field) annotatedElement, !injectee.isOptional()); } else if (annotatedElement.getClass().isAssignableFrom(Method.class)) { return new DependencyDescriptor( new MethodParameter((Method) annotatedElement, injectee.getPosition()), !injectee.isOptional()); } else { return new DependencyDescriptor( new MethodParameter((Constructor) annotatedElement, injectee.getPosition()), !injectee.isOptional()); } }
private static InjecteeImpl toInjecteeImpl(org.glassfish.jersey.internal.inject.Injectee injectee) { InjecteeImpl hk2injectee = new InjecteeImpl() { @Override public Class<?> getInjecteeClass() { return injectee.getInjecteeClass(); } }; hk2injectee.setRequiredType(injectee.getRequiredType()); hk2injectee.setRequiredQualifiers(injectee.getRequiredQualifiers()); hk2injectee.setParent(injectee.getParent()); if (injectee.getInjecteeDescriptor() != null) { hk2injectee.setInjecteeDescriptor((ActiveDescriptor<?>) injectee.getInjecteeDescriptor().get()); } return hk2injectee; }
@Override public Object resolve(Injectee injectee) { 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))); }
@Override public Object resolve(Injectee injectee) { Injectee newInjectee = injectee; if (injectee.isFactory()) { newInjectee = getFactoryInjectee(injectee, ReflectionHelper.getTypeArgument(injectee.getRequiredType(), 0)); } Bean<?> bean = descriptorCache.apply(newInjectee.getRequiredType()); if (bean != null) { CreationalContext ctx = beanManager.get().createCreationalContext(bean); Object result = bean.create(ctx); if (injectee.isFactory()) { return (Supplier<Object>) () -> result; } else { return result; } } return null; }
/** * Returns a proxy (newly created or cached) which is able to call {@link InjectionResolver} with the given {@link Injectee} * to get the value in proper scope. * * @param injectee information about the injection point. * @param resolver dedicated resolver which find the value. * @return created proxy which resolve the value in the proper scope. */ public Object proxy(Injectee injectee, InjectionResolver resolver) { return cachedProxies.computeIfAbsent(injectee.getParent(), type -> createProxy(injectee, resolver)); }
/** * Returns {@code true} if one of the proxiable annotations is present on the clazz into which values are injected. * <p> * In these cases the value is not proxiable: * <ul> * <li>Class without the annotation</li> * <li>Class annotated by {@link javax.enterprise.context.RequestScoped}</li> * <li>Class annotated by {@link org.glassfish.jersey.process.internal.RequestScoped}</li> * <ul/> * * @param injectee information about the injection point. * @return {@code true} if contains one proxiable annotation at least. */ public boolean isPrixiable(Injectee injectee) { return !ignoredClass(injectee.getRequiredType()) && isPrixiable(injectee.getParentClassScope()); }
@Override public Class<?> getInjecteeClass() { return injectee.getInjecteeClass(); } };
/** * Returns a proxy (newly created or cached) which is able to call the given {@link Supplier}. This method does not cache * a result. * * @param injectee information about the injection point. * @param supplier supplier called using the proxy. * @return created proxy which resolve the value in the proper scope. */ public Object noCachedProxy(Injectee injectee, Supplier<Object> supplier) { return createProxy(getClass(injectee.getRequiredType()), supplier); }
private static InjecteeImpl toInjecteeImpl(org.glassfish.jersey.internal.inject.Injectee injectee) { InjecteeImpl hk2injectee = new InjecteeImpl() { @Override public Class<?> getInjecteeClass() { return injectee.getInjecteeClass(); } }; hk2injectee.setRequiredType(injectee.getRequiredType()); hk2injectee.setRequiredQualifiers(injectee.getRequiredQualifiers()); hk2injectee.setParent(injectee.getParent()); if (injectee.getInjecteeDescriptor() != null) { hk2injectee.setInjecteeDescriptor((ActiveDescriptor<?>) injectee.getInjecteeDescriptor().get()); } return hk2injectee; }
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); }
@Override public Object resolve(Injectee injectee) { 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))); }
private DependencyDescriptor createSpringDependencyDescriptor(final Injectee injectee) { AnnotatedElement annotatedElement = injectee.getParent(); if (annotatedElement.getClass().isAssignableFrom(Field.class)) { return new DependencyDescriptor((Field) annotatedElement, !injectee.isOptional()); } else if (annotatedElement.getClass().isAssignableFrom(Method.class)) { return new DependencyDescriptor( new MethodParameter((Method) annotatedElement, injectee.getPosition()), !injectee.isOptional()); } else { return new DependencyDescriptor( new MethodParameter((Constructor) annotatedElement, injectee.getPosition()), !injectee.isOptional()); } }
@Override public Object resolve(Injectee injectee) { AnnotatedElement parent = injectee.getParent(); String beanName = null; if (parent != null) { Qualifier an = parent.getAnnotation(Qualifier.class); if (an != null) { beanName = an.value(); } } boolean required = parent != null ? parent.getAnnotation(Autowired.class).required() : false; return getBeanFromSpringContext(beanName, injectee, required); }
@Override public Class<?> getInjecteeClass() { return injectee.getInjecteeClass(); } };
private Object createProxy(Injectee injectee, InjectionResolver resolver) { return createProxy(getClass(injectee.getRequiredType()), () -> resolver.resolve(injectee)); }
@Override @SuppressWarnings("unchecked") public Object resolve(Injectee injectee) { 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(); if (injectee.isFactory()) { targetGenericType = ReflectionHelper.getTypeArgument(genericType, 0); } else { if (injectee.isFactory()) { return (Supplier<Object>) () -> valueProvider.apply(request.get()); } else {
private static InjecteeImpl toInjecteeImpl(org.glassfish.jersey.internal.inject.Injectee injectee) { InjecteeImpl hk2injectee = new InjecteeImpl() { @Override public Class<?> getInjecteeClass() { return injectee.getInjecteeClass(); } }; hk2injectee.setRequiredType(injectee.getRequiredType()); hk2injectee.setRequiredQualifiers(injectee.getRequiredQualifiers()); hk2injectee.setParent(injectee.getParent()); if (injectee.getInjecteeDescriptor() != null) { hk2injectee.setInjecteeDescriptor((ActiveDescriptor<?>) injectee.getInjecteeDescriptor().get()); } return hk2injectee; }