用户通过配置支持 userType 和 userFunction:
{"userType": "com.user.Person", "userFunction": "com.user.userFunction"}
我们希望获取用户数据并将数据提供给 userFunction:
public class DataConsumer<T> {
//get data from some place, such as kafka
T data;
}
process() {
String userType = userInput();
DataConsumer<userType> dataConsumer = new DataConsumer<userType>(); // error because generic class's type name don't support String or Class
userFunction(dataConsumer.data); // We get this function by reflection
}
// This is ok in the user side
userFunction(userType data) {
data;
}
我怎样才能在没有错误的情况下实现这个功能?如果不可能,还有其他方法吗?
最后,我使用 Object 来代表所有类型名称。但用户必须手动将 Object 类型转换为 userType。有没有更好的办法?
{
String userType = userInput();
DataConsumer<Object> dataConsumer = new DataConsumer<Object>(); // ok
userFunction(dataConsumer.data);
}
userFunction(Object data) {
(userType)data;
}