1 16 package org.apache.myfaces.el; 17 18 import org.apache.myfaces.el.ValueBindingImpl.NotVariableReferenceException; 19 20 import org.apache.commons.beanutils.MethodUtils; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.application.Application; 25 import javax.faces.component.StateHolder; 26 import javax.faces.context.FacesContext; 27 import javax.faces.el.*; 28 import javax.faces.event.AbortProcessingException; 29 import javax.faces.validator.ValidatorException; 30 import javax.servlet.jsp.el.ELException ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Method ; 33 34 35 58 public class MethodBindingImpl extends MethodBinding 59 implements StateHolder 60 { 61 static final Log log = LogFactory.getLog(MethodBindingImpl.class); 62 63 65 ValueBindingImpl _valueBinding; 66 Class [] _argClasses; 67 68 70 public MethodBindingImpl(Application application, String reference, 71 Class [] argClasses) 72 { 73 _valueBinding = new ValueBindingImpl(application, reference.trim()); 78 _argClasses = argClasses; 79 } 80 81 83 public String getExpressionString() 84 { 85 return _valueBinding._expressionString; 86 } 87 88 public Class getType(FacesContext facesContext) 89 { 90 try 91 { 92 Object [] baseAndProperty = resolveToBaseAndProperty(facesContext); 93 Object base = baseAndProperty[0]; 94 Object property = baseAndProperty[1]; 95 96 return base.getClass().getMethod(property.toString(), _argClasses) 97 .getReturnType(); 98 } 99 catch (ReferenceSyntaxException e) 100 { 101 throw e; 102 } 103 catch (IndexOutOfBoundsException e) 104 { 105 throw new PropertyNotFoundException("Expression: " 107 + getExpressionString(), e); 108 } 109 catch (Exception e) 110 { 111 log.error( 112 "Cannot get type for expression " + getExpressionString(), e); 113 throw new EvaluationException("Expression: " 114 + getExpressionString(), e); 115 } 116 } 117 118 public Object invoke(FacesContext facesContext, Object [] args) 119 throws EvaluationException, MethodNotFoundException 120 { 121 try 122 { 123 Object [] baseAndProperty = resolveToBaseAndProperty(facesContext); 124 Object base = baseAndProperty[0]; 125 Object property = baseAndProperty[1]; 126 127 Method m = base.getClass().getMethod(property.toString(), _argClasses); 128 129 m = MethodUtils.getAccessibleMethod(m); 132 if (m == null) 133 { 134 throw new MethodNotFoundException( 135 getExpressionString() + " (not accessible!)"); 136 } 137 138 return m.invoke(base, args); 139 } 140 catch (ReferenceSyntaxException e) 141 { 142 throw e; 143 } 144 catch (IndexOutOfBoundsException e) 145 { 146 throw new PropertyNotFoundException("Expression: " 148 + getExpressionString(), e); 149 } 150 catch (InvocationTargetException e) 151 { 152 Throwable cause = e.getCause(); 153 if (cause != null) 154 { 155 if (cause instanceof ValidatorException || 156 cause instanceof AbortProcessingException) 157 { 158 throw new EvaluationException(cause); 159 } 160 else 161 { 162 log.error("Exception while invoking expression " 163 + getExpressionString(), cause); 164 throw new EvaluationException("Expression: " 165 + getExpressionString(), cause); 166 } 167 } 168 else 169 { 170 log.error("Exception while invoking expression " 171 + getExpressionString(), e); 172 throw new EvaluationException("Expression: " 173 + getExpressionString(), e); 174 } 175 } 176 catch (Exception e) 177 { 178 log.error("Exception while invoking expression " 179 + getExpressionString(), e); 180 throw new EvaluationException("Expression: " 181 + getExpressionString(), e); 182 } 183 } 184 185 protected Object [] resolveToBaseAndProperty(FacesContext facesContext) 186 throws ELException 187 { 188 if (facesContext == null) 189 { 190 throw new NullPointerException ("facesContext"); 191 } 192 193 try 194 { 195 Object base = _valueBinding.resolveToBaseAndProperty(facesContext); 196 197 if (!(base instanceof Object [])) 198 { 199 String errorMessage = "Expression not a valid method binding: " 200 + getExpressionString(); 201 log.error(errorMessage); 202 throw new ReferenceSyntaxException(errorMessage); 203 } 204 205 return (Object []) base; 206 } 207 catch (NotVariableReferenceException e) 208 { 209 throw new ReferenceSyntaxException("Expression: " 210 + getExpressionString(), e); 211 } 212 } 213 214 public String toString() 215 { 216 return _valueBinding.toString(); 217 } 218 219 221 private boolean _transient = false; 222 223 227 public MethodBindingImpl() 228 { 229 _valueBinding = null; 230 _argClasses = null; 231 } 232 233 public Object saveState(FacesContext facescontext) 234 { 235 return new Object [] { _valueBinding.saveState(facescontext), 236 _argClasses}; 237 } 238 239 public void restoreState(FacesContext facescontext, Object obj) 240 { 241 Object [] ar = (Object []) obj; 242 _valueBinding = new ValueBindingImpl(); 243 _valueBinding.restoreState(facescontext, ar[0]); 244 _argClasses = (Class []) ar[1]; 245 } 246 247 public boolean isTransient() 248 { 249 return _transient; 250 } 251 252 public void setTransient(boolean flag) 253 { 254 _transient = flag; 255 } 256 257 } | Popular Tags |