2929
3030package org .scijava .common3 ;
3131
32- import static org .scijava .common3 .Exceptions .iae ;
33-
3432import java .lang .reflect .Array ;
3533import java .lang .reflect .Field ;
3634import java .lang .reflect .Method ;
@@ -183,7 +181,7 @@ public static Class<?> load(final String name, final ClassLoader classLoader,
183181 // Not NoClassDefFoundError.
184182 // Not UnsupportedClassVersionError!
185183 if (quietly ) return null ;
186- throw iae (t , "Cannot load class: " + className );
184+ throw iae (t , "Cannot load class: %s" , className );
187185 }
188186 }
189187
@@ -250,15 +248,15 @@ public static URL location(final Class<?> c, final boolean quietly) {
250248 if (classResource == null ) {
251249 // cannot find class resource
252250 if (quietly ) return null ;
253- throw iae (cause , "No class resource for class: " + c .getName (), why );
251+ throw iae (cause , "No class resource for class: %s (%s)" , c .getName (), why );
254252 }
255253
256254 final String url = classResource .toString ();
257255 final String suffix = c .getCanonicalName ().replace ('.' , '/' ) + ".class" ;
258256 if (!url .endsWith (suffix )) {
259257 // weird URL
260258 if (quietly ) return null ;
261- throw iae (cause , "Unsupported URL format: " + url , why );
259+ throw iae (cause , "Unsupported URL format: %s (%s)" , url , why );
262260 }
263261
264262 // strip the class's path from the URL string
@@ -274,7 +272,7 @@ public static URL location(final Class<?> c, final boolean quietly) {
274272 }
275273 catch (final MalformedURLException e ) {
276274 if (quietly ) return null ;
277- throw iae (e , "Malformed URL" , why );
275+ throw iae (e , "Malformed URL: %s (%s)" , path , why );
278276 }
279277 }
280278
@@ -436,7 +434,7 @@ public static <T> T nullValue(final Class<T> type) {
436434 * field with the given name
437435 */
438436 public static Field field (final Class <?> c , final String name ) {
439- if (c == null ) throw iae ("No such field: " + name );
437+ if (c == null ) throw iae (null , "No such field: %s" , name );
440438 try {
441439 return c .getDeclaredField (name );
442440 }
@@ -473,7 +471,7 @@ public static Field field(final Class<?> c, final String name) {
473471 public static Method method (final Class <?> c , final String name ,
474472 final Class <?>... parameterTypes )
475473 {
476- if (c == null ) throw iae ("No such field: " + name );
474+ if (c == null ) throw iae (null , "No such field: %s" , name );
477475 try {
478476 return c .getDeclaredMethod (name , parameterTypes );
479477 }
@@ -514,7 +512,7 @@ public static Class<?> array(final Class<?> componentType) {
514512 * @param dim The dimensionality of the array
515513 */
516514 public static Class <?> array (final Class <?> componentType , final int dim ) {
517- if (dim < 0 ) throw iae ("Negative dimension" );
515+ if (dim < 0 ) throw iae (null , "Negative dimension" );
518516 if (dim == 0 ) return componentType ;
519517 return array (array (componentType ), dim - 1 );
520518 }
@@ -551,4 +549,17 @@ private static Class<?> arrayOrNull(final Class<?> componentType) {
551549 return null ;
552550 }
553551 }
552+
553+ /**
554+ * Creates a new {@link IllegalArgumentException} with the given cause and
555+ * formatted message string.
556+ */
557+ private static IllegalArgumentException iae (final Throwable cause ,
558+ final String formattedMessage , final String ... values )
559+ {
560+ final String s = String .format (formattedMessage , values );
561+ final IllegalArgumentException exc = new IllegalArgumentException (s );
562+ if (cause != null ) exc .initCause (cause );
563+ return exc ;
564+ }
554565}
0 commit comments