为泛型分配默认值
我通过反射调用一个方法,它的返回类型是通用的。我不希望返回值为 null,因此在这种情况下我想分配该泛型类型的默认值。
也就是说,通过反射调用方法之后,我想执行如下操作:
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";
}
}
但是,当然,如果 'resultNotNull == null',我们不能调用 'resultNotNull.getClass( )' 没有得到异常。
关于如何根据泛型类型分配默认值有什么想法吗?
整个代码将是这样的:
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 getReturnType();
You can use getReturnType();
为什么您的
T
无法实现某些接口或扩展某些基类?然后,您可以从T
调用一些静态默认方法,该方法返回给定类T
的默认值。优点?它将把您的switch
代码封装到正确的类中。当然,如果没有任何接口,所有可能的
T
类都可以只具有像getDefaultValue()
这样的方法。Why your
T
could not implements some interface or extend some base class? Then you could call some static default method fromT
which return default value for given classT
. Pros? It will encapsulate yourswitch
code just to right class.Of course, without any interfaces, all your possible
T
class could just have some method likegetDefaultValue()
.