public static void validatePassivating(Class<?> cl, Bean<?> bean, String typeName) { Class<?> beanClass = bean.getBeanClass(); if (!Serializable.class.isAssignableFrom(beanClass) && false) { ConfigException exn = new ConfigException( L.l( "{0}: {1} is an invalid {2} because it is not serializable.", cl.getName(), bean, typeName)); throw exn; // InjectManager.create().addDefinitionError(exn); } for (InjectionPoint ip : bean.getInjectionPoints()) { if (ip.isTransient() || ip.isDelegate()) continue; Class<?> type = getRawClass(ip.getType()); if (type.isInterface()) continue; if (!Serializable.class.isAssignableFrom(type)) { ConfigException exn = new ConfigException( L.l( "{0}: {1} is an invalid {4} because its injection point '{2}' of type {3} is not serializable.", cl.getName(), bean, ip.getMember().getName(), ip.getType(), typeName)); throw exn; } } }
private static void checkFacadeInjectionPoint(InjectionPoint injectionPoint, Class<?> type) { if (injectionPoint.getAnnotated().getBaseType().equals(type)) { if (injectionPoint.getType() instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) injectionPoint.getType(); if (parameterizedType.getActualTypeArguments()[0] instanceof TypeVariable<?>) { throw new DefinitionException(INJECTION_POINT_WITH_TYPE_VARIABLE, injectionPoint); } if (parameterizedType.getActualTypeArguments()[0] instanceof WildcardType) { throw new DefinitionException(INJECTION_POINT_HAS_WILDCARD, type, injectionPoint); } } else { throw new DefinitionException( INJECTION_POINT_MUST_HAVE_TYPE_PARAMETER, type, injectionPoint); } } }
protected static Type getFacadeType(InjectionPoint injectionPoint) { Type genericType = injectionPoint.getType(); if (genericType instanceof ParameterizedType) { return ((ParameterizedType) genericType).getActualTypeArguments()[0]; } else { throw new IllegalStateException(TYPE_PARAMETER_MUST_BE_CONCRETE, injectionPoint); } }
protected static Type getFacadeType(InjectionPoint injectionPoint) { Type genericType = injectionPoint.getType(); if (genericType instanceof ParameterizedType) { return ((ParameterizedType) genericType).getActualTypeArguments()[0]; } else { throw new IllegalStateException(BeanLogger.LOG.typeParameterMustBeConcrete(injectionPoint)); } }
private void validatePassivating(Bean<?> bean) { Type baseType = _annotatedType.getBaseType(); Class<?> cl = getBeanManager().createTargetBaseType(baseType).getRawClass(); boolean isStateful = _annotatedType.isAnnotationPresent(Stateful.class); if (!Serializable.class.isAssignableFrom(cl) && !isStateful) { throw new ConfigException( L.l( "'{0}' is an invalid @{1} bean because it's not serializable for {2}.", cl.getSimpleName(), bean.getScope().getSimpleName(), bean)); } for (InjectionPoint ip : bean.getInjectionPoints()) { if (ip.isTransient()) continue; Type type = ip.getType(); if (ip.getBean() instanceof CdiStatefulBean) continue; if (type instanceof Class<?>) { Class<?> ipClass = (Class<?>) type; if (!ipClass.isInterface() && !Serializable.class.isAssignableFrom(ipClass) && !getBeanManager().isNormalScope(ip.getBean().getScope())) { throw new ConfigException( L.l( "'{0}' is an invalid @{1} bean because '{2}' value {3} is not serializable for {4}.", cl.getSimpleName(), bean.getScope().getSimpleName(), ip.getType(), ip.getMember().getName(), bean)); } } } }
/** * @param bean * @param probe * @return the set of dependents */ static Set<Dependency> getDependents(Bean<?> bean, Probe probe) { Set<Dependency> dependents = new HashSet<Dependency>(); for (Bean<?> candidate : probe.getBeans()) { if (candidate.equals(bean)) { continue; } BeanManager beanManager = probe.getBeanManager(candidate); if (beanManager == null) { // Don't process built-in beans continue; } Set<InjectionPoint> injectionPoints = candidate.getInjectionPoints(); if (injectionPoints != null && !injectionPoints.isEmpty()) { for (InjectionPoint injectionPoint : injectionPoints) { // At this point unsatisfied or ambiguous dependency should not exits Bean<?> candidateDependency = beanManager.resolve( beanManager.getBeans( injectionPoint.getType(), injectionPoint .getQualifiers() .toArray(new Annotation[injectionPoint.getQualifiers().size()]))); boolean satisfies = false; if (isBuiltinBeanButNotExtension(candidateDependency)) { satisfies = bean.equals( probe.getBean( Components.getBuiltinBeanId((AbstractBuiltInBean<?>) candidateDependency))); } else { satisfies = bean.equals(candidateDependency); } if (satisfies) { dependents.add(new Dependency(candidate, injectionPoint)); } } } } return dependents; }
/** * @param bean * @param beanManager * @param probe * @return the set of dependencies */ static Set<Dependency> getDependencies(Bean<?> bean, BeanManager beanManager, Probe probe) { Set<Dependency> dependencies = new HashSet<Dependency>(); Set<InjectionPoint> injectionPoints = bean.getInjectionPoints(); if (injectionPoints != null && !injectionPoints.isEmpty()) { for (InjectionPoint injectionPoint : injectionPoints) { // At this point unsatisfied or ambiguous dependency should not exits Bean<?> dependency = beanManager.resolve( beanManager.getBeans( injectionPoint.getType(), injectionPoint .getQualifiers() .toArray(new Annotation[injectionPoint.getQualifiers().size()]))); if (isBuiltinBeanButNotExtension(dependency)) { dependency = probe.getBean(Components.getBuiltinBeanId((AbstractBuiltInBean<?>) dependency)); } dependencies.add(new Dependency(dependency, injectionPoint)); } } return dependencies; }
/** * Discovery the custom REST beans. * * @param abd the after bean discovery. * @param bm the bean manager. */ void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) { Set<String> restClients = new HashSet<>(); // create the rest clients Set<Bean<?>> allBeans = bm.getBeans( Object.class, new AnnotationLiteral<Any>() { private static final long serialVersionUID = 3109256773218160485L; }); for (Bean<?> bean : allBeans) { Set<InjectionPoint> bb = bean.getInjectionPoints(); if (bb != null) { for (InjectionPoint ip : bb) { RestClient rest = ip.getAnnotated().getAnnotation(RestClient.class); if (rest != null) { // create ID for the bean = class name + configuration id Class<?> clazz = (Class<?>) ip.getType(); String value = rest.value(); String id = clazz.getName() + value; if (!restClients.contains(id)) { restClients.add(id); RestClientServiceBean sbean = new RestClientServiceBean( clazz, value, CdiInterceptorBindingExtension.CDI_AUTOBINDING); abd.addBean(sbean); } } } } } }
/** * Variation of the validateInjectionPoint method which allows the bean to be defined explicitly * (used for disposer method validation) */ public void validateInjectionPoint(InjectionPoint ij, Bean<?> bean, BeanManagerImpl beanManager) { if (ij.getAnnotated().getAnnotation(New.class) != null && ij.getQualifiers().size() > 1) { throw new DefinitionException(NEW_WITH_QUALIFIERS, ij); } if (ij.getType().equals(InjectionPoint.class) && bean == null) { throw new DefinitionException(INJECTION_INTO_NON_BEAN, ij); } if (ij.getType().equals(InjectionPoint.class) && !Dependent.class.equals(bean.getScope())) { throw new DefinitionException(INJECTION_INTO_NON_DEPENDENT_BEAN, ij); } if (ij.getType() instanceof TypeVariable<?>) { throw new DefinitionException(INJECTION_POINT_WITH_TYPE_VARIABLE, ij); } if (!(ij.getMember() instanceof Field) && ij.getAnnotated().isAnnotationPresent(Named.class) && ij.getAnnotated().getAnnotation(Named.class).value().equals("")) { throw new DefinitionException(NON_FIELD_INJECTION_POINT_CANNOT_USE_NAMED, ij); } boolean newBean = (bean instanceof NewManagedBean<?>) || (bean instanceof NewSessionBean<?>); if (!newBean) { checkScopeAnnotations(ij, beanManager.getServices().get(MetaAnnotationStore.class)); } checkFacadeInjectionPoint(ij, Instance.class); checkFacadeInjectionPoint(ij, Event.class); Annotation[] bindings = ij.getQualifiers().toArray(new Annotation[0]); Set<?> resolvedBeans = beanManager.getBeanResolver().resolve(beanManager.getBeans(ij)); if (!isInjectionPointSatisfied(ij, resolvedBeans, beanManager)) { throw new DeploymentException( INJECTION_POINT_HAS_UNSATISFIED_DEPENDENCIES, ij, Formats.formatAnnotations(bindings), Formats.formatType(ij.getType())); } if (resolvedBeans.size() > 1 && !ij.isDelegate()) { throw new DeploymentException( INJECTION_POINT_HAS_AMBIGUOUS_DEPENDENCIES, ij, Formats.formatAnnotations(bindings), Formats.formatType(ij.getType()), resolvedBeans); } // Account for the case this is disabled decorator if (!resolvedBeans.isEmpty()) { Bean<?> resolvedBean = (Bean<?>) resolvedBeans.iterator().next(); if (beanManager .getServices() .get(MetaAnnotationStore.class) .getScopeModel(resolvedBean.getScope()) .isNormal()) { UnproxyableResolutionException ue = Proxies.getUnproxyableTypeException(ij.getType()); if (ue != null) { UnproxyableResolutionException exception = new UnproxyableResolutionException( INJECTION_POINT_HAS_NON_PROXYABLE_DEPENDENCIES, ij); exception.initCause(ue); throw exception; } } if (Reflections.isPrimitive(ij.getType()) && resolvedBean.isNullable()) { throw new NullableDependencyException(INJECTION_POINT_HAS_NULLABLE_DEPENDENCIES, ij); } if (bean != null && Beans.isPassivatingScope(bean, beanManager) && (!ij.isTransient()) && !Beans.isPassivationCapableBean(resolvedBean)) { validateInjectionPointPassivationCapable(ij, resolvedBean, beanManager); } } }