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 import java.lang.reflect.Modifier ; 39 40 43 public class MethodExpr extends Expr { 44 private Expr _expr; 45 private String _methodName; 46 47 private Expr []_args; 48 49 57 public MethodExpr(Expr expr, String methodName, Expr []args) 58 { 59 _expr = expr; 60 _methodName = methodName; 61 _args = args; 62 } 63 64 69 @Override 70 public Object getValue(ELContext env) 71 throws ELException 72 { 73 Object aObj = _expr.getValue(env); 74 75 if (aObj == null) 76 return null; 77 78 Object []objs = new Object [_args.length]; 79 80 try { 81 Method method = findMethod(aObj.getClass()); 82 83 if (method != null) { 84 Class []params = method.getParameterTypes(); 85 86 for (int j = 0; j < params.length; j++) { 87 objs[j] = evalArg(params[j], _args[j], env); 88 } 89 90 try { 92 method.setAccessible(true); 93 } catch (Throwable e) { 94 } 95 96 return method.invoke(aObj, objs); 97 } 98 99 return null; 100 } catch (Exception e) { 101 return invocationError(e); 102 } 103 } 104 105 private Method findMethod(Class type) 106 { 107 if (type == null) 108 return null; 109 110 if (Modifier.isPublic(type.getModifiers())) { 111 Method []methods = type.getDeclaredMethods(); 112 113 for (int i = 0; i < methods.length; i++) { 114 Method method = methods[i]; 115 116 if (! Modifier.isPublic(method.getModifiers())) 117 continue; 118 119 Class []params = method.getParameterTypes(); 120 121 if (method.getName().equals(_methodName) && 122 params.length == _args.length) 123 return method; 124 } 125 } 126 127 Class []interfaces = type.getInterfaces(); 128 for (int i = 0; i < interfaces.length; i++) { 129 Method method = findMethod(interfaces[i]); 130 if (method != null) 131 return method; 132 } 133 134 return findMethod(type.getSuperclass()); 135 } 136 137 static Object evalArg(Class cl, Expr expr, ELContext env) 138 throws ELException 139 { 140 Marshall marshall = Marshall.create(cl); 141 142 return marshall.marshall(expr, env); 143 } 144 145 148 @Override 149 public void printCreate(WriteStream os) 150 throws IOException 151 { 152 os.print("new com.caucho.el.MethodExpr("); 153 _expr.printCreate(os); 154 os.print(", \""); 155 os.print(_methodName); 156 os.print("\", new com.caucho.el.Expr[] {"); 157 158 for (int i = 0; i < _args.length; i++) { 159 if (i != 0) 160 os.print(", "); 161 _args[i].printCreate(os); 162 } 163 os.println("})"); 164 } 165 166 169 public boolean equals(Object o) 170 { 171 if (! (o instanceof MethodExpr)) 172 return false; 173 174 MethodExpr expr = (MethodExpr) o; 175 176 if (! _expr.equals(expr._expr)) 177 return false; 178 179 if (! _methodName.equals(expr._methodName)) 180 return false; 181 182 if (_args.length != expr._args.length) 183 return false; 184 185 for (int i = 0; i < _args.length; i++) { 186 if (! _args[i].equals(expr._args[i])) 187 return false; 188 } 189 190 return true; 191 } 192 193 196 public String toString() 197 { 198 return "MethodExpr[" + _expr + "," + _methodName + "]"; 199 } 200 } 201 | Popular Tags |