KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > MethodExpressionLiteral


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */
package com.sun.el;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28
29 import javax.el.ELContext;
30 import javax.el.ELException;
31 import javax.el.MethodExpression;
32 import javax.el.MethodInfo;
33
34 import com.sun.el.lang.ELSupport;
35 import com.sun.el.util.ReflectionUtil;
36
37 public class MethodExpressionLiteral extends MethodExpression implements Externalizable JavaDoc {
38
39     private Class JavaDoc expectedType;
40
41     private String JavaDoc expr;
42     
43     private Class JavaDoc[] paramTypes;
44     
45     public MethodExpressionLiteral() {
46         // do nothing
47
}
48     
49     public MethodExpressionLiteral(String JavaDoc expr, Class JavaDoc expectedType, Class JavaDoc[] paramTypes) {
50         this.expr = expr;
51         this.expectedType = expectedType;
52         this.paramTypes = paramTypes;
53     }
54
55     public MethodInfo getMethodInfo(ELContext context) throws ELException {
56         return new MethodInfo(this.expr, this.expectedType, this.paramTypes);
57     }
58
59     public Object JavaDoc invoke(ELContext context, Object JavaDoc[] params) throws ELException {
60         if (this.expectedType != null) {
61             return ELSupport.coerceToType(this.expr, this.expectedType);
62         } else {
63             return this.expr;
64         }
65     }
66
67     public String JavaDoc getExpressionString() {
68         return this.expr;
69     }
70
71     public boolean equals(Object JavaDoc obj) {
72         return (obj instanceof MethodExpressionLiteral && this.hashCode() == obj.hashCode());
73     }
74
75     public int hashCode() {
76         return this.expr.hashCode();
77     }
78
79     public boolean isLiteralText() {
80         return true;
81     }
82
83     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
84         this.expr = in.readUTF();
85         String JavaDoc type = in.readUTF();
86         if (!"".equals(type)) {
87             this.expectedType = ReflectionUtil.forName(type);
88         }
89         this.paramTypes = ReflectionUtil.toTypeArray(((String JavaDoc[]) in
90                 .readObject()));
91     }
92
93     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
94         out.writeUTF(this.expr);
95         out.writeUTF((this.expectedType != null) ? this.expectedType.getName()
96                 : "");
97         out.writeObject(ReflectionUtil.toTypeNameArray(this.paramTypes));
98     }
99 }
100
Popular Tags