From 0051f4061d16a0badcdd918626adf0cb70a327e9 Mon Sep 17 00:00:00 2001 From: Ekaterina Frolova Date: Thu, 16 Nov 2017 13:54:30 +0300 Subject: [PATCH 1/2] fix coding style, mark variables final --- .../jetpad/base/ThrowableHandlers.java | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java b/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java index b1c8e4d9..dbe68d2d 100755 --- a/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java +++ b/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java @@ -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 ourForceProduction = new ThreadLocal() { + private static final ThreadLocal FORCE_PRODUCTION = new ThreadLocal() { @Override protected Boolean initialValue() { - return false; + return Boolean.FALSE; } }; @SuppressWarnings("NonJREEmulationClassesInClientCode") - private static ThreadLocal ourHandlers = new ThreadLocal() { + private static final ThreadLocal HANDLERS = new ThreadLocal() { @Override protected MyEventSource initialValue() { return new MyEventSource(); } }; - private final static Set ourLeaks = Collections.newSetFromMap( - new IdentityHashMap()); + private final static Set LEAKS = Collections.newSetFromMap(new IdentityHashMap()); public static Registration addHandler(Consumer handler) { - final Registration handlerReg = ourHandlers.get().addHandler(handler); + final Registration handlerReg = HANDLERS.get().addHandler(handler); final Value 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); } } }); @@ -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; @@ -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()) { @@ -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; } @@ -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); } } From d1bae32e36661057c772d0d3634407c701844002 Mon Sep 17 00:00:00 2001 From: Ekaterina Frolova Date: Thu, 16 Nov 2017 14:06:38 +0300 Subject: [PATCH 2/2] add ability to disable logging from ThrowableHandlers --- .../jetbrains/jetpad/base/ThrowableHandlers.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java b/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java index dbe68d2d..73198f7c 100755 --- a/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java +++ b/util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java @@ -40,6 +40,14 @@ protected Boolean initialValue() { } }; + @SuppressWarnings("NonJREEmulationClassesInClientCode") + private static final ThreadLocal LOGGING_ENABLED = new ThreadLocal() { + @Override + protected Boolean initialValue() { + return Boolean.TRUE; + } + }; + @SuppressWarnings("NonJREEmulationClassesInClientCode") private static final ThreadLocal HANDLERS = new ThreadLocal() { @Override @@ -50,6 +58,10 @@ protected MyEventSource initialValue() { private final static Set LEAKS = Collections.newSetFromMap(new IdentityHashMap()); + public static void setLoggingEnabled(boolean enabled) { + LOGGING_ENABLED.set(enabled); + } + public static Registration addHandler(Consumer handler) { final Registration handlerReg = HANDLERS.get().addHandler(handler); @@ -101,7 +113,7 @@ public static void handle(Throwable t) { } // ourForceProduction is used only in tests, where severe level is logged by default. - if (!FORCE_PRODUCTION.get()) { + if (!FORCE_PRODUCTION.get() && LOGGING_ENABLED.get()) { LOG.log(Level.SEVERE, "Exception handled at ThrowableHandlers", t); } handleError(t);