javaoop

Why isn't the condition Integer.class.isAssignableFrom(getGenericClass()) working correctly in my Java code?


I'm working on a generic class in Java that stores an object of type T, and I need to check the type of the object to handle null values differently for Integer and String. However, when I try to check the class type with isAssignableFrom, the condition doesn't seem to trigger as expected. Specifically, I'm trying to return default values (0 for Integer and "default" for String) when the stored value is null. But for some reason, my check for Integer.class.isAssignableFrom(getGenericClass()) doesn't work, and it always falls through to returning null.

Here's the relevant code:

// General class for storage
public class Storage<T> {
    private final T item; // Stored object

    // Constructor for storage
    public Storage(T item) {
        this.item = item;
    }

    // Method to get the object from storage
    public T get() {
        if (item == null) {
            // Logic for null values
            if (Integer.class.isAssignableFrom(getGenericClass())) {
                return (T) Integer.valueOf(0); // Default for integers
            } else if (String.class.isAssignableFrom(getGenericClass())) {
                return (T) "default"; // Default for strings
            }
            // Return null if the type is not recognized
            return null;
        }

        return item; // Return the item if it's not null
    }

    // Helper method to determine the class type
    private Class<?> getGenericClass() {
        return item != null ? item.getClass() : Object.class;
    }
}

In this example, if I pass null to the storage, I expect the code to return different output based on type , but Integer.class.isAssignableFrom(getGenericClass()) doesn't trigger when the type is Integer, and it always returns null.

Expected output:

If the type is Integer, I want to return 0 when the value is null. If the type is String, I want to return "default" when the value is null. Actual output:

The output is null in both cases, even though the value is null.

How can I correctly compare the class type with Integer or String? Is isAssignableFrom not the right approach in this case, and if not, what is the correct way to compare the class type in this scenario?

Also tried

    if (item == null) {
        if (Integer.class.equals(getGenericClass())) {
            return (T) Integer.valueOf(0); // Alternative for numbers
        } else if (String.class.equals(getGenericClass())) {
            return (T) "default"; // Alternative for strings
        }
        return null; // Return null for unknown types
    }

Solution

  • How can I correctly compare the class type with Integer or String? Is isAssignableFrom not the right approach in this case, and if not, what is the correct way to compare the class type in this scenario?

    You can't. If Storage<T> is constructed with null, then there is no way whatsoever to retrieve T, or tell if it's Integer or String or whatever.

    private Class<?> getGenericClass() {
        return item != null ? item.getClass() : Object.class;
    }
    

    This of course returns Object.class if item is null, so you certainly can't get Integer.class or String.class from Object.class.