| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.el.impl; |
| 17 | |
|
| 18 | |
import java.lang.reflect.InvocationTargetException; |
| 19 | |
import java.lang.reflect.Method; |
| 20 | |
|
| 21 | |
import javax.faces.component.StateHolder; |
| 22 | |
import javax.faces.context.FacesContext; |
| 23 | |
import javax.faces.el.EvaluationException; |
| 24 | |
import javax.faces.el.MethodBinding; |
| 25 | |
import javax.faces.el.MethodNotFoundException; |
| 26 | |
import javax.faces.el.PropertyNotFoundException; |
| 27 | |
import javax.faces.el.ReferenceSyntaxException; |
| 28 | |
|
| 29 | |
import org.seasar.framework.util.AssertionUtil; |
| 30 | |
import org.seasar.teeda.core.el.ELParser; |
| 31 | |
import org.seasar.teeda.core.el.ValueBindingBase; |
| 32 | |
import org.seasar.teeda.core.exception.ExtendEvaluationException; |
| 33 | |
import org.seasar.teeda.core.exception.ExtendMethodNotFoundExceptin; |
| 34 | |
import org.seasar.teeda.core.exception.MethodNotAccessibleException; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class MethodBindingImpl extends MethodBinding implements StateHolder { |
| 40 | |
|
| 41 | |
private ValueBindingBase vb_; |
| 42 | |
|
| 43 | |
private Class[] classes_; |
| 44 | |
|
| 45 | |
private ELParser parser_; |
| 46 | |
|
| 47 | 10 | private boolean transientValue_ = false; |
| 48 | |
|
| 49 | |
public MethodBindingImpl(ValueBindingBase vb, Class[] classes, |
| 50 | 8 | ELParser parser) { |
| 51 | 8 | classes_ = classes; |
| 52 | 8 | vb_ = vb; |
| 53 | 8 | parser_ = parser; |
| 54 | 8 | } |
| 55 | |
|
| 56 | 2 | public MethodBindingImpl() { |
| 57 | 2 | classes_ = null; |
| 58 | 2 | vb_ = null; |
| 59 | 2 | parser_ = null; |
| 60 | 2 | } |
| 61 | |
|
| 62 | |
public Object invoke(FacesContext context, Object[] params) |
| 63 | |
throws EvaluationException, MethodNotFoundException { |
| 64 | 6 | AssertionUtil.assertNotNull("context is null.", context); |
| 65 | 6 | Object[] obj = getBaseAndProperty(context); |
| 66 | 6 | Object base = obj[0]; |
| 67 | 6 | Object property = obj[1]; |
| 68 | |
try { |
| 69 | 6 | Method m = base.getClass().getMethod(property.toString(), classes_); |
| 70 | 5 | return m.invoke(base, params); |
| 71 | 0 | } catch (SecurityException e) { |
| 72 | 0 | throw new MethodNotAccessibleException(e, |
| 73 | |
base.getClass().getName(), getExpressionString()); |
| 74 | 1 | } catch (NoSuchMethodException e) { |
| 75 | 1 | throw new ExtendMethodNotFoundExceptin(e, |
| 76 | |
base.getClass().getName(), getExpressionString()); |
| 77 | 0 | } catch (IndexOutOfBoundsException e) { |
| 78 | 0 | throw new PropertyNotFoundException("Expression:" |
| 79 | |
+ getExpressionString(), e); |
| 80 | 1 | } catch (InvocationTargetException e) { |
| 81 | 1 | throw new ExtendEvaluationException(e.getCause(), base.getClass() |
| 82 | |
.getName(), getExpressionString()); |
| 83 | 0 | } catch (Exception e) { |
| 84 | 0 | throw new ExtendEvaluationException(e, base.getClass().getName(), |
| 85 | |
getExpressionString()); |
| 86 | |
} |
| 87 | |
} |
| 88 | |
|
| 89 | |
public Class getType(FacesContext context) throws MethodNotFoundException { |
| 90 | 3 | AssertionUtil.assertNotNull("context is null.", context); |
| 91 | 3 | Object[] obj = getBaseAndProperty(context); |
| 92 | 3 | Object base = obj[0]; |
| 93 | 3 | Object property = obj[1]; |
| 94 | |
|
| 95 | |
Class returnType; |
| 96 | |
try { |
| 97 | 3 | returnType = base.getClass().getMethod(property.toString(), |
| 98 | |
classes_).getReturnType(); |
| 99 | 3 | if (returnType.getName().equals("void")) { |
| 100 | 0 | return Void.class; |
| 101 | |
} |
| 102 | 3 | return returnType; |
| 103 | 0 | } catch (SecurityException e) { |
| 104 | 0 | throw new MethodNotAccessibleException(e, |
| 105 | |
base.getClass().getName(), getExpressionString()); |
| 106 | 0 | } catch (NoSuchMethodException e) { |
| 107 | 0 | throw new ExtendMethodNotFoundExceptin(e, |
| 108 | |
base.getClass().getName(), getExpressionString()); |
| 109 | 0 | } catch (IndexOutOfBoundsException e) { |
| 110 | 0 | throw new PropertyNotFoundException("Expression:" |
| 111 | |
+ getExpressionString(), e); |
| 112 | |
} |
| 113 | |
} |
| 114 | |
|
| 115 | |
public String getExpressionString() { |
| 116 | 3 | return vb_.getExpressionString(); |
| 117 | |
} |
| 118 | |
|
| 119 | |
public boolean isTransient() { |
| 120 | 0 | return transientValue_; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public void setTransient(boolean transientValue) { |
| 124 | 0 | transientValue_ = transientValue; |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
public Object saveState(FacesContext context) { |
| 128 | 2 | Object[] state = new Object[3]; |
| 129 | 2 | state[0] = vb_.saveState(context); |
| 130 | 2 | state[1] = classes_; |
| 131 | 2 | state[2] = parser_; |
| 132 | 2 | return state; |
| 133 | |
} |
| 134 | |
|
| 135 | |
public void restoreState(FacesContext context, Object state) { |
| 136 | 2 | Object[] obj = (Object[]) state; |
| 137 | 2 | vb_ = new ValueBindingImpl(); |
| 138 | 2 | vb_.restoreState(context, obj[0]); |
| 139 | 2 | classes_ = (Class[]) obj[1]; |
| 140 | 2 | parser_ = (ELParser) obj[2]; |
| 141 | 2 | } |
| 142 | |
|
| 143 | |
private Object[] getBaseAndProperty(FacesContext context) |
| 144 | |
throws ReferenceSyntaxException { |
| 145 | 9 | Object expression = vb_.getExpression(); |
| 146 | 9 | Object o = parser_.getExpressionProcessor().resolveBase(context, |
| 147 | |
expression); |
| 148 | 9 | if (!(o instanceof Object[])) { |
| 149 | 0 | throw new ReferenceSyntaxException(); |
| 150 | |
} |
| 151 | 9 | Object[] obj = (Object[]) o; |
| 152 | 9 | return obj; |
| 153 | |
} |
| 154 | |
} |