From 21f8c121c27b53f1c57b941b54b0ca1f368555d6 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 27 Mar 2016 20:40:27 +0000 Subject: [PATCH] Refactor --- source/net/filebot/Settings.java | 3 +- .../net/filebot/util/ExceptionUtilities.java | 54 +++---------------- source/net/filebot/util/PreferencesMap.java | 4 -- .../filebot/util/ui/SimpleLabelProvider.java | 30 ++++------- 4 files changed, 18 insertions(+), 73 deletions(-) diff --git a/source/net/filebot/Settings.java b/source/net/filebot/Settings.java index 55219da3..a69b7427 100644 --- a/source/net/filebot/Settings.java +++ b/source/net/filebot/Settings.java @@ -15,7 +15,6 @@ import java.util.prefs.Preferences; import net.filebot.UserFiles.FileChooser; import net.filebot.archive.Archive.Extractor; import net.filebot.cli.ArgumentBean; -import net.filebot.util.ExceptionUtilities; import net.filebot.util.PreferencesList; import net.filebot.util.PreferencesMap; import net.filebot.util.PreferencesMap.PreferencesEntry; @@ -315,7 +314,7 @@ public final class Settings { // remove entries prefs.clear(); } catch (BackingStoreException e) { - throw ExceptionUtilities.asRuntimeException(e); + debug.warning(e.getMessage()); } } diff --git a/source/net/filebot/util/ExceptionUtilities.java b/source/net/filebot/util/ExceptionUtilities.java index e81befac..318e03f2 100644 --- a/source/net/filebot/util/ExceptionUtilities.java +++ b/source/net/filebot/util/ExceptionUtilities.java @@ -1,36 +1,21 @@ package net.filebot.util; -import static java.util.Arrays.*; - -import java.io.PrintWriter; -import java.io.StringWriter; - public final class ExceptionUtilities { - public static T shiftStackTrace(T t, int offset) { - StackTraceElement[] stackTrace = t.getStackTrace(); - if (offset < stackTrace.length) { - t.setStackTrace(copyOfRange(stackTrace, offset, stackTrace.length)); - } - return t; - } - public static Throwable getRootCause(Throwable t) { while (t.getCause() != null) { t = t.getCause(); } - return t; } public static T findCause(Throwable t, Class type) { while (t != null) { - if (type.isInstance(t)) + if (type.isInstance(t)) { return type.cast(t); - + } t = t.getCause(); } - return null; } @@ -39,40 +24,13 @@ public final class ExceptionUtilities { } public static String getMessage(Throwable t) { - String message = t.getMessage(); - - if (message == null || message.isEmpty()) { - message = t.toString(); + String m = t.getMessage(); + if (m == null || m.isEmpty()) { + m = t.toString(); } - - return message; + return m; } - public static String getStackTrace(Throwable t) { - StringWriter buffer = new StringWriter(); - t.printStackTrace(new PrintWriter(buffer, true)); - return buffer.getBuffer().toString(); - } - - public static T wrap(Throwable t, Class type) { - if (type.isInstance(t)) { - return type.cast(t); - } - - try { - return type.getConstructor(Throwable.class).newInstance(t); - } catch (Exception e) { - throw new IllegalArgumentException(e); - } - } - - public static RuntimeException asRuntimeException(Throwable t) { - return wrap(t, RuntimeException.class); - } - - /** - * Dummy constructor to prevent instantiation. - */ private ExceptionUtilities() { throw new UnsupportedOperationException(); } diff --git a/source/net/filebot/util/PreferencesMap.java b/source/net/filebot/util/PreferencesMap.java index 4559862f..50c0b431 100644 --- a/source/net/filebot/util/PreferencesMap.java +++ b/source/net/filebot/util/PreferencesMap.java @@ -9,7 +9,6 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -202,9 +201,6 @@ public class PreferencesMap implements Map { if (value != null) { try { return constructor.newInstance(value); - } catch (InvocationTargetException e) { - // try to throw the cause directly, e.g. NumberFormatException - throw ExceptionUtilities.asRuntimeException(e.getCause()); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/source/net/filebot/util/ui/SimpleLabelProvider.java b/source/net/filebot/util/ui/SimpleLabelProvider.java index e16f3896..56b981d4 100644 --- a/source/net/filebot/util/ui/SimpleLabelProvider.java +++ b/source/net/filebot/util/ui/SimpleLabelProvider.java @@ -1,15 +1,11 @@ package net.filebot.util.ui; - import java.lang.reflect.Method; import java.util.Arrays; import javax.swing.Icon; -import net.filebot.util.ExceptionUtilities; - - /** * LabelProvider based on reflection. */ @@ -18,7 +14,6 @@ public class SimpleLabelProvider implements LabelProvider { private final Method getIconMethod; private final Method getTextMethod; - /** * Factory method for {@link #SimpleLabelProvider(Class)}. * @@ -28,33 +23,32 @@ public class SimpleLabelProvider implements LabelProvider { return new SimpleLabelProvider(type); } - /** - * Create a new LabelProvider which will use the getText, getName or toString - * method for text and the getIcon method for the - * icon. + * Create a new LabelProvider which will use the getText, getName or toString method for text and the getIcon method for the icon. * - * @param type a class that has one of the text methods and the icon method + * @param type + * a class that has one of the text methods and the icon method */ public SimpleLabelProvider(Class type) { getTextMethod = findAnyMethod(type, "getText", "getName", "toString"); getIconMethod = findAnyMethod(type, "getIcon"); } - /** * Create a new LabelProvider which will use a specified method of a given class * - * @param type a class with the specified method - * @param getText a method name such as getText - * @param getIcon a method name such as getIcon + * @param type + * a class with the specified method + * @param getText + * a method name such as getText + * @param getIcon + * a method name such as getIcon */ public SimpleLabelProvider(Class type, String getText, String getIcon) { getTextMethod = findAnyMethod(type, getText); getIconMethod = findAnyMethod(type, getIcon); } - private Method findAnyMethod(Class type, String... names) { for (String name : names) { try { @@ -67,23 +61,21 @@ public class SimpleLabelProvider implements LabelProvider { throw new IllegalArgumentException("Method not found: " + Arrays.toString(names)); } - @Override public String getText(T value) { try { return (String) getTextMethod.invoke(value); } catch (Exception e) { - throw ExceptionUtilities.asRuntimeException(e); + throw new RuntimeException(e); } } - @Override public Icon getIcon(T value) { try { return (Icon) getIconMethod.invoke(value); } catch (Exception e) { - throw ExceptionUtilities.asRuntimeException(e); + throw new RuntimeException(e); } }