1 package org.apache.velocity.runtime.parser.node; 2 3 18 19 import org.apache.velocity.context.InternalContextAdapter; 20 import org.apache.velocity.runtime.parser.*; 21 import org.apache.velocity.util.introspection.IntrospectionCacheData; 22 import org.apache.velocity.util.introspection.VelMethod; 23 import org.apache.velocity.util.introspection.Info; 24 25 import org.apache.velocity.exception.MethodInvocationException; 26 import java.lang.reflect.InvocationTargetException ; 27 28 import org.apache.velocity.app.event.EventCartridge; 29 30 46 public class ASTMethod extends SimpleNode 47 { 48 private String methodName = ""; 49 private int paramCount = 0; 50 51 public ASTMethod(int id) 52 { 53 super(id); 54 } 55 56 public ASTMethod(Parser p, int id) 57 { 58 super(p, id); 59 } 60 61 62 public Object jjtAccept(ParserVisitor visitor, Object data) 63 { 64 return visitor.visit(this, data); 65 } 66 67 71 public Object init( InternalContextAdapter context, Object data) 72 throws Exception 73 { 74 super.init( context, data ); 75 76 79 80 methodName = getFirstToken().image; 81 paramCount = jjtGetNumChildren() - 1; 82 83 return data; 84 } 85 86 91 public Object execute(Object o, InternalContextAdapter context) 92 throws MethodInvocationException 93 { 94 100 101 VelMethod method = null; 102 103 Object [] params = new Object [paramCount]; 104 105 try 106 { 107 110 111 IntrospectionCacheData icd = context.icacheGet( this ); 112 Class c = o.getClass(); 113 114 119 120 if ( icd != null && icd.contextData == c ) 121 { 122 126 127 for (int j = 0; j < paramCount; j++) 128 params[j] = jjtGetChild(j + 1).value(context); 129 130 133 134 method = (VelMethod) icd.thingy; 135 } 136 else 137 { 138 142 143 for (int j = 0; j < paramCount; j++) 144 params[j] = jjtGetChild(j + 1).value(context); 145 146 method = rsvc.getUberspect().getMethod(o, methodName, params, new Info("",1,1)); 147 148 if (method != null) 149 { 150 icd = new IntrospectionCacheData(); 151 icd.contextData = c; 152 icd.thingy = method; 153 context.icachePut( this, icd ); 154 } 155 } 156 157 162 163 if (method == null) 164 return null; 165 } 166 catch( MethodInvocationException mie ) 167 { 168 173 174 throw mie; 175 } 176 catch( Exception e ) 177 { 178 181 182 rsvc.error("ASTMethod.execute() : exception from introspection : " + e); 183 return null; 184 } 185 186 try 187 { 188 196 197 Object obj = method.invoke(o, params); 198 199 if (obj == null) 200 { 201 if( method.getReturnType() == Void.TYPE) 202 return new String (""); 203 } 204 205 return obj; 206 } 207 catch( InvocationTargetException ite ) 208 { 209 216 217 EventCartridge ec = context.getEventCartridge(); 218 219 223 224 if ( ec != null && ite.getTargetException() instanceof java.lang.Exception ) 225 { 226 try 227 { 228 return ec.methodException( o.getClass(), methodName, (Exception )ite.getTargetException() ); 229 } 230 catch( Exception e ) 231 { 232 throw new MethodInvocationException( 233 "Invocation of method '" 234 + methodName + "' in " + o.getClass() 235 + " threw exception " 236 + e.getClass() + " : " + e.getMessage(), 237 e, methodName ); 238 } 239 } 240 else 241 { 242 245 246 throw new MethodInvocationException( 247 "Invocation of method '" 248 + methodName + "' in " + o.getClass() 249 + " threw exception " 250 + ite.getTargetException().getClass() + " : " 251 + ite.getTargetException().getMessage(), 252 ite.getTargetException(), methodName ); 253 } 254 } 255 catch( Exception e ) 256 { 257 rsvc.error("ASTMethod.execute() : exception invoking method '" 258 + methodName + "' in " + o.getClass() + " : " + e ); 259 260 return null; 261 } 262 } 263 } 264 | Popular Tags |