Skip to content

Commit fdc1335

Browse files
committed
Types: factor out common exception throwing logic
1 parent 5b2c5c7 commit fdc1335

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/main/java/org/scijava/util/Types.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public static Class<?> load(final String name, final ClassLoader classLoader,
214214
// Not NoClassDefFoundError.
215215
// Not UnsupportedClassVersionError!
216216
if (quietly) return null;
217-
throw new IllegalArgumentException("Cannot load class: " + className, t);
217+
throw iae(t, "Cannot load class: " + className);
218218
}
219219
}
220220

@@ -495,7 +495,7 @@ public static <T> T nullValue(final Class<T> type) {
495495
* method with the given name
496496
*/
497497
public static Field field(final Class<?> c, final String name) {
498-
if (c == null) throw new IllegalArgumentException("No such field: " + name);
498+
if (c == null) throw iae("No such field: " + name);
499499
try {
500500
return c.getDeclaredField(name);
501501
}
@@ -533,7 +533,7 @@ public static Class<?> array(final Class<?> componentType) {
533533
* @param dim The dimensionality of the array
534534
*/
535535
public static Class<?> array(final Class<?> componentType, final int dim) {
536-
if (dim < 0) throw new IllegalArgumentException("Negative dimension");
536+
if (dim < 0) throw iae("Negative dimension");
537537
if (dim == 0) return componentType;
538538
return array(array(componentType), dim - 1);
539539
}
@@ -700,9 +700,7 @@ public static <T> T cast(final Object src, final Class<T> dest) {
700700
* has no such constant.
701701
*/
702702
public static <T> T enumValue(final String name, final Class<T> dest) {
703-
if (!dest.isEnum()) {
704-
throw new IllegalArgumentException("Not an enum type: " + name(dest));
705-
}
703+
if (!dest.isEnum()) throw iae("Not an enum type: " + name(dest));
706704
@SuppressWarnings({ "rawtypes", "unchecked" })
707705
final Enum result = Enum.valueOf((Class) dest, name);
708706
@SuppressWarnings("unchecked")
@@ -878,6 +876,19 @@ public static final ParameterizedType parameterize(final Class<?> raw,
878876

879877
// -- Helper methods --
880878

879+
private static IllegalArgumentException iae(final String... s) {
880+
return iae(null, s);
881+
}
882+
883+
private static IllegalArgumentException iae(final Throwable cause,
884+
final String... notes)
885+
{
886+
final String s = String.join(", ", notes);
887+
final IllegalArgumentException exc = new IllegalArgumentException(s);
888+
if (cause != null) exc.initCause(cause);
889+
throw exc;
890+
}
891+
881892
private static Class<?> arrayOrNull(final Class<?> componentType) {
882893
try {
883894
return Types.array(componentType);

0 commit comments

Comments
 (0)