I am building something like a data flow graph, with nodes and connections that pass data between them. The base class in this case is
, which has a previous and a next target to pass data back and forth. Other classes extend this class to provide sources for the data, merge data (e.g. a multiplication) etc.ValueTarget<T>
Now, I wanted to write a data source that takes its value from any given method. It takes a java.lang.reflect.Method
instance (and the Object and parameters to invoke it) and uses this to set the data:
@Override
public T getValue() {
if (valueCalculator != null) {
try {
Object result = valueCalculator.invoke(sourceObject);
T typedResult = (T)(result);
return typedResult;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return super.getValue();
}
Now, I have tried this with the public int getLength()
Method of another object. However, in the graph, I need to map this method to a float
result. The given typecast doesn't throw an Exception, but when reading the last
's value and using its result as a ValueTarget<Float>
float
parameter to another method, the program crashes, stating that "java.lang.Integer cannot be cast to java.lang.Float".
Now, I am aware that Integer and Float are boxed and therefore cannot be cast into each other's type. However, as the types are all handled using the generics, I cannot unbox to a specific type.
Is there any way to perform a generic unboxing in order to be able to cast boxed values into the according value-types?