static Class<?> getDeclaringRawType(InjectionPoint injectionPoint) { if (injectionPoint.getBean() != null) { return getRawType(injectionPoint.getBean().getBeanClass()); } else { return getRawType(injectionPoint.getMember().getDeclaringClass()); } }
private void validateInjectionPoints(Set<InjectionPoint> injectionPoints) { assertEquals(2, injectionPoints.size()); for (InjectionPoint ip : injectionPoints) { AnnotatedParameter<Factory> parameter = this.<AnnotatedParameter<Factory>>cast(ip.getAnnotated()); if (parameter.getPosition() == 0) { // BeanManager assertEquals(BeanManager.class, parameter.getBaseType()); } else if (parameter.getPosition() == 1) { // SpaceSuit<Toy> Type baseType = parameter.getBaseType(); if (baseType instanceof ParameterizedType && ((ParameterizedType) baseType).getRawType() instanceof Class<?>) { assertEquals(((ParameterizedType) baseType).getRawType(), SpaceSuit.class); } else { fail(); } } else { fail("Unexpected injection point " + ip); } assertFalse(ip.isDelegate()); assertFalse(ip.isTransient()); assertNull(ip.getBean()); } }
private void validateDecorators(BeanManagerImpl beanManager, AbstractClassBean<?> classBean) { if (classBean.getDecorators().size() > 0) { boolean passivationCapabilityCheckRequired = isPassivationCapabilityCheckRequired(beanManager, classBean); for (Decorator<?> decorator : classBean.getDecorators()) { if (passivationCapabilityCheckRequired) { boolean isSerializable = (decorator instanceof WeldDecorator<?>) ? (((WeldDecorator<?>) decorator).getWeldAnnotated().isSerializable()) : (decorator instanceof PassivationCapable); if (!isSerializable) { throw new UnserializableDependencyException( PASSIVATING_BEAN_WITH_NONSERIALIZABLE_DECORATOR, classBean, decorator); } } for (InjectionPoint ij : decorator.getInjectionPoints()) { if (!ij.isDelegate()) { Bean<?> resolvedBean = beanManager.resolve(beanManager.getBeans(ij)); validateInjectionPoint(ij, beanManager); if (passivationCapabilityCheckRequired) { validateInjectionPointPassivationCapable(ij, resolvedBean, beanManager); } } } } } }
@Produces @RequestParam public String produce(InjectionPoint ip) { RequestParam requestParam = ip.getAnnotated().getAnnotation(RequestParam.class); String paramName = requestParam.value().equals("") ? ip.getMember().getName() : requestParam.value(); return req.getParameter(paramName); }
public static Object[] generateProxyDelegate( InjectManager manager, List<Decorator<?>> beans, Object delegateProxy, CreationalContextImpl<?> parentEnv) { Object[] instances = new Object[beans.size()]; DependentCreationalContext<Object> proxyEnv = new DependentCreationalContext<Object>(DelegateProxyBean.BEAN, parentEnv, null); proxyEnv.push(delegateProxy); for (int i = 0; i < beans.size(); i++) { Decorator<?> bean = beans.get(i); CreationalContextImpl<?> env = new DependentCreationalContext(bean, proxyEnv, null); Object instance = manager.getReference(bean, bean.getBeanClass(), env); // XXX: InjectionPoint ip = getDelegate(bean); if (ip.getMember() instanceof Field) { Field field = (Field) ip.getMember(); field.setAccessible(true); try { field.set(instance, delegateProxy); } catch (Exception e) { throw new InjectionException(e); } } else if (ip.getMember() instanceof Method) { Method method = (Method) ip.getMember(); method.setAccessible(true); try { method.invoke(instance, delegateProxy); } catch (Exception e) { throw new InjectionException(e); } } /* DecoratorBean<?> decoratorBean = (DecoratorBean<?>) bean; decoratorBean.setDelegate(instance, proxy); */ instances[beans.size() - i - 1] = instance; if (parentEnv instanceof CreationalContextImpl<?>) { // InjectionPoint ip = decoratorBean.getDelegateInjectionPoint(); ((CreationalContextImpl<?>) parentEnv).setInjectionPoint(ip); } } return instances; }
private static Annotation[] getFacadeEventQualifiers(InjectionPoint injectionPoint) { if (!injectionPoint.getAnnotated().isAnnotationPresent(Default.class)) { Set<Annotation> qualifers = new HashSet<Annotation>(injectionPoint.getQualifiers()); qualifers.remove(DefaultLiteral.INSTANCE); return qualifers.toArray(EMPTY_ANNOTATIONS); } else { return injectionPoint.getQualifiers().toArray(EMPTY_ANNOTATIONS); } }
private static InjectionPoint getDelegate(Decorator<?> bean) { if (bean instanceof DecoratorBean) return ((DecoratorBean) bean).getDelegateInjectionPoint(); for (InjectionPoint ip : bean.getInjectionPoints()) { if (ip.isDelegate()) return ip; } throw new IllegalStateException(String.valueOf(bean)); }
public void validateInjectionPointPassivationCapable( InjectionPoint ij, Bean<?> resolvedBean, BeanManagerImpl beanManager) { if (!ij.isTransient() && !Beans.isPassivationCapableDependency(resolvedBean)) { if (resolvedBean.getScope().equals(Dependent.class) && resolvedBean instanceof AbstractProducerBean<?, ?, ?>) { throw new IllegalProductException( NON_SERIALIZABLE_BEAN_INJECTED_INTO_PASSIVATING_BEAN, ij.getBean(), resolvedBean); } throw new UnserializableDependencyException( INJECTION_POINT_HAS_NON_SERIALIZABLE_DEPENDENCY, ij.getBean(), resolvedBean); } }
private static boolean isInjectionPointSatisfied( InjectionPoint ij, Set<?> resolvedBeans, BeanManagerImpl beanManager) { if (ij.getBean() instanceof Decorator<?>) { if (beanManager.getEnabled().getDecorator(ij.getBean().getBeanClass()) != null) { return resolvedBeans.size() > 0; } else { return true; } } else { return resolvedBeans.size() > 0; } }
@Produces public Logger produceLogger(InjectionPoint injectionPoint) { Bean<?> bean = injectionPoint.getBean(); Class<?> clazz; if (bean == null) { clazz = injectionPoint.getMember().getDeclaringClass(); } else { clazz = bean.getBeanClass(); } return produceLogger(clazz); }
public void validateProducer(Producer<?> producer) { if (container.getState().equals(ContainerState.VALIDATED) || container.getState().equals(ContainerState.INITIALIZED)) { // We are past the bootstrap and therefore we can validate the producer immediately validator.validateProducer(producer, beanManager); } else { // Validate injection points for definition errors now for (InjectionPoint ip : producer.getInjectionPoints()) { validator.validateInjectionPointForDefinitionErrors(ip, ip.getBean(), beanManager); validator.validateEventMetadataInjectionPoint(ip); } // Schedule validation for deployment problems to be done later producersToValidate.add(producer); } }
@Produces Logger produceLog(InjectionPoint injectionPoint) { Annotated annotated = injectionPoint.getAnnotated(); if (annotated.isAnnotationPresent(Category.class)) { if (annotated.isAnnotationPresent(Suffix.class)) { return getLogger( annotated.getAnnotation(Category.class).value(), annotated.getAnnotation(Suffix.class).value()); } else { return getLogger(annotated.getAnnotation(Category.class).value()); } } else if (annotated.isAnnotationPresent(TypedCategory.class)) { if (annotated.isAnnotationPresent(Suffix.class)) { return getLogger( annotated.getAnnotation(TypedCategory.class).value(), annotated.getAnnotation(Suffix.class).value()); } else { return getLogger(annotated.getAnnotation(TypedCategory.class).value()); } } else { if (annotated.isAnnotationPresent(Suffix.class)) { return getLogger( getDeclaringRawType(injectionPoint), annotated.getAnnotation(Suffix.class).value()); } else { return getLogger(getDeclaringRawType(injectionPoint)); } } }
@Produces protected OAuthSession resolveSession( InjectionPoint ip, @Current UserSessionRepository repository) { if (ip == null) return repository.getCurrent(); Set<Annotation> quals = ip.getQualifiers(); OAuthSession res; Annotation service = null; boolean iscurrent = false; for (Annotation qual : quals) { if (qual.annotationType().isAnnotationPresent(ProviderRelated.class)) { if (service != null) throw new AgoravaException( "There's more thant one provider related qualifier aon Injection Point" + ip); service = qual; } if (qual.annotationType().equals(Current.class)) iscurrent = true; } if (iscurrent) { if (service != null) { if (!service.equals(repository.getCurrent().getServiceQualifier())) { repository.setCurrent( new OAuthSession.Builder().qualifier(service).repo(repository).build()); } } return repository.getCurrent(); } throw new UnsupportedOperationException( "Cannot inject session whitout Current Qualifier in " + ip); }
public static Annotated getResourceAnnotated(InjectionPoint injectionPoint) { if (injectionPoint instanceof ParameterInjectionPoint) { return ((ParameterInjectionPoint<?, ?>) injectionPoint).getAnnotated().getDeclaringCallable(); } return injectionPoint.getAnnotated(); }
private void checkScopeAnnotations(InjectionPoint ij, MetaAnnotationStore metaAnnotationStore) { for (Annotation annotation : ij.getAnnotated().getAnnotations()) { if (hasScopeMetaAnnotation(annotation)) { log.warn(SCOPE_ANNOTATION_ON_INJECTION_POINT, annotation, ij); } } }
@Inject public StateBuilder(InjectionPoint ip) { if (ip != null) { this.type = ip.getMember().getDeclaringClass(); INSTANCE = this; } }
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(BeanLogger.LOG.typeParameterMustBeConcrete(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); } }
/** * Returns the qualifier annotation of the given qualifier class from the given injection point. */ public static <A extends Annotation> A getQualifier( InjectionPoint injectionPoint, Class<A> qualifierClass) { for (Annotation annotation : injectionPoint.getQualifiers()) { if (qualifierClass.isAssignableFrom(annotation.getClass())) { return qualifierClass.cast(annotation); } } return null; }
@Produces @Log public Logger createLogger(InjectionPoint injectionPoint) { Logger logger = null; Class<?> aClass = injectionPoint.getMember().getDeclaringClass(); logger = LogManager.getLogger(aClass.getName()); return logger; }
/** * @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; }
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; } } }
/** * @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; }
<T> void saveRemoteInjectionPoints( @Observes ProcessInjectionTarget<T> event, BeanManager beanManager) { final InjectionTarget<T> injectionTarget = event.getInjectionTarget(); for (InjectionPoint injectionPoint : injectionTarget.getInjectionPoints()) { final Annotated annotated = injectionPoint.getAnnotated(); final Type type = annotated.getBaseType(); final Class<?> rawType = Reflections.getRawType(annotated.getBaseType()); final Set<Annotation> qualifiers = Reflections.getQualifiers(beanManager, annotated.getAnnotations()); if (rawType.equals(RemoteCache.class) && qualifiers.isEmpty()) { qualifiers.add(new AnnotationLiteral<Default>() {}); addRemoteCacheInjectionPoint(type, qualifiers); } else if (!annotated.isAnnotationPresent(Remote.class) && Reflections.getMetaAnnotation(annotated, Remote.class) != null && rawType.isAssignableFrom(RemoteCache.class)) { addRemoteCacheInjectionPoint(type, qualifiers); } } }
/** * Produces an instance of a slf4j logger for the given injection point. * * @param injectionPoint to use * @return a logger */ @Produces public Logger getLogger(final InjectionPoint injectionPoint) { // The injection point is used to instantiate the correct logger for the // caller class. Bean<?> bean = injectionPoint.getBean(); Logger l = null; if (bean != null) { Class<?> beanClass = bean.getBeanClass(); l = LoggerFactory.getLogger(beanClass); } else { l = LoggerFactory.getLogger("Default logger"); } return l; }
/** * 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); } } } } } }
@Produces public Configuration getConfiguration(final InjectionPoint injectionPoint) { Context context; String defaultName; // try CDI bean name defaultName = injectionPoint.getBean() != null ? injectionPoint.getBean().getName() : null; // try field / method name if (StringHelper.isEmpty(defaultName)) { defaultName = injectionPoint.getMember().getName(); } // context needed annotation context = injectionPoint.getAnnotated().getAnnotation(Context.class); // if no @Context annotation is present, create a default one with no name if (context == null) { context = ContextHelper.createContext(defaultName); } return provides(context, defaultName, Configuration.class); }
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)); } } } }
public static String getEjbBindLocation(InjectionPoint injectionPoint) { EJB ejb = getResourceAnnotated(injectionPoint).getAnnotation(EJB.class); String mappedName = ejb.mappedName(); if (!mappedName.equals("")) { return mappedName; } String name = ejb.name(); if (!name.equals("")) { return RESOURCE_LOOKUP_PREFIX + "/" + name; } String propertyName; if (injectionPoint.getMember() instanceof Field) { propertyName = injectionPoint.getMember().getName(); } else if (injectionPoint.getMember() instanceof Method) { propertyName = getPropertyName((Method) injectionPoint.getMember()); if (propertyName == null) { throw WeldMessages.MESSAGES.injectionPointNotAJavabean((Method) injectionPoint.getMember()); } } else { throw WeldMessages.MESSAGES.cannotInject(injectionPoint); } String className = injectionPoint.getMember().getDeclaringClass().getName(); return RESOURCE_LOOKUP_PREFIX + "/" + className + "/" + propertyName; }