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.Parser; 21 import org.apache.velocity.util.introspection.IntrospectionCacheData; 22 import org.apache.velocity.util.introspection.Info; 23 import org.apache.velocity.util.introspection.VelPropertyGet; 24 25 import org.apache.velocity.exception.MethodInvocationException; 26 import org.apache.velocity.app.event.EventCartridge; 27 28 import java.lang.reflect.InvocationTargetException ; 29 30 46 public class ASTIdentifier extends SimpleNode 47 { 48 private String identifier = ""; 49 50 53 protected Info uberInfo; 54 55 public ASTIdentifier(int id) 56 { 57 super(id); 58 } 59 60 public ASTIdentifier(Parser p, int id) 61 { 62 super(p, id); 63 } 64 65 66 public Object jjtAccept(ParserVisitor visitor, Object data) 67 { 68 return visitor.visit(this, data); 69 } 70 71 75 public Object init(InternalContextAdapter context, Object data) 76 throws Exception 77 { 78 super.init(context, data); 79 80 identifier = getFirstToken().image; 81 82 uberInfo = new Info(context.getCurrentTemplateName(), 83 getLine(), getColumn()); 84 85 return data; 86 } 87 88 91 public Object execute(Object o, InternalContextAdapter context) 92 throws MethodInvocationException 93 { 94 95 VelPropertyGet vg = null; 96 97 try 98 { 99 Class c = o.getClass(); 100 101 104 105 IntrospectionCacheData icd = context.icacheGet(this); 106 107 113 114 if (icd != null && icd.contextData == c) 115 { 116 vg = (VelPropertyGet) icd.thingy; 117 } 118 else 119 { 120 124 125 vg = rsvc.getUberspect().getPropertyGet(o,identifier, uberInfo); 126 127 if (vg != null && vg.isCacheable()) 128 { 129 icd = new IntrospectionCacheData(); 130 icd.contextData = c; 131 icd.thingy = vg; 132 context.icachePut(this,icd); 133 } 134 } 135 } 136 catch(Exception e) 137 { 138 rsvc.error("ASTIdentifier.execute() : identifier = " 139 + identifier + " : " + e); 140 } 141 142 145 146 if (vg == null) 147 { 148 return null; 149 } 150 151 155 try 156 { 157 return vg.invoke(o); 158 } 159 catch(InvocationTargetException ite) 160 { 161 EventCartridge ec = context.getEventCartridge(); 162 163 167 168 if (ec != null 169 && ite.getTargetException() instanceof java.lang.Exception ) 170 { 171 try 172 { 173 return ec.methodException(o.getClass(), vg.getMethodName(), 174 (Exception )ite.getTargetException()); 175 } 176 catch(Exception e) 177 { 178 throw new MethodInvocationException( 179 "Invocation of method '" + vg.getMethodName() + "'" 180 + " in " + o.getClass() 181 + " threw exception " 182 + ite.getTargetException().getClass() + " : " 183 + ite.getTargetException().getMessage(), 184 ite.getTargetException(), vg.getMethodName()); 185 } 186 } 187 else 188 { 189 192 193 throw new MethodInvocationException( 194 "Invocation of method '" + vg.getMethodName() + "'" 195 + " in " + o.getClass() 196 + " threw exception " 197 + ite.getTargetException().getClass() + " : " 198 + ite.getTargetException().getMessage(), 199 ite.getTargetException(), vg.getMethodName()); 200 201 202 } 203 } 204 catch(IllegalArgumentException iae) 205 { 206 return null; 207 } 208 catch(Exception e) 209 { 210 rsvc.error("ASTIdentifier() : exception invoking method " 211 + "for identifier '" + identifier + "' in " 212 + o.getClass() + " : " + e); 213 } 214 215 return null; 216 } 217 } 218 | Popular Tags |