Skip to content

Commit 7f26835

Browse files
committed
ClassUtils: throw exceptions in loadClass methods
We keep the old behavior of returning null, both for convenience and for backwards compatibility. But we add a "boolean quietly" flag which, when set to false, makes the methods throw IllegalArgumentException instead. This avoids problems where downstream code needs to know _why_ a class couldn't be loaded, but the exception was eaten in the loadClass method.
1 parent 684697e commit 7f26835

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,46 @@ private ClassUtils() {
9595
*
9696
* @param name The name of the class to load.
9797
* @return The loaded class, or null if the class could not be loaded.
98-
* @see #loadClass(String, ClassLoader)
98+
* @see #loadClass(String, ClassLoader, boolean)
9999
*/
100100
public static Class<?> loadClass(final String name) {
101-
return loadClass(name, null);
101+
return loadClass(name, null, true);
102+
}
103+
104+
/**
105+
* Loads the class with the given name, using the specified
106+
* {@link ClassLoader}, or null if it cannot be loaded.
107+
*
108+
* @param name The name of the class to load.
109+
* @param classLoader The class loader with which to load the class; if null,
110+
* the current thread's context class loader will be used.
111+
* @return The loaded class, or null if the class could not be loaded.
112+
* @see #loadClass(String, ClassLoader, boolean)
113+
*/
114+
public static Class<?> loadClass(final String name,
115+
final ClassLoader classLoader)
116+
{
117+
return loadClass(name, classLoader, true);
118+
}
119+
120+
/**
121+
* Loads the class with the given name, using the current thread's context
122+
* class loader.
123+
*
124+
* @param className the name of the class to load
125+
* @param quietly Whether to return {@code null} (rather than throwing
126+
* {@link IllegalArgumentException}) if something goes wrong loading
127+
* the class
128+
* @return The loaded class, or {@code null} if the class could not be loaded
129+
* and the {@code quietly} flag is set.
130+
* @see #loadClass(String, ClassLoader, boolean)
131+
* @throws IllegalArgumentException If the class cannot be loaded and the
132+
* {@code quietly} flag is not set.
133+
*/
134+
public static Class<?> loadClass(final String className,
135+
final boolean quietly)
136+
{
137+
return loadClass(className, null, quietly);
102138
}
103139

104140
/**
@@ -120,10 +156,16 @@ public static Class<?> loadClass(final String name) {
120156
* @param name The name of the class to load.
121157
* @param classLoader The class loader with which to load the class; if null,
122158
* the current thread's context class loader will be used.
123-
* @return The loaded class, or null if the class could not be loaded.
159+
* @param quietly Whether to return {@code null} (rather than throwing
160+
* {@link IllegalArgumentException}) if something goes wrong loading
161+
* the class
162+
* @return The loaded class, or {@code null} if the class could not be loaded
163+
* and the {@code quietly} flag is set.
164+
* @throws IllegalArgumentException If the class cannot be loaded and the
165+
* {@code quietly} flag is not set.
124166
*/
125167
public static Class<?> loadClass(final String name,
126-
final ClassLoader classLoader)
168+
final ClassLoader classLoader, final boolean quietly)
127169
{
128170
// handle primitive types
129171
if (name.equals("Z") || name.equals("boolean")) return boolean.class;
@@ -171,7 +213,8 @@ public static Class<?> loadClass(final String name,
171213
// Not ClassNotFoundException.
172214
// Not NoClassDefFoundError.
173215
// Not UnsupportedClassVersionError!
174-
return null;
216+
if (quietly) return null;
217+
throw new IllegalArgumentException("Cannot load class: " + className, t);
175218
}
176219
}
177220

0 commit comments

Comments
 (0)