View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.guard;
14  
15  import java.lang.reflect.Method;
16  import java.util.LinkedList;
17  
18  import net.sf.oval.exception.ConstraintsViolatedException;
19  import net.sf.oval.exception.InvokingMethodFailedException;
20  import net.sf.oval.internal.util.MethodInvocationCommand;
21  
22  /**
23   * 
24   * @author Sebastian Thomschke
25   */
26  public class ProbeModeListener extends ConstraintsViolatedAdapter
27  {
28  	private final Object target;
29  	private final LinkedList<MethodInvocationCommand> commands = new LinkedList<MethodInvocationCommand>();
30  
31  	/**
32  	 * Creates a new instance for the given target object.
33  	 * @param target the target object
34  	 */
35  	ProbeModeListener(final Object target)
36  	{
37  		this.target = target;
38  	}
39  
40  	/**
41  	 * Executes the collected method calls and clears the internal list holding them.
42  	 */
43  	public synchronized void commit() throws InvokingMethodFailedException, ConstraintsViolatedException
44  	{
45  		for (final MethodInvocationCommand cmd : commands)
46  		{
47  			cmd.execute();
48  		}
49  		commands.clear();
50  	}
51  
52  	/**
53  	 * Returns the object that is/was in probe mode.
54  	 * @return the object that is/was in probe mode
55  	 */
56  	public Object getTarget()
57  	{
58  		return target;
59  	}
60  
61  	/**
62  	 * Adds the given method and method arguments to the method call stack.
63  	 * @param method the method
64  	 * @param args the method arguments
65  	 */
66  	void onMethodCall(final Method method, final Object[] args)
67  	{
68  		commands.add(new MethodInvocationCommand(target, method, args));
69  	}
70  }