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 | 0 | { |
49 | 0 | this.methodName = methodName; |
50 | 0 | this.returnTypeClass = returnTypeClass; |
51 | 0 | this.params = params; |
52 | 0 | } |
53 | |
|
54 | |
public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) |
55 | |
{ |
56 | 0 | if (false == name.equals(this.methodName)) |
57 | |
{ |
58 | 0 | return null; |
59 | |
} |
60 | |
|
61 | 0 | if (MethodExpression.class.equals(meta.getPropertyType(name))) |
62 | |
{ |
63 | 0 | Method method = meta.getWriteMethod(name); |
64 | 0 | if (method != null) |
65 | |
{ |
66 | 0 | return new MethodExpressionMetadata(method, attribute, this.returnTypeClass, this.params); |
67 | |
} |
68 | |
} |
69 | |
|
70 | 0 | 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 | 0 | { |
86 | 0 | _method = method; |
87 | 0 | _attribute = attribute; |
88 | 0 | _paramList = paramList; |
89 | 0 | _returnType = returnType; |
90 | 0 | } |
91 | |
|
92 | |
public void applyMetadata(FaceletContext ctx, Object instance) |
93 | |
{ |
94 | 0 | MethodExpression expr = _attribute.getMethodExpression(ctx, _returnType, _paramList); |
95 | |
|
96 | |
try |
97 | |
{ |
98 | 0 | _method.invoke(instance, new Object[] { expr }); |
99 | |
} |
100 | 0 | catch (InvocationTargetException e) |
101 | |
{ |
102 | 0 | throw new TagAttributeException(_attribute, e.getCause()); |
103 | |
} |
104 | 0 | catch (Exception e) |
105 | |
{ |
106 | 0 | throw new TagAttributeException(_attribute, e); |
107 | 0 | } |
108 | 0 | } |
109 | |
} |
110 | |
} |