diff --git a/projects/samskivert/src/java/com/samskivert/util/RepeatCallTracker.java b/projects/samskivert/src/java/com/samskivert/util/RepeatCallTracker.java new file mode 100644 index 00000000..a8f5c480 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/util/RepeatCallTracker.java @@ -0,0 +1,51 @@ +// +// $Id$ + +package com.samskivert.util; + +import com.samskivert.Log; + +/** + * A class used to debug situations where a method that should be called + * once and only once is being called again later and one wishes to report + * the stack trace of the first and current calling on any repeat + * callings. Note: this object is not thread safe. If the + * multiple callings may take place on different threads, you must + * synchronize the calls to {@link #checkCall} yourself. + */ +public class RepeatCallTracker +{ + /** + * This method should be called when the code passes through the code + * path that should be called only once. The first time through this + * path, the method will return false and record the stack trace. + * Subsquent calls will log an error and report the current and + * first-time-through stack traces. + * + * @param warning the warning message to issue prior to logging the + * two stack traces. + */ + public boolean checkCall (String warning) + { + if (_firstCall == null) { + _firstCall = new Exception("---- First call ----"); + _firstCall.fillInStackTrace(); + return false; + } + + Log.logStackTrace(new Exception(warning)); + Log.logStackTrace(_firstCall); + return true; + } + + /** + * Resets this repeat call tracker. + */ + public void clear () + { + _firstCall = null; + } + + /** Used to keep the stack trace around from the first call. */ + protected Exception _firstCall; +}