@@ -93,11 +93,48 @@ private ClassUtils() {
9393 * Loads the class with the given name, using the current thread's context
9494 * class loader, or null if it cannot be loaded.
9595 *
96+ * @param name The name of the class to load.
9697 * @return The loaded class, or null if the class could not be loaded.
97- * @see #loadClass(String, ClassLoader)
98+ * @see #loadClass(String, ClassLoader, boolean )
9899 */
99- public static Class <?> loadClass (final String className ) {
100- return loadClass (className , null );
100+ public static Class <?> loadClass (final String name ) {
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 );
101138 }
102139
103140 /**
@@ -119,10 +156,16 @@ public static Class<?> loadClass(final String className) {
119156 * @param name The name of the class to load.
120157 * @param classLoader The class loader with which to load the class; if null,
121158 * the current thread's context class loader will be used.
122- * @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.
123166 */
124167 public static Class <?> loadClass (final String name ,
125- final ClassLoader classLoader )
168+ final ClassLoader classLoader , final boolean quietly )
126169 {
127170 // handle primitive types
128171 if (name .equals ("Z" ) || name .equals ("boolean" )) return boolean .class ;
@@ -170,7 +213,8 @@ public static Class<?> loadClass(final String name,
170213 // Not ClassNotFoundException.
171214 // Not NoClassDefFoundError.
172215 // Not UnsupportedClassVersionError!
173- return null ;
216+ if (quietly ) return null ;
217+ throw new IllegalArgumentException ("Cannot load class: " + className , t );
174218 }
175219 }
176220
0 commit comments