Assigning a default value to general

I call the method by reflection, and its return type is generic. I do not want the return value to be null, so in this case I want to assign a default value to this generic type .

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

T resultNotNull = (T) reflectionMethodThatCanReturnNull.invoke(anObject); // If it null, let 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 cannot call 'resultNotNull.getClass ()' without getting an exception.

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

All code will look 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 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; } } 
+6
source share
2 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 null, let assign something assignable. if (resultNotNull == null) { Class returnType = reflectionMethodThatCanReturnNull.getReturnType(); resultNotNull = replacementForNull.get(returnType); } 
+3
source

Why was your T unable to implement any interface or extend the base class? You can then call the default static method from T , which returns the default value for the given class T Pros? It will encapsulate your switch code only in the right class.

Of course, without any interfaces, your entire possible class T could have some method, for example getDefaultValue() .

0
source

Source: https://habr.com/ru/post/907777/


All Articles