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.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());
}
}

View File

@ -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 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) {
while (t.getCause() != null) {
t = t.getCause();
}
return t;
}
public static <T extends Throwable> T findCause(Throwable t, Class<T> 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 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() {
throw new UnsupportedOperationException();
}

View File

@ -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<T> implements Map<String, T> {
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);
}

View File

@ -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;
/**
* <code>LabelProvider</code> based on reflection.
*/
@ -18,7 +14,6 @@ public class SimpleLabelProvider<T> implements LabelProvider<T> {
private final Method getIconMethod;
private final Method getTextMethod;
/**
* Factory method for {@link #SimpleLabelProvider(Class)}.
*
@ -28,33 +23,32 @@ public class SimpleLabelProvider<T> implements LabelProvider<T> {
return new SimpleLabelProvider<T>(type);
}
/**
* 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.
* 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.
*
* @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) {
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 <code>getText</code>
* @param getIcon a method name such as <code>getIcon</code>
* @param type
* a class with the specified method
* @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) {
getTextMethod = findAnyMethod(type, getText);
getIconMethod = findAnyMethod(type, getIcon);
}
private Method findAnyMethod(Class<T> type, String... names) {
for (String name : names) {
try {
@ -67,23 +61,21 @@ public class SimpleLabelProvider<T> implements LabelProvider<T> {
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);
}
}