1 55 56 package org.apache.commons.el; 57 58 import java.util.List ; 59 import java.util.Map ; 60 import java.util.Iterator ; 61 import java.lang.reflect.*; 62 import javax.servlet.jsp.el.ELException ; 63 import javax.servlet.jsp.el.VariableResolver ; 64 import javax.servlet.jsp.el.FunctionMapper ; 65 66 72 73 public class FunctionInvocation 74 extends Expression 75 { 76 81 private String functionName; 82 private List argumentList; 83 public String getFunctionName() { return functionName; } 84 public void setFunctionName(String f) { functionName = f; } 85 public List getArgumentList() { return argumentList; } 86 public void setArgumentList(List l) { argumentList = l; } 87 88 92 public FunctionInvocation (String functionName, List argumentList) 93 { 94 this.functionName = functionName; 95 this.argumentList = argumentList; 96 } 97 98 104 public String getExpressionString () 105 { 106 StringBuffer b = new StringBuffer (); 107 b.append(functionName); 108 b.append("("); 109 Iterator i = argumentList.iterator(); 110 while (i.hasNext()) { 111 b.append(((Expression) i.next()).getExpressionString()); 112 if (i.hasNext()) 113 b.append(", "); 114 } 115 b.append(")"); 116 return b.toString(); 117 } 118 119 120 125 public Object evaluate (VariableResolver pResolver, 126 FunctionMapper functions, 127 Logger pLogger) 128 throws ELException 129 { 130 131 if (functions == null) 133 pLogger.logError(Constants.UNKNOWN_FUNCTION, functionName); 134 135 String prefix = null; 137 String localName = null; 138 int index = functionName.indexOf( ':' ); 139 if (index == -1) { 140 prefix = ""; 141 localName = functionName; 142 } else { 143 prefix = functionName.substring( 0, index ); 144 localName = functionName.substring( index + 1 ); 145 } 146 147 Method target = (Method) functions.resolveFunction(prefix, localName); 149 if (target == null) 150 pLogger.logError(Constants.UNKNOWN_FUNCTION, functionName); 151 152 Class [] params = target.getParameterTypes(); 154 if (params.length != argumentList.size()) 155 pLogger.logError(Constants.INAPPROPRIATE_FUNCTION_ARG_COUNT, 156 functionName, new Integer (params.length), 157 new Integer (argumentList.size())); 158 159 Object [] arguments = new Object [argumentList.size()]; 161 for (int i = 0; i < params.length; i++) { 162 arguments[i] = ((Expression) argumentList.get(i)).evaluate(pResolver, 164 functions, 165 pLogger); 166 arguments[i] = Coercions.coerce(arguments[i], params[i], pLogger); 168 } 169 170 try { 172 return (target.invoke(null, arguments)); 173 } catch (InvocationTargetException ex) { 174 pLogger.logError(Constants.FUNCTION_INVOCATION_ERROR, 175 ex.getTargetException(), 176 functionName); 177 return null; 178 } catch (Exception ex) { 179 pLogger.logError(Constants.FUNCTION_INVOCATION_ERROR, ex, functionName); 180 return null; 181 } 182 } 183 184 } 186 | Popular Tags |