Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a default value to a generic

I'm calling a method by reflection, and its return type is generic. I don't want the return value to be null, so I in that case I want to assign a default value of that generic type.

That is, after calling the method by reflection, I'd like to execute something like this:

T resultNotNull = (T) reflectionMethodThatCanReturnNull.invoke(anObject);
// If it's null, let's assign something assignable.
if (resultNotNull == null) {
    if (resultNotNull.getClass().isAssignableFrom(Long.class)) {
        resultNotNull = (T) Long.valueOf(-1);
    } else if (resultNotNull.getClass().isAssignableFrom(String.class)) {
        resultNotNull = (T) "NOTHING";
    } 
}

But, of course, if 'resultNotNull == null', we can't call 'resultNotNull.getClass()' without getting an exception.

Any ideas on how to assign a default value depending on the generic type?

The whole code would be something like this:

public class ReflectionTest {
    public class Class1ok {
        public Long method () { return Long.valueOf(1); }
    }   
    public class Class1null {
        public Long method () { return null; } // It returns null
    }   
    public class Class2ok {
        public String method () { return "SOMETHING"; }
    }

    public static void main(String[] args) {
        ReflectionTest test = new ReflectionTest();

        Long notNullValue1 
            = test.methodReturnNotNull(Class1ok.class); // 1
        Long notNullValue2 
            = test.methodReturnNotNull(Class1null.class); // -1, not null
        String notNullValue3 
            = test.methodReturnNotNull(Class2ok.class); // "SOMETHING"
    }

    public <T> T methodReturnNotNull(Class theClass) {
        T resultNotNull = null;
        try {
            Object theObject = theClass.newInstance();
            Method methodThatCanReturnNull = theClass.getMethod("method");
            resultNotNull = (T) methodThatCanReturnNull.invoke(theObject);
        } catch (Exception e) {
            // Meh.
        }

        // If it's null, assign something assignable.
        if (resultNotNull == null) {
            if (resultNotNull.getClass().isAssignableFrom(Long.class)) {
                resultNotNull = (T) Long.valueOf(-1);
            } else if (resultNotNull.getClass().isAssignableFrom(String.class)) {
                resultNotNull = (T) "NOTHING";
            } 
        }
        return resultNotNull;
    }
}
like image 557
J.A.I.L. Avatar asked Feb 06 '12 15:02

J.A.I.L.


Video Answer


1 Answers

You can use getReturnType();

private static final Map<Class, Object> replacementForNull = new HashMap<>();
static {
    replacementForNull.put(String.class, "NOTHING");
    replacementForNull.put(Long.class, 1L);
}


Method reflectionMethodThatCanReturnNull = ...
T resultNotNull = (T) reflectionMethodThatCanReturnNull.invoke(anObject);
// If it's null, let's assign something assignable.
if (resultNotNull == null) {
    Class returnType = reflectionMethodThatCanReturnNull.getReturnType();
    resultNotNull = replacementForNull.get(returnType);
}
like image 146
Peter Lawrey Avatar answered Oct 12 '22 23:10

Peter Lawrey