1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23
24 import javax.el.MethodExpression;
25 import javax.faces.view.facelets.FaceletContext;
26 import javax.faces.view.facelets.MetaRule;
27 import javax.faces.view.facelets.Metadata;
28 import javax.faces.view.facelets.MetadataTarget;
29 import javax.faces.view.facelets.TagAttribute;
30 import javax.faces.view.facelets.TagAttributeException;
31
32
33
34
35
36
37
38 public final class MethodRule extends MetaRule
39 {
40
41 private final String methodName;
42
43 private final Class<?> returnTypeClass;
44
45 private final Class<?>[] params;
46
47 public MethodRule(String methodName, Class<?> returnTypeClass, Class<?>[] params)
48 {
49 this.methodName = methodName;
50 this.returnTypeClass = returnTypeClass;
51 this.params = params;
52 }
53
54 public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
55 {
56 if (false == name.equals(this.methodName))
57 {
58 return null;
59 }
60
61 if (MethodExpression.class.equals(meta.getPropertyType(name)))
62 {
63 Method method = meta.getWriteMethod(name);
64 if (method != null)
65 {
66 return new MethodExpressionMetadata(method, attribute, this.returnTypeClass, this.params);
67 }
68 }
69
70 return null;
71 }
72
73 private class MethodExpressionMetadata extends Metadata
74 {
75 private final Method _method;
76
77 private final TagAttribute _attribute;
78
79 private Class<?>[] _paramList;
80
81 private Class<?> _returnType;
82
83 public MethodExpressionMetadata(Method method, TagAttribute attribute, Class<?> returnType,
84 Class<?>[] paramList)
85 {
86 _method = method;
87 _attribute = attribute;
88 _paramList = paramList;
89 _returnType = returnType;
90 }
91
92 public void applyMetadata(FaceletContext ctx, Object instance)
93 {
94 MethodExpression expr = _attribute.getMethodExpression(ctx, _returnType, _paramList);
95
96 try
97 {
98 _method.invoke(instance, new Object[] { expr });
99 }
100 catch (InvocationTargetException e)
101 {
102 throw new TagAttributeException(_attribute, e.getCause());
103 }
104 catch (Exception e)
105 {
106 throw new TagAttributeException(_attribute, e);
107 }
108 }
109 }
110 }