| 1 | |
package biz.xsoftware.impl.mock; |
| 2 | |
|
| 3 | |
import java.lang.reflect.Method; |
| 4 | |
import java.util.logging.Level; |
| 5 | |
import java.util.logging.Logger; |
| 6 | |
|
| 7 | |
import biz.xsoftware.mock.Behavior; |
| 8 | |
|
| 9 | |
public final class BehaviorInfo implements Action { |
| 10 | |
|
| 11 | 5 | private static Logger log = Logger.getLogger(BehaviorInfo.class.getName()); |
| 12 | |
|
| 13 | |
private Behavior behavior; |
| 14 | |
|
| 15 | |
private Method behaviorMethod; |
| 16 | |
|
| 17 | |
private Method clonerMethod; |
| 18 | |
|
| 19 | 12 | protected BehaviorInfo(Behavior behavior, Method method) { |
| 20 | 12 | if (log.isLoggable(Level.FINEST)) { |
| 21 | 0 | log.finest("creating new : " + behavior + " : " + method.getName()); |
| 22 | |
} |
| 23 | 12 | if (behavior == null) |
| 24 | 0 | throw new IllegalArgumentException("behavior cannot be null"); |
| 25 | 12 | else if (method == null) |
| 26 | 0 | throw new IllegalArgumentException("method should not be null"); |
| 27 | 12 | this.behavior = behavior; |
| 28 | 12 | this.behaviorMethod = method; |
| 29 | 12 | } |
| 30 | |
|
| 31 | |
public Object execute(Object[] args) throws Throwable { |
| 32 | |
try { |
| 33 | 12 | if (log.isLoggable(Level.FINEST)) { |
| 34 | 0 | StringBuilder sb = new StringBuilder(); |
| 35 | 0 | sb.append("about to execute "); |
| 36 | 0 | sb.append(behaviorMethod); |
| 37 | 0 | log.finest(sb.toString()); |
| 38 | |
} |
| 39 | 12 | return behaviorMethod.invoke(behavior, args); |
| 40 | 2 | } catch (Exception e) { |
| 41 | 2 | if (log.isLoggable(Level.FINEST)) { |
| 42 | 0 | log.log(Level.FINEST, "failed in execution of " + behaviorMethod.getName() + ":" + e, e); |
| 43 | |
} |
| 44 | |
|
| 45 | 2 | if (e.getCause() != null) |
| 46 | 2 | throw e.getCause(); |
| 47 | |
else |
| 48 | 0 | throw e; |
| 49 | |
} |
| 50 | |
} |
| 51 | |
|
| 52 | |
protected Object[] runClonerMethod(Object[] args) throws Throwable { |
| 53 | 4 | return (Object[]) clonerMethod.invoke(behavior, args); |
| 54 | |
} |
| 55 | |
|
| 56 | |
protected void setClonerMethod(Method clonerMethod) { |
| 57 | 4 | this.clonerMethod = clonerMethod; |
| 58 | 4 | } |
| 59 | |
|
| 60 | |
protected boolean hasClonerMethod() { |
| 61 | 9 | if (clonerMethod == null) |
| 62 | 5 | return false; |
| 63 | 4 | return true; |
| 64 | |
} |
| 65 | |
} |