Reputation: 20475
I am Working on a configuration scenario for a complex distributed OSGi system.
I need to make the following transition from library code to application code on Java 7 (this code below is incorrect):
void someFrameworkMethod(...) {
....
// arg 2 is of type Object
// service is of type SimpleStrongTypedManagedService<?>
// arg 3 is of type Class<?>
updateSimple( service, entry.getValue(), desc.getClazz());
....
}
private <T> void updateSimple( SimpleStrongTypedManagedService<T> service,
T value,
Class<T> clazz) {
...
}
I have access to the clazz
in question. Are there any tricks to get the compiler to "read my mind"(what I am trying to do above) or am I going to have to do reflection / MethodHandle trickery ?
I have decided to just store the MethodHandle
in my library code instead of the Class<?>
in order to keep going. Still interested in a solution though.
Upvotes: 2
Views: 71
Reputation: 20760
The problem you are having is because the method updateSimple
requires all generic types to be T
, this means: any but all equal!
However you give different types for the generic parameters, respectively. ?
(any), Object
and ?
(does not have to be the same as the one before).
So you have two options
<Object>
instead of <?>
.Normally casting is a code smell for bad object oriented design, but in case of 3rd party software you sometimes can't avoid it. (and in some other cases the OO complexity to avoid casting is not worth it).
If the parameters you are getting are really of the types you need, then just cast them. Otherwise adapt your method signature so it accepts a more wide range of parameters.
Upvotes: 1