001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian 003 * Thomschke. 004 * 005 * All Rights Reserved. This program and the accompanying materials 006 * are made available under the terms of the Eclipse Public License v1.0 007 * which accompanies this distribution, and is available at 008 * http://www.eclipse.org/legal/epl-v10.html 009 * 010 * Contributors: 011 * Sebastian Thomschke - initial implementation. 012 *******************************************************************************/ 013 package net.sf.oval.guard; 014 015 import java.lang.reflect.Method; 016 import java.util.LinkedList; 017 018 import net.sf.oval.exception.ConstraintsViolatedException; 019 import net.sf.oval.exception.InvokingMethodFailedException; 020 import net.sf.oval.internal.util.MethodInvocationCommand; 021 022 /** 023 * 024 * @author Sebastian Thomschke 025 */ 026 public class ProbeModeListener extends ConstraintsViolatedAdapter 027 { 028 private final Object target; 029 private final LinkedList<MethodInvocationCommand> commands = new LinkedList<MethodInvocationCommand>(); 030 031 /** 032 * Creates a new instance for the given target object. 033 * @param target the target object 034 */ 035 ProbeModeListener(final Object target) 036 { 037 this.target = target; 038 } 039 040 /** 041 * Executes the collected method calls and clears the internal list holding them. 042 */ 043 public synchronized void commit() throws InvokingMethodFailedException, ConstraintsViolatedException 044 { 045 for (final MethodInvocationCommand cmd : commands) 046 { 047 cmd.execute(); 048 } 049 commands.clear(); 050 } 051 052 /** 053 * Returns the object that is/was in probe mode. 054 * @return the object that is/was in probe mode 055 */ 056 public Object getTarget() 057 { 058 return target; 059 } 060 061 /** 062 * Adds the given method and method arguments to the method call stack. 063 * @param method the method 064 * @param args the method arguments 065 */ 066 void onMethodCall(final Method method, final Object[] args) 067 { 068 commands.add(new MethodInvocationCommand(target, method, args)); 069 } 070 }