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; }
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; } } }
static Class<?> getDeclaringRawType(InjectionPoint injectionPoint) { if (injectionPoint.getBean() != null) { return getRawType(injectionPoint.getBean().getBeanClass()); } else { return getRawType(injectionPoint.getMember().getDeclaringClass()); } }
@Inject public StateBuilder(InjectionPoint ip) { if (ip != null) { this.type = ip.getMember().getDeclaringClass(); INSTANCE = this; } }
@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); }
@Produces @Log public Logger createLogger(InjectionPoint injectionPoint) { Logger logger = null; Class<?> aClass = injectionPoint.getMember().getDeclaringClass(); logger = LogManager.getLogger(aClass.getName()); return logger; }
@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); }
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)); } } } }
@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); }
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; }
@Produces public Logger produceLog(InjectionPoint injectionPoint) { return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); }
public void dispose(@Disposes @Any Bar bar, InjectionPoint injectionPoint) { disposedBar = bar; disposedInjection = injectionPoint.getMember(); }
@Produces public Bar getBar(InjectionPoint injectionPoint) { producedInjection = injectionPoint.getMember(); return new Bar("blah"); }
@Produces public Logger getLogger(InjectionPoint ip) { String category = ip.getMember().getDeclaringClass().getName(); return Logger.getLogger(category); }
/** * 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); } } }
@Produces public Logger expose(InjectionPoint ip) { Class<?> clazz = ip.getMember().getDeclaringClass(); return Logger.getLogger(clazz.getName()); }
@Produces public LogSink produce(InjectionPoint ip) { Class<?> injectionTarget = ip.getMember().getDeclaringClass(); return Logger.getLogger(injectionTarget.getName())::info; }
@Produces public Logger expose(InjectionPoint ip) { String loggerName = ip.getMember().getDeclaringClass().getName(); return Logger.getLogger(loggerName); }