Created
December 16, 2010 19:47
-
-
Save olim7t/743887 to your computer and use it in GitHub Desktop.
Utility class to simulate dependencies between breakpoints in the Eclipse debugger.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2010, Olivier Michallat | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without modification, | |
* are permitted provided that the following conditions are met: | |
* | |
* 1. Redistributions of source code must retain the above copyright notice, this | |
* list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright notice, | |
* this list of conditions and the following disclaimer in the documentation | |
* and/or other materials provided with the distribution. | |
* 3. The names of the author or contributors may not be used to endorse or promote | |
* products derived from this software without specific prior written | |
* permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.concurrent.ConcurrentSkipListSet; | |
/** | |
* Utility class to simulate dependencies between breakpoints in the Eclipse debugger (e.g "stop at | |
* breakpoint X only if we have stopped at breakpoint Y before"). | |
* | |
* Usage : | |
* - create conditional breakpoints in your code (see the "Breakpoint properties" menu in Eclipse) ; | |
* - inside of the breakpoint's condition, use: | |
* - #set or #setAndBreak to set a marker, | |
* - #test or #testAndClear to depend on a marker set by a previous breakpoint. | |
*/ | |
public abstract class Breaks { | |
/** Returns an instance that maintains a private set of markers for each thread. */ | |
public static final Breaks threadLocal() { return THREAD_LOCAL; } | |
/** Returns an instance that maintains a shared set of markers for all threads. */ | |
public static final Breaks allThreads() { return ALL_THREADS; } | |
protected abstract Set<String> markers(); | |
/** Use this in the condition of a breakpoint where you want to set a marker but not stop. */ | |
public final boolean set(String marker) { | |
innerSet(marker); | |
return false; | |
} | |
/** Use this in the condition of a breakpoint where you want to set a marker and stop. */ | |
public final boolean setAndBreak(String marker) { | |
innerSet(marker); | |
return true; | |
} | |
private void innerSet(String marker) { | |
boolean wasNew = markers().add(marker); | |
assert wasNew; | |
} | |
/** Use this in the condition of a breakpoint where you want to stop if a marker is set. */ | |
public final boolean test(String marker) { return markers().contains(marker); } | |
/** | |
* Use this in the condition of a breakpoint if you want to stop if the marker is set, and then | |
* clear the marker. | |
*/ | |
public final boolean testAndClear(String marker) { return markers().remove(marker); } | |
private static Breaks THREAD_LOCAL = new Breaks() { | |
private final ThreadLocal<Set<String>> markers = new ThreadLocal<Set<String>>() { | |
@Override protected java.util.Set<String> initialValue() { return new HashSet<String>(); }; | |
}; | |
@Override protected java.util.Set<String> markers() { return markers.get(); } | |
}; | |
private static Breaks ALL_THREADS = new Breaks() { | |
private final Set<String> markers = new ConcurrentSkipListSet<String>(); | |
@Override protected java.util.Set<String> markers() { return markers; } | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment