Skip to content

Commit

Permalink
fix coding style, mark variables final
Browse files Browse the repository at this point in the history
  • Loading branch information
katepol committed Nov 16, 2017
1 parent c206c8d commit 0051f40
Showing 1 changed file with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,37 @@ public final class ThrowableHandlers {
// we can use ThreadLocal here because of our own emulation
// at model-gwt jetbrains.jetpad.model.jre.java.lang.ThreadLocal
@SuppressWarnings("NonJREEmulationClassesInClientCode")
private static ThreadLocal<Boolean> ourForceProduction = new ThreadLocal<Boolean>() {
private static final ThreadLocal<Boolean> FORCE_PRODUCTION = new ThreadLocal<Boolean>() {
@Override
protected Boolean initialValue() {
return false;
return Boolean.FALSE;
}
};

@SuppressWarnings("NonJREEmulationClassesInClientCode")
private static ThreadLocal<MyEventSource> ourHandlers = new ThreadLocal<MyEventSource>() {
private static final ThreadLocal<MyEventSource> HANDLERS = new ThreadLocal<MyEventSource>() {
@Override
protected MyEventSource initialValue() {
return new MyEventSource();
}
};

private final static Set<RuntimeException> ourLeaks = Collections.newSetFromMap(
new IdentityHashMap<RuntimeException, Boolean>());
private final static Set<RuntimeException> LEAKS = Collections.newSetFromMap(new IdentityHashMap<RuntimeException, Boolean>());

public static Registration addHandler(Consumer<? super Throwable> handler) {
final Registration handlerReg = ourHandlers.get().addHandler(handler);
final Registration handlerReg = HANDLERS.get().addHandler(handler);

final Value<Registration> leakReg = new Value<>();
if (DEBUG) {
final RuntimeException leakStacktrace = new RuntimeException("Potential leak");
synchronized (ourLeaks) {
ourLeaks.add(leakStacktrace);
synchronized (LEAKS) {
LEAKS.add(leakStacktrace);
}
leakReg.set(new Registration() {
@Override
protected void doRemove() {
synchronized (ourLeaks) {
ourLeaks.remove(leakStacktrace);
synchronized (LEAKS) {
LEAKS.remove(leakStacktrace);
}
}
});
Expand All @@ -82,19 +81,18 @@ protected void doRemove() {
}

public static void asInProduction(Runnable r) {
if (ourForceProduction.get()) {
if (FORCE_PRODUCTION.get()) {
throw new IllegalStateException();
}
ourForceProduction.set(true);
FORCE_PRODUCTION.set(Boolean.TRUE);
try {
r.run();
} finally {
ourForceProduction.set(false);
FORCE_PRODUCTION.set(Boolean.FALSE);
}
}

public static void handle(Throwable t) {

if (isInUnitTests(t)) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
Expand All @@ -103,17 +101,15 @@ public static void handle(Throwable t) {
}

// ourForceProduction is used only in tests, where severe level is logged by default.
if (!ourForceProduction.get()) {
if (!FORCE_PRODUCTION.get()) {
LOG.log(Level.SEVERE, "Exception handled at ThrowableHandlers", t);
}

handleError(t);

ourHandlers.get().fire(t);
HANDLERS.get().fire(t);
}

private static boolean isInUnitTests(Throwable t) {
if (ourForceProduction.get()) {
if (FORCE_PRODUCTION.get()) {
return false;
}
for (StackTraceElement e : t.getStackTrace()) {
Expand All @@ -125,13 +121,10 @@ private static boolean isInUnitTests(Throwable t) {
}

static void handleError(Throwable t) {

if (isClient(t)) {
return;
}

Error error = getError(t);

if (error != null) {
throw error;
}
Expand Down Expand Up @@ -167,15 +160,15 @@ private static Error getError(Throwable t) {
}

static int getHandlersSize() {
return ourHandlers.get().size();
return HANDLERS.get().size();
}

public static boolean checkForLeaks(PrintStream stream) {
if (ourHandlers.get().size() == 0) {
if (HANDLERS.get().size() == 0) {
return false;
}
synchronized (ourLeaks) {
for (RuntimeException leak : ourLeaks) {
synchronized (LEAKS) {
for (RuntimeException leak : LEAKS) {
leak.printStackTrace(stream);
}
}
Expand Down

0 comments on commit 0051f40

Please sign in to comment.