1 29 30 package com.caucho.el; 31 32 import com.caucho.config.types.Signature; 33 import com.caucho.vfs.WriteStream; 34 35 import javax.el.ELContext; 36 import javax.el.ELException; 37 import java.io.IOException ; 38 import java.lang.reflect.Method ; 39 import java.util.logging.Level ; 40 41 44 public class StaticMethodExpr extends Expr { 45 private Method _method; 46 private Marshall []_marshall; 47 private boolean _isVoid; 48 49 55 public StaticMethodExpr(Method method) 56 { 57 _method = method; 58 59 initMethod(); 60 } 61 62 67 public StaticMethodExpr(String signature) 68 { 69 try { 70 Signature sig = new Signature(); 71 sig.addText(signature); 72 sig.init(); 73 74 _method = sig.getMethod(); 75 } catch (Exception e) { 76 log.log(Level.FINE, e.toString(), e); 77 } 78 79 initMethod(); 80 } 81 82 85 private void initMethod() 86 { 87 Class []param = _method.getParameterTypes(); 88 89 _marshall = new Marshall[param.length]; 90 91 for (int i = 0; i < _marshall.length; i++) { 92 _marshall[i] = Marshall.create(param[i]); 93 } 94 95 _isVoid = void.class.equals(_method.getReturnType()); 96 } 97 98 103 @Override 104 public Object getValue(ELContext env) 105 throws ELException 106 { 107 return _method; 108 } 109 110 115 public Object evalMethod(Expr []args, 116 ELContext env) 117 throws ELException 118 { 119 if (_marshall.length != args.length) { 120 throw new ELParseException(L.l("Arguments to '{0}' do not match expected length {1}.", _method.getName(), _marshall.length)); 122 } 123 124 try { 125 Object []objs = new Object [args.length]; 126 127 for (int i = 0; i < _marshall.length; i++) 128 objs[i] = _marshall[i].marshall(args[i], env); 129 130 if (! _isVoid) 131 return _method.invoke(null, objs); 132 else { 133 _method.invoke(null, objs); 134 135 return null; 136 } 137 } catch (ELException e) { 138 throw e; 139 } catch (Exception e) { 140 throw new ELException(e); 141 } 142 } 143 144 147 public void printCreate(WriteStream os) 148 throws IOException 149 { 150 os.print("new com.caucho.el.StaticMethodExpr(\""); 151 printType(os, _method.getReturnType()); 152 os.print(" "); 153 os.print(_method.getDeclaringClass().getName()); 154 os.print("."); 155 os.print(_method.getName()); 156 os.print("("); 157 Class []parameterTypes = _method.getParameterTypes(); 158 159 for (int i = 0; i < parameterTypes.length; i++) { 160 if (i != 0) 161 os.print(", "); 162 printType(os, parameterTypes[i]); 163 } 164 os.print(")"); 165 os.print("\")"); 166 } 167 168 private void printType(WriteStream os, Class cl) 169 throws IOException 170 { 171 if (cl.isArray()) { 172 printType(os, cl.getComponentType()); 173 os.print("[]"); 174 } 175 else 176 os.print(cl.getName()); 177 } 178 179 182 public boolean equals(Object o) 183 { 184 if (! (o instanceof StaticMethodExpr)) 185 return false; 186 187 StaticMethodExpr expr = (StaticMethodExpr) o; 188 189 return _method.equals(expr._method); 190 } 191 192 public String toString() 193 { 194 return _method.getName(); 195 } 196 } 197 | Popular Tags |