Check whether a String value is convertible to an instance of a Wrapper class given as Class object?

This is the follow up of my answer to this Stack Overflow question. The question goes like this:

Suppose we have a String and a Class<?> object like this:

String string = "true";
Class<?> clazz = Integer.class;

How do we find that the value in string is convertible to an instance of class given by Class<?> clazz or not?

Assumption here is that, we are dealing with wrapper classes. It wouldn’t be that easy(if only possible) to do this for any normal class. Now, considering the fact that, every wrapper class has a method – valueOf(String), that converts a string value to an instance of that wrapper class, we can use some reflection hack to get around with this.

Here’s the working code:

public class ReflectionTest {

    public static void main(String... args) throws InvocationTargetException, IllegalAccessException {

        String string = "24";
        Class<?> clazz = Integer.class;

        Method method = null;

        try {
            method = clazz.getMethod("valueOf", String.class);
        } catch (NoSuchMethodException | SecurityException e) {

        if (method != null) {
            try {
                Object obj = method.invoke(null, string);
                System.out.println("String value converted to " +
                                   clazz.getSimpleName() +
                                   " instance: " + obj);

            } catch (IllegalArgumentException ex) {
                System.out.println("Failure : " + string +
                                   " is not of type " +

Assuming that there is no SecurityManager to prevent you from running this code, it will print:

String value converted to Integer instance: 24

So, what is that code actually doing? We’ll understand it step by step in the following paragraph.

We already have the Class object for Integer.class class. If you look at the Integer class, or any other wrapper classes, they have a static method – valueOf(String), which creates that wrapper class instance from passed String, and throws NumberFormatException if the string cannot be converted to the wrapper class instance.

We then use Class#getMethod(String, Class<?>...) method to get a Method instance for valueOf method in the following part:

method = clazz.getMethod("valueOf", String.class);

After we have got the Method instance, we try to invoke the method using Method#invoke(Object, Object...) method, passing null as first argument (To invoke a static method, we pass null as first argument), and the given String as second argument. This is done in following part of the code:

Object obj = method.invoke(null, string);

If the method invocation is successful, and the string value can be converted to an Integer, the next statement in the try block is executed, and the success message is printed. Else, if any exception is thrown, the error message in the catch block is printed.

The above code will pass, as the string "24" is parseable to Integer instance. You can try out the code giving an invalid integer value in String, and see if exception is thrown or not.

Final Note: An important point to note here is that for Boolean wrapper, any value other than true will give you a Boolean.FALSE instance. Even the null argument will give false. The Boolean#valueOf(String) method doesn’t throw any exception.


One Response to Check whether a String value is convertible to an instance of a Wrapper class given as Class object?

Leave a Reply


You are commenting using your account. Log Out / Change )

Twitter picture


Facebook photo


Google+ photo


Connecting to %s

%d bloggers like this: