1 16 17 package org.apache.taglibs.standard.lang.jpath.expression; 18 19 import java.beans.BeanInfo ; 20 import java.beans.IntrospectionException ; 21 import java.beans.Introspector ; 22 import java.beans.MethodDescriptor ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Modifier ; 26 27 import javax.servlet.jsp.PageContext ; 28 29 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext; 30 31 38 public class MethodCall extends SimpleNode implements Introspectable { 39 40 47 public MethodCall(int id) { 48 super(id); 49 } 50 51 59 public MethodCall(Parser p, int id) { 60 super(p, id); 61 } 62 63 76 public Object evaluate( 77 PageContext pageContext, IterationContext icontext, int scope) 78 throws EvaluationException { 79 throw new EvaluationException(this, 80 "A MethodCall must be called on " + "another object"); 81 } 82 83 96 public Object evaluate( 97 PageContext pageContext, IterationContext icontext, Object parent) 98 throws EvaluationException { 99 100 Object result = null; 101 102 String methodName = ((Identifier) jjtGetChild(0)).val; 104 105 if (parent != null) { 106 try { 107 MethodDescriptor md = getFeatureDescriptor(parent.getClass(), 108 methodName); 109 Object [] args = new Object [jjtGetNumChildren() - 1]; 110 111 for (int i = 1; i < jjtGetNumChildren(); i++) { 112 args[i - 1] = jjtGetChild(i).evaluate(pageContext, 113 icontext); 114 } 115 116 if (md != null) { 117 118 result = tempGetAttribute(parent, methodName, args); 120 } 121 } catch (IntrospectionException ie) { 122 throw new EvaluationException(this, 123 "Introspection Exception:" + ie.getMessage()); 124 } catch (NoSuchMethodException nsme) { 125 throw new EvaluationException(this, 126 "NoSuchMethodException:" + nsme.getMessage()); 127 } catch (IllegalAccessException iae) { 128 throw new EvaluationException(this, 129 "IllegalAccessException:" + iae.getMessage()); 130 } catch (InvocationTargetException ite) { 131 throw new EvaluationException(this, 132 "InvocationTargetException:" + ite.getMessage()); 133 } 134 } 135 136 return result; 141 } 142 143 155 private MethodDescriptor getFeatureDescriptor(Class c, String key) 156 throws IntrospectionException { 157 158 BeanInfo beanInfo = Introspector.getBeanInfo(c); 159 MethodDescriptor [] mda = beanInfo.getMethodDescriptors(); 160 161 for (int i = mda.length - 1; i >= 0; --i) { 162 MethodDescriptor md = mda[i]; 163 164 if (md.getName().equals(key)) { 165 return md; 166 } 167 } 168 169 return null; 170 } 171 172 187 private Object getAttribute(MethodDescriptor md, Object o, Object [] args) 188 throws NoSuchMethodException , IllegalAccessException , 189 InvocationTargetException { 190 191 Object result = null; 192 Method m = md.getMethod(); 193 194 result = m.invoke(o, args); 195 196 return result; 197 } 198 199 215 private Object tempGetAttribute(Object parent, String key, Object [] args) 216 throws IntrospectionException , NoSuchMethodException , 217 IllegalAccessException , InvocationTargetException { 218 219 Object result; 220 Class c = parent.getClass(); 221 Method [] methods = c.getMethods(); 222 Method m = null; 223 224 for (int i = 0; i < methods.length; i++) { 225 if (methods[i].getName().equals(key)) { 226 m = methods[i]; 227 } 228 } 229 230 m = getPublicMethod(c, m.getName(), m.getParameterTypes()); 231 result = m.invoke(parent, args); 232 233 return result; 234 } 235 236 249 private Method getPublicMethod(Class c, String name, Class [] paramTypes) 250 throws NoSuchMethodException { 251 252 Method result = null; 253 254 if ((c.getModifiers() & Modifier.PUBLIC) == 0) { 255 Class sc = c.getSuperclass(); 256 257 if (sc != null) { 258 try { 259 result = getPublicMethod(sc, name, paramTypes); 260 } catch (NoSuchMethodException nsme) { 261 262 } 264 } 265 266 if (result == null) { 267 Class [] interfaces = c.getInterfaces(); 268 269 for (int i = 0; i < interfaces.length; i++) { 270 try { 271 result = getPublicMethod(interfaces[i], name, 272 paramTypes); 273 } catch (NoSuchMethodException nsme) { 274 275 } 277 } 278 } 279 } else { 280 281 result = c.getMethod(name, paramTypes); 283 } 284 285 return result; 286 } 287 } 288 | Popular Tags |