001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.el;
019:
020: import java.io.Externalizable;
021: import java.io.IOException;
022: import java.io.ObjectInput;
023: import java.io.ObjectOutput;
024:
025: import javax.el.ELContext;
026: import javax.el.ELException;
027: import javax.el.MethodExpression;
028: import javax.el.MethodInfo;
029:
030: import org.apache.el.lang.ELSupport;
031: import org.apache.el.util.ReflectionUtil;
032:
033: public class MethodExpressionLiteral extends MethodExpression implements
034: Externalizable {
035:
036: private Class expectedType;
037:
038: private String expr;
039:
040: private Class[] paramTypes;
041:
042: public MethodExpressionLiteral() {
043: // do nothing
044: }
045:
046: public MethodExpressionLiteral(String expr, Class expectedType,
047: Class[] paramTypes) {
048: this .expr = expr;
049: this .expectedType = expectedType;
050: this .paramTypes = paramTypes;
051: }
052:
053: public MethodInfo getMethodInfo(ELContext context)
054: throws ELException {
055: return new MethodInfo(this .expr, this .expectedType,
056: this .paramTypes);
057: }
058:
059: public Object invoke(ELContext context, Object[] params)
060: throws ELException {
061: if (this .expectedType != null) {
062: return ELSupport.coerceToType(this .expr, this .expectedType);
063: } else {
064: return this .expr;
065: }
066: }
067:
068: public String getExpressionString() {
069: return this .expr;
070: }
071:
072: public boolean equals(Object obj) {
073: return (obj instanceof MethodExpressionLiteral && this
074: .hashCode() == obj.hashCode());
075: }
076:
077: public int hashCode() {
078: return this .expr.hashCode();
079: }
080:
081: public boolean isLiteralText() {
082: return true;
083: }
084:
085: public void readExternal(ObjectInput in) throws IOException,
086: ClassNotFoundException {
087: this .expr = in.readUTF();
088: String type = in.readUTF();
089: if (!"".equals(type)) {
090: this .expectedType = ReflectionUtil.forName(type);
091: }
092: this .paramTypes = ReflectionUtil.toTypeArray(((String[]) in
093: .readObject()));
094: }
095:
096: public void writeExternal(ObjectOutput out) throws IOException {
097: out.writeUTF(this .expr);
098: out.writeUTF((this .expectedType != null) ? this .expectedType
099: .getName() : "");
100: out
101: .writeObject(ReflectionUtil
102: .toTypeNameArray(this.paramTypes));
103: }
104: }
|