1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.cdi.managedproperty;
21
22 import java.lang.reflect.ParameterizedType;
23 import javax.enterprise.context.Dependent;
24 import javax.enterprise.context.spi.CreationalContext;
25 import javax.enterprise.inject.spi.BeanManager;
26 import javax.enterprise.util.AnnotationLiteral;
27 import javax.faces.FacesException;
28 import javax.faces.annotation.ManagedProperty;
29 import javax.faces.context.FacesContext;
30 import org.apache.myfaces.cdi.util.AbstractDynamicProducer;
31 import org.apache.myfaces.shared.util.ClassUtils;
32
33
34
35
36 public class ManagedPropertyProducer extends AbstractDynamicProducer<Object>
37 {
38 class ManagedPropertyAnnotationLiteral extends AnnotationLiteral<ManagedProperty> implements ManagedProperty
39 {
40 private static final long serialVersionUID = 1L;
41
42 private String value;
43
44 public ManagedPropertyAnnotationLiteral(String value)
45 {
46 this.value = value;
47 }
48
49 @Override
50 public String value()
51 {
52 return value;
53 }
54 }
55
56 public ManagedPropertyProducer(BeanManager beanManager, ManagedPropertyInfo typeInfo)
57 {
58 super(beanManager);
59
60 Class<?> beanClass;
61
62 if (typeInfo.getType() instanceof ParameterizedType)
63 {
64 beanClass = ClassUtils.simpleClassForName(
65 ((ParameterizedType) typeInfo.getType()).getRawType().getTypeName());
66 }
67 else
68 {
69
70 beanClass = ClassUtils.simpleJavaTypeToClass(typeInfo.getType().getTypeName());
71 }
72
73 super.id(typeInfo.getType() + "_" + typeInfo.getExpression())
74 .scope(Dependent.class)
75 .qualifiers(new ManagedPropertyAnnotationLiteral(typeInfo.getExpression()))
76 .types(typeInfo.getType(), Object.class)
77 .beanClass(beanClass)
78 .create(e -> createManagedProperty(e, typeInfo));
79 }
80
81 protected Object createManagedProperty(CreationalContext<Object> cc, ManagedPropertyInfo typeInfo)
82 {
83 FacesContext facesContext = FacesContext.getCurrentInstance();
84 if (facesContext == null)
85 {
86 throw new FacesException("@ManagedProperty(\"" + typeInfo.getExpression()
87 + "\") can only be resolved in a JSF request!");
88 }
89
90 return facesContext.getApplication().evaluateExpressionGet(
91 facesContext, typeInfo.getExpression(), getBeanClass());
92 }
93 }