| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.services; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.ComponentResources; |
| 18 | |
import org.apache.tapestry5.services.ComponentMethodAdvice; |
| 19 | |
import org.apache.tapestry5.services.ComponentMethodInvocation; |
| 20 | |
|
| 21 | |
public abstract class AbstractComponentMethodInvocation implements ComponentMethodInvocation |
| 22 | |
{ |
| 23 | |
private final ComponentMethodInvocationInfo info; |
| 24 | |
|
| 25 | |
private final ComponentResources resources; |
| 26 | |
|
| 27 | 200 | private int adviceIndex = 0; |
| 28 | |
|
| 29 | |
private Throwable thrown; |
| 30 | |
|
| 31 | |
private Object result; |
| 32 | |
|
| 33 | |
public AbstractComponentMethodInvocation(ComponentMethodInvocationInfo info, ComponentResources resources) |
| 34 | 200 | { |
| 35 | 200 | this.info = info; |
| 36 | 200 | this.resources = resources; |
| 37 | 200 | } |
| 38 | |
|
| 39 | |
public ComponentResources getComponentResources() |
| 40 | |
{ |
| 41 | 200 | return resources; |
| 42 | |
} |
| 43 | |
|
| 44 | |
public String getMethodName() |
| 45 | |
{ |
| 46 | 4 | return info.getMethodName(); |
| 47 | |
} |
| 48 | |
|
| 49 | |
public Class getResultType() |
| 50 | |
{ |
| 51 | 14 | return info.getResultType(); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public int getParameterCount() |
| 55 | |
{ |
| 56 | 16 | return info.getParameterCount(); |
| 57 | |
} |
| 58 | |
|
| 59 | |
public Class getParameterType(int index) |
| 60 | |
{ |
| 61 | 2 | return info.getParameterType(index); |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public void proceed() |
| 68 | |
{ |
| 69 | 400 | if (adviceIndex >= info.getAdviceCount()) |
| 70 | |
{ |
| 71 | 200 | invokeAdvisedMethod(); |
| 72 | 198 | return; |
| 73 | |
} |
| 74 | |
|
| 75 | 200 | ComponentMethodAdvice advice = info.getAdvice(adviceIndex++); |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | 200 | advice.advise(this); |
| 81 | 198 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
protected abstract void invokeAdvisedMethod(); |
| 87 | |
|
| 88 | |
public boolean isFail() |
| 89 | |
{ |
| 90 | 8 | return thrown != null; |
| 91 | |
} |
| 92 | |
|
| 93 | |
public <T extends Throwable> T getThrown(Class<T> throwableClass) |
| 94 | |
{ |
| 95 | 172 | if (throwableClass.isInstance(thrown)) |
| 96 | 4 | return throwableClass.cast(thrown); |
| 97 | |
|
| 98 | 168 | return null; |
| 99 | |
} |
| 100 | |
|
| 101 | |
public void overrideThrown(Exception thrown) |
| 102 | |
{ |
| 103 | 4 | for (Class type : info.getExceptionTypes()) |
| 104 | |
{ |
| 105 | 4 | if (type.isInstance(thrown)) |
| 106 | |
{ |
| 107 | 4 | this.thrown = thrown; |
| 108 | 4 | return; |
| 109 | |
} |
| 110 | |
} |
| 111 | |
|
| 112 | 0 | throw new IllegalArgumentException( |
| 113 | |
String.format("Exception class %s is not a declared exception type for method %s().", |
| 114 | |
thrown.getClass(), |
| 115 | |
info.getMethodName())); |
| 116 | |
} |
| 117 | |
|
| 118 | |
public Object getResult() |
| 119 | |
{ |
| 120 | 198 | return result; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public void overrideResult(Object newResult) |
| 124 | |
{ |
| 125 | 198 | if (newResult != null) |
| 126 | |
{ |
| 127 | 198 | Class expectedType = info.getEffectiveResultType(); |
| 128 | |
|
| 129 | 198 | if (!expectedType.isInstance(newResult)) |
| 130 | |
{ |
| 131 | 0 | throw new IllegalArgumentException( |
| 132 | |
String.format("Invalid result value (%s) does not match return type %s for method %s.", |
| 133 | |
newResult, |
| 134 | |
expectedType.getName(), |
| 135 | |
info.getMethodName())); |
| 136 | |
} |
| 137 | |
} |
| 138 | |
|
| 139 | 198 | result = newResult; |
| 140 | 198 | thrown = null; |
| 141 | 198 | } |
| 142 | |
} |