You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/01/27 17:29:50 UTC
svn commit: r1439112 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/ConfigurationUtils.java
test/java/org/apache/commons/configuration/TestConfigurationUtils.java
Author: oheger
Date: Sun Jan 27 16:29:50 2013
New Revision: 1439112
URL: http://svn.apache.org/viewvc?rev=1439112&view=rev
Log:
Added a convenience method to ConfigurationUtils for loading a class without throwing a ClassNotFoundException.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=1439112&r1=1439111&r2=1439112&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java Sun Jan 27 16:29:50 2013
@@ -808,6 +808,7 @@ public final class ConfigurationUtils
* @param clsName the name of the class to be loaded
* @return the loaded class
* @throws ClassNotFoundException if the class cannot be resolved
+ * @since 2.0
*/
public static Class<?> loadClass(String clsName)
throws ClassNotFoundException
@@ -835,6 +836,30 @@ public final class ConfigurationUtils
}
/**
+ * Loads the class with the specified name re-throwing
+ * {@code ClassNotFoundException} exceptions as runtime exceptions. This
+ * method works like {@link #loadClass(String)}. However, checked exceptions
+ * are caught and re-thrown as {@code ConfigurationRuntimeException}.
+ *
+ * @param clsName the name of the class to be loaded
+ * @return the loaded class
+ * @throws ConfigurationRuntimeException if the class cannot be resolved
+ * @since 2.0
+ */
+ public static Class<?> loadClassNoEx(String clsName)
+ {
+ try
+ {
+ return loadClass(clsName);
+ }
+ catch (ClassNotFoundException cnfex)
+ {
+ throw new ConfigurationRuntimeException("Cannot load class "
+ + clsName, cnfex);
+ }
+ }
+
+ /**
* Creates an {@code ImmutableConfiguration} from the given
* {@code Configuration} object. This method creates a proxy object wrapping
* the original configuration and making it available under the
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java?rev=1439112&r1=1439111&r2=1439112&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java Sun Jan 27 16:29:50 2013
@@ -610,6 +610,25 @@ public class TestConfigurationUtils
}
/**
+ * Tests loadClassNoEx() if the class can be resolved.
+ */
+ @Test
+ public void testLoadClassNoExFound()
+ {
+ assertEquals("Wrong class", CLS_NAME,
+ ConfigurationUtils.loadClassNoEx(CLS_NAME).getName());
+ }
+
+ /**
+ * Tests loadClassNoEx() if the class cannot be resolved.
+ */
+ @Test(expected = ConfigurationRuntimeException.class)
+ public void testLoadClassNoExNotFound()
+ {
+ ConfigurationUtils.loadClassNoEx("a non existing class!");
+ }
+
+ /**
* Tests asEventSource() if the passed in object implements this interface.
*/
@Test