1 2 3 package org.apache.el.parser; 4 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 8 import javax.el.ELException; 9 import javax.el.FunctionMapper; 10 11 import org.apache.el.lang.EvaluationContext; 12 import org.apache.el.util.MessageFactory; 13 14 15 19 public final class AstFunction extends SimpleNode { 20 21 protected String localName = ""; 22 23 protected String prefix = ""; 24 25 public AstFunction(int id) { 26 super(id); 27 } 28 29 public String getLocalName() { 30 return localName; 31 } 32 33 public String getOutputName() { 34 if (this.prefix == null) { 35 return this.localName; 36 } else { 37 return this.prefix + ":" + this.localName; 38 } 39 } 40 41 public String getPrefix() { 42 return prefix; 43 } 44 45 public Class getType(EvaluationContext ctx) 46 throws ELException { 47 48 FunctionMapper fnMapper = ctx.getFunctionMapper(); 49 50 if (fnMapper == null) { 52 throw new ELException(MessageFactory.get("error.fnMapper.null")); 53 } 54 Method m = fnMapper.resolveFunction(this.prefix, this.localName); 55 if (m == null) { 56 throw new ELException(MessageFactory.get("error.fnMapper.method", 57 this.getOutputName())); 58 } 59 return m.getReturnType(); 60 } 61 62 public Object getValue(EvaluationContext ctx) 63 throws ELException { 64 65 FunctionMapper fnMapper = ctx.getFunctionMapper(); 66 67 if (fnMapper == null) { 69 throw new ELException(MessageFactory.get("error.fnMapper.null")); 70 } 71 Method m = fnMapper.resolveFunction(this.prefix, this.localName); 72 if (m == null) { 73 throw new ELException(MessageFactory.get("error.fnMapper.method", 74 this.getOutputName())); 75 } 76 77 Class [] paramTypes = m.getParameterTypes(); 78 Object [] params = null; 79 Object result = null; 80 int numParams = this.jjtGetNumChildren(); 81 if (numParams > 0) { 82 params = new Object [numParams]; 83 try { 84 for (int i = 0; i < numParams; i++) { 85 params[i] = this.children[i].getValue(ctx); 86 params[i] = coerceToType(params[i], paramTypes[i]); 87 } 88 } catch (ELException ele) { 89 throw new ELException(MessageFactory.get("error.function", this 90 .getOutputName()), ele); 91 } 92 } 93 try { 94 result = m.invoke(null, params); 95 } catch (IllegalAccessException iae) { 96 throw new ELException(MessageFactory.get("error.function", this 97 .getOutputName()), iae); 98 } catch (InvocationTargetException ite) { 99 throw new ELException(MessageFactory.get("error.function", this 100 .getOutputName()), ite.getCause()); 101 } 102 return result; 103 } 104 105 public void setLocalName(String localName) { 106 this.localName = localName; 107 } 108 109 public void setPrefix(String prefix) { 110 this.prefix = prefix; 111 } 112 113 114 public String toString() 115 { 116 return ELParserTreeConstants.jjtNodeName[id] + "[" + this.getOutputName() + "]"; 117 } 118 } 119 | Popular Tags |