1 16 package org.apache.commons.betwixt.expression; 17 18 import java.lang.reflect.Method ; 19 20 25 public class MethodExpression implements Expression { 26 27 28 protected static Object [] NULL_ARGUMENTS; 29 30 protected static Class [] NULL_CLASSES; 31 32 33 private Method method; 34 35 36 public MethodExpression() { 37 } 38 39 44 public MethodExpression(Method method) { 45 this.method = method; 46 } 47 48 56 public Object evaluate(Context context) { 57 Object bean = context.getBean(); 58 if ( bean != null ) { 59 Object [] arguments = getArguments(); 60 try { 61 return method.invoke( bean, arguments ); 62 63 } catch (IllegalAccessException e) { 64 try { 66 Class type = bean.getClass(); 67 Method alternate = findAlternateMethod( type, method ); 68 if ( alternate != null ) { 69 return alternate.invoke( bean, arguments ); 70 } 71 } catch (Exception e2) { 72 handleException(context, e2); 73 } 74 } catch (Exception e) { 75 handleException(context, e); 76 } 77 } 78 return null; 79 } 80 81 85 public void update(Context context, String newValue) { 86 } 88 89 94 public Method getMethod() { 95 return method; 96 } 97 98 103 public void setMethod(Method method) { 104 this.method = method; 105 } 106 107 110 114 protected Object [] getArguments() { 115 return NULL_ARGUMENTS; 116 } 117 118 126 protected Method findAlternateMethod( 127 Class type, 128 Method method ) { 129 Class [] interfaces = type.getInterfaces(); 134 if ( interfaces != null ) { 135 String name = method.getName(); 136 for ( int i = 0, size = interfaces.length; i < size; i++ ) { 137 Class otherType = interfaces[i]; 138 try { 141 Method alternate = otherType.getMethod( name, NULL_CLASSES ); 142 if ( alternate != null && alternate != method ) { 143 return alternate; 144 } 145 } catch (NoSuchMethodException e) { 146 } 148 } 149 } 150 return null; 151 } 152 153 161 protected void handleException(Context context, Exception e) { 162 context.getLog().error("[MethodExpression] Cannot evaluate expression ", e); 164 } 165 166 170 public String toString() { 171 return "MethodExpression [method=" + method + "]"; 172 } 173 } 174 | Popular Tags |