1 /*
2  * Copyright 2001-2009 OFFIS, Tammo Freese
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.easymock.internal;
17 
18 import java.io.Serializable;
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Method;
21 
22 public final class MockInvocationHandler implements InvocationHandler, Serializable {
23 
24     private static final long serialVersionUID = -7799769066534714634L;
25 
26     private final MocksControl control;
27 
MockInvocationHandler(MocksControl control)28     public MockInvocationHandler(MocksControl control) {
29         this.control = control;
30     }
31 
invoke(Object proxy, Method method, Object[] args)32     public Object invoke(Object proxy, Method method, Object[] args)
33             throws Throwable {
34         try {
35             if (control.getState() instanceof RecordState) {
36                 LastControl.reportLastControl(control);
37             }
38             return control.getState().invoke(
39                     new Invocation(proxy, method, args));
40         } catch (RuntimeExceptionWrapper e) {
41             throw e.getRuntimeException().fillInStackTrace();
42         } catch (AssertionErrorWrapper e) {
43             throw e.getAssertionError().fillInStackTrace();
44         } catch (ThrowableWrapper t) {
45             throw t.getThrowable().fillInStackTrace();
46         } catch (Throwable t) {
47             throw t; // let all unwrapped pass unmodified
48         }
49     }
50 
getControl()51     public MocksControl getControl() {
52         return control;
53     }
54 }