KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > MethodExpressionLiteral


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.el;
19
20 import java.io.Externalizable JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInput JavaDoc;
23 import java.io.ObjectOutput JavaDoc;
24
25 import javax.el.ELContext;
26 import javax.el.ELException;
27 import javax.el.MethodExpression;
28 import javax.el.MethodInfo;
29
30 import org.apache.el.lang.ELSupport;
31 import org.apache.el.util.ReflectionUtil;
32
33
34 public class MethodExpressionLiteral extends MethodExpression implements Externalizable JavaDoc {
35
36     private Class JavaDoc expectedType;
37
38     private String JavaDoc expr;
39     
40     private Class JavaDoc[] paramTypes;
41     
42     public MethodExpressionLiteral() {
43         // do nothing
44
}
45     
46     public MethodExpressionLiteral(String JavaDoc expr, Class JavaDoc expectedType, Class JavaDoc[] paramTypes) {
47         this.expr = expr;
48         this.expectedType = expectedType;
49         this.paramTypes = paramTypes;
50     }
51
52     public MethodInfo getMethodInfo(ELContext context) throws ELException {
53         return new MethodInfo(this.expr, this.expectedType, this.paramTypes);
54     }
55
56     public Object JavaDoc invoke(ELContext context, Object JavaDoc[] params) throws ELException {
57         if (this.expectedType != null) {
58             return ELSupport.coerceToType(this.expr, this.expectedType);
59         } else {
60             return this.expr;
61         }
62     }
63
64     public String JavaDoc getExpressionString() {
65         return this.expr;
66     }
67
68     public boolean equals(Object JavaDoc obj) {
69         return (obj instanceof MethodExpressionLiteral && this.hashCode() == obj.hashCode());
70     }
71
72     public int hashCode() {
73         return this.expr.hashCode();
74     }
75
76     public boolean isLiteralText() {
77         return true;
78     }
79
80     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
81         this.expr = in.readUTF();
82         String JavaDoc type = in.readUTF();
83         if (!"".equals(type)) {
84             this.expectedType = ReflectionUtil.forName(type);
85         }
86         this.paramTypes = ReflectionUtil.toTypeArray(((String JavaDoc[]) in
87                 .readObject()));
88     }
89
90     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
91         out.writeUTF(this.expr);
92         out.writeUTF((this.expectedType != null) ? this.expectedType.getName()
93                 : "");
94         out.writeObject(ReflectionUtil.toTypeNameArray(this.paramTypes));
95     }
96 }
97
Popular Tags