This commit is contained in:
Reinhard Pointner 2016-03-27 20:40:27 +00:00
parent 8d35a67d71
commit 21f8c121c2
4 changed files with 18 additions and 73 deletions

View File

@ -15,7 +15,6 @@ import java.util.prefs.Preferences;
import net.filebot.UserFiles.FileChooser; import net.filebot.UserFiles.FileChooser;
import net.filebot.archive.Archive.Extractor; import net.filebot.archive.Archive.Extractor;
import net.filebot.cli.ArgumentBean; import net.filebot.cli.ArgumentBean;
import net.filebot.util.ExceptionUtilities;
import net.filebot.util.PreferencesList; import net.filebot.util.PreferencesList;
import net.filebot.util.PreferencesMap; import net.filebot.util.PreferencesMap;
import net.filebot.util.PreferencesMap.PreferencesEntry; import net.filebot.util.PreferencesMap.PreferencesEntry;
@ -315,7 +314,7 @@ public final class Settings {
// remove entries // remove entries
prefs.clear(); prefs.clear();
} catch (BackingStoreException e) { } catch (BackingStoreException e) {
throw ExceptionUtilities.asRuntimeException(e); debug.warning(e.getMessage());
} }
} }

View File

@ -1,36 +1,21 @@
package net.filebot.util; package net.filebot.util;
import static java.util.Arrays.*;
import java.io.PrintWriter;
import java.io.StringWriter;
public final class ExceptionUtilities { public final class ExceptionUtilities {
public static <T extends Throwable> 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) { public static Throwable getRootCause(Throwable t) {
while (t.getCause() != null) { while (t.getCause() != null) {
t = t.getCause(); t = t.getCause();
} }
return t; return t;
} }
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) { public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
while (t != null) { while (t != null) {
if (type.isInstance(t)) if (type.isInstance(t)) {
return type.cast(t); return type.cast(t);
}
t = t.getCause(); t = t.getCause();
} }
return null; return null;
} }
@ -39,40 +24,13 @@ public final class ExceptionUtilities {
} }
public static String getMessage(Throwable t) { public static String getMessage(Throwable t) {
String message = t.getMessage(); String m = t.getMessage();
if (m == null || m.isEmpty()) {
if (message == null || message.isEmpty()) { m = t.toString();
message = t.toString();
} }
return m;
return message;
} }
public static String getStackTrace(Throwable t) {
StringWriter buffer = new StringWriter();
t.printStackTrace(new PrintWriter(buffer, true));
return buffer.getBuffer().toString();
}
public static <T extends Throwable> T wrap(Throwable t, Class<T> 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() { private ExceptionUtilities() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@ -9,7 +9,6 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -202,9 +201,6 @@ public class PreferencesMap<T> implements Map<String, T> {
if (value != null) { if (value != null) {
try { try {
return constructor.newInstance(value); return constructor.newInstance(value);
} catch (InvocationTargetException e) {
// try to throw the cause directly, e.g. NumberFormatException
throw ExceptionUtilities.asRuntimeException(e.getCause());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -1,15 +1,11 @@
package net.filebot.util.ui; package net.filebot.util.ui;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import javax.swing.Icon; import javax.swing.Icon;
import net.filebot.util.ExceptionUtilities;
/** /**
* <code>LabelProvider</code> based on reflection. * <code>LabelProvider</code> based on reflection.
*/ */
@ -18,7 +14,6 @@ public class SimpleLabelProvider<T> implements LabelProvider<T> {
private final Method getIconMethod; private final Method getIconMethod;
private final Method getTextMethod; private final Method getTextMethod;
/** /**
* Factory method for {@link #SimpleLabelProvider(Class)}. * Factory method for {@link #SimpleLabelProvider(Class)}.
* *
@ -28,33 +23,32 @@ public class SimpleLabelProvider<T> implements LabelProvider<T> {
return new SimpleLabelProvider<T>(type); return new SimpleLabelProvider<T>(type);
} }
/** /**
* Create a new LabelProvider which will use the <code>getText</code>, <code>getName</code> or <code>toString</code> * Create a new LabelProvider which will use the <code>getText</code>, <code>getName</code> or <code>toString</code> method for text and the <code>getIcon</code> method for the icon.
* method for text and the <code>getIcon</code> 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<T> type) { public SimpleLabelProvider(Class<T> type) {
getTextMethod = findAnyMethod(type, "getText", "getName", "toString"); getTextMethod = findAnyMethod(type, "getText", "getName", "toString");
getIconMethod = findAnyMethod(type, "getIcon"); getIconMethod = findAnyMethod(type, "getIcon");
} }
/** /**
* Create a new LabelProvider which will use a specified method of a given class * Create a new LabelProvider which will use a specified method of a given class
* *
* @param type a class with the specified method * @param type
* @param getText a method name such as <code>getText</code> * a class with the specified method
* @param getIcon a method name such as <code>getIcon</code> * @param getText
* a method name such as <code>getText</code>
* @param getIcon
* a method name such as <code>getIcon</code>
*/ */
public SimpleLabelProvider(Class<T> type, String getText, String getIcon) { public SimpleLabelProvider(Class<T> type, String getText, String getIcon) {
getTextMethod = findAnyMethod(type, getText); getTextMethod = findAnyMethod(type, getText);
getIconMethod = findAnyMethod(type, getIcon); getIconMethod = findAnyMethod(type, getIcon);
} }
private Method findAnyMethod(Class<T> type, String... names) { private Method findAnyMethod(Class<T> type, String... names) {
for (String name : names) { for (String name : names) {
try { try {
@ -67,23 +61,21 @@ public class SimpleLabelProvider<T> implements LabelProvider<T> {
throw new IllegalArgumentException("Method not found: " + Arrays.toString(names)); throw new IllegalArgumentException("Method not found: " + Arrays.toString(names));
} }
@Override @Override
public String getText(T value) { public String getText(T value) {
try { try {
return (String) getTextMethod.invoke(value); return (String) getTextMethod.invoke(value);
} catch (Exception e) { } catch (Exception e) {
throw ExceptionUtilities.asRuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public Icon getIcon(T value) { public Icon getIcon(T value) {
try { try {
return (Icon) getIconMethod.invoke(value); return (Icon) getIconMethod.invoke(value);
} catch (Exception e) { } catch (Exception e) {
throw ExceptionUtilities.asRuntimeException(e); throw new RuntimeException(e);
} }
} }