KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > MethodRule


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14 package com.sun.facelets.tag;
15
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18
19 import javax.el.MethodExpression;
20 import javax.faces.el.MethodBinding;
21
22 import com.sun.facelets.FaceletContext;
23 import com.sun.facelets.el.LegacyMethodBinding;
24
25 /**
26  * Optional Rule for binding Method[Binding|Expression] properties
27  *
28  * @author Mike Kienenberger
29  * @author Jacob Hookom
30  */

31 public final class MethodRule extends MetaRule {
32
33     private final String JavaDoc methodName;
34
35     private final Class JavaDoc returnTypeClass;
36
37     private final Class JavaDoc[] params;
38
39     public MethodRule(String JavaDoc methodName, Class JavaDoc returnTypeClass, Class JavaDoc[] params) {
40         this.methodName = methodName;
41         this.returnTypeClass = returnTypeClass;
42         this.params = params;
43     }
44
45     public Metadata applyRule(String JavaDoc name, TagAttribute attribute,
46             MetadataTarget meta) {
47         if (false == name.equals(this.methodName))
48             return null;
49
50         if (MethodBinding.class.equals(meta.getPropertyType(name))) {
51             Method JavaDoc method = meta.getWriteMethod(name);
52             if (method != null) {
53                 return new MethodBindingMetadata(method, attribute,
54                         this.returnTypeClass, this.params);
55             }
56         } else if (MethodExpression.class.equals(meta.getPropertyType(name))) {
57             Method JavaDoc method = meta.getWriteMethod(name);
58             if (method != null) {
59                 return new MethodExpressionMetadata(method, attribute,
60                         this.returnTypeClass, this.params);
61             }
62         }
63
64         return null;
65     }
66
67     private class MethodBindingMetadata extends Metadata {
68         private final Method JavaDoc _method;
69
70         private final TagAttribute _attribute;
71
72         private Class JavaDoc[] _paramList;
73
74         private Class JavaDoc _returnType;
75
76         public MethodBindingMetadata(Method JavaDoc method, TagAttribute attribute,
77                 Class JavaDoc returnType, Class JavaDoc[] paramList) {
78             _method = method;
79             _attribute = attribute;
80             _paramList = paramList;
81             _returnType = returnType;
82         }
83
84         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
85             MethodExpression expr = _attribute.getMethodExpression(ctx,
86                     _returnType, _paramList);
87
88             try {
89                 _method.invoke(instance,
90                         new Object JavaDoc[] { new LegacyMethodBinding(expr) });
91             } catch (InvocationTargetException JavaDoc e) {
92                 throw new TagAttributeException(_attribute, e.getCause());
93             } catch (Exception JavaDoc e) {
94                 throw new TagAttributeException(_attribute, e);
95             }
96         }
97     }
98
99     private class MethodExpressionMetadata extends Metadata {
100         private final Method JavaDoc _method;
101
102         private final TagAttribute _attribute;
103
104         private Class JavaDoc[] _paramList;
105
106         private Class JavaDoc _returnType;
107
108         public MethodExpressionMetadata(Method JavaDoc method, TagAttribute attribute,
109                 Class JavaDoc returnType, Class JavaDoc[] paramList) {
110             _method = method;
111             _attribute = attribute;
112             _paramList = paramList;
113             _returnType = returnType;
114         }
115
116         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
117             MethodExpression expr = _attribute.getMethodExpression(ctx,
118                     _returnType, _paramList);
119
120             try {
121                 _method.invoke(instance, new Object JavaDoc[] { expr });
122             } catch (InvocationTargetException JavaDoc e) {
123                 throw new TagAttributeException(_attribute, e.getCause());
124             } catch (Exception JavaDoc e) {
125                 throw new TagAttributeException(_attribute, e);
126             }
127         }
128     }
129 }
130
Popular Tags