1 29 30 package com.caucho.el; 31 32 import com.caucho.vfs.WriteStream; 33 34 import javax.el.ELContext; 35 import javax.el.ELException; 36 import java.io.IOException ; 37 import java.lang.reflect.Method ; 38 39 42 public class FunctionExpr extends Expr { 43 private Expr _expr; 44 45 private Expr []_args; 46 47 53 public FunctionExpr(Expr expr, Expr []args) 54 { 55 _expr = expr; 56 _args = args; 57 } 58 59 64 @Override 65 public Object getValue(ELContext env) 66 throws ELException 67 { 68 if (_expr instanceof StaticMethodExpr) 69 return ((StaticMethodExpr) _expr).evalMethod(_args, env); 70 71 Object aObj = _expr.getValue(env); 72 73 Method method = null; 74 75 if (aObj instanceof Method ) 76 method = (Method ) aObj; 77 78 else if (aObj instanceof Method []) { 79 Method []methods = (Method []) aObj; 80 81 if (methods.length < _args.length) 82 return null; 83 84 method = methods[_args.length]; 85 } 86 87 if (method == null) { 88 throw new ELParseException(L.l("'{0}' is an unknown function.", _expr)); 90 } 91 92 Class []params = method.getParameterTypes(); 93 94 if (params.length != _args.length) { 95 throw new ELParseException(L.l("arguments '{0}' do not match expected length {1}.", _expr, params.length)); 97 } 98 99 try { 100 Object []objs = new Object [_args.length]; 101 102 for (int i = 0; i < params.length; i++) 103 objs[i] = MethodExpr.evalArg(params[i], _args[i], env); 104 105 return method.invoke(null, objs); 106 } catch (ELException e) { 107 throw e; 108 } catch (Exception e) { 109 throw new ELException(e); 110 } 111 } 112 113 116 @Override 117 public void printCreate(WriteStream os) 118 throws IOException 119 { 120 os.print("new com.caucho.el.FunctionExpr("); 121 _expr.printCreate(os); 122 os.print(", new com.caucho.el.Expr[] {"); 123 124 for (int i = 0; i < _args.length; i++) { 125 if (i != 0) 126 os.print(", "); 127 _args[i].printCreate(os); 128 } 129 os.println("})"); 130 } 131 132 135 public boolean equals(Object o) 136 { 137 if (! (o instanceof FunctionExpr)) 138 return false; 139 140 FunctionExpr expr = (FunctionExpr) o; 141 142 if (! _expr.equals(expr._expr)) 143 return false; 144 145 if (_args.length != expr._args.length) 146 return false; 147 148 for (int i = 0; i < _args.length; i++) { 149 if (! _args[i].equals(expr._args[i])) 150 return false; 151 } 152 153 return true; 154 } 155 156 public String toString() 157 { 158 StringBuilder sb = new StringBuilder (); 159 160 sb.append(_expr); 161 sb.append('('); 162 for (int i = 0; i < _args.length; i++) { 163 if (i != 0) 164 sb.append(", "); 165 166 sb.append(_args[i]); 167 } 168 sb.append(')'); 169 170 return sb.toString(); 171 } 172 } 173 | Popular Tags |