Java Utililty Methods Class Load

List of utility methods to do Class Load

Description

The list of methods to do Class Load are organized into topic(s).

Method

ClassclassForName(String typeName)
class For Name
try {
    return Class.forName(typeName);
} catch (ClassNotFoundException e) {
    return null;
ClassclassForName(String typeName)
class For Name
if (typeName.equals("boolean"))
    return boolean.class;
if (typeName.equals("byte"))
    return byte.class;
if (typeName.equals("char"))
    return char.class;
if (typeName.equals("short"))
    return short.class;
...
ClassclassForNameNoThrow(String className)
class For Name No Throw
try {
    return Class.forName(className);
} catch (Throwable ex) {
return Object.class;
ClassclassForNameOrNull(final String className)
Call Class.forName(className), but return null if any exception is thrown.
try {
    return Class.forName(className);
} catch (final Exception e) {
    return null;
ClassclassForNameOrPrimitive(String name, ClassLoader loader)
class For Name Or Primitive
if (name == null || name.equals("") || name.equals("null") || name.equals("void"))
    return Void.TYPE;
if (name.equals("boolean"))
    return Boolean.TYPE;
if (name.equals("byte"))
    return Byte.TYPE;
if (name.equals("char"))
    return Character.TYPE;
...
ObjectfromString(Class clazz, String stringValue)
from String
if (String.class == clazz) {
    return stringValue;
} else if (Long.class == clazz) {
    return Long.valueOf(stringValue);
throw new IllegalArgumentException(
        "Instantiating " + clazz + " from string representation is not supported. Most likely an"
                + " incompatible version of google-http-client was used.");
...
TfromString(Class clazz, String name)
from String
try {
    return Enum.valueOf(clazz, name);
} catch (IllegalArgumentException e) {
    return null;
TfromString(Class clz, String value, T defaultVal)
from String
assert (clz != null);
if (value != null) {
    try {
        return Enum.valueOf(clz, value.trim());
    } catch (IllegalArgumentException ex) {
        assert (false);
return defaultVal;
TfromString(Class enumClass, String s, T defaultValue)
Returns the enum constant that matches the specified string, or the default value if none of the constants matches or if the string is null.
try {
    return Enum.valueOf(enumClass, s);
} catch (IllegalArgumentException e) {
    return defaultValue;
} catch (NullPointerException e) {
    return defaultValue;
TfromString(Class enumType, String text)
from String
if (text != null) {
    for (T value : enumType.getEnumConstants()) {
        if (text.equalsIgnoreCase(value.toString())) {
            return value;
throw new IllegalArgumentException("No constant with text " + text + " found");
...