I'm looking for a similar concept of inferring captured generic types, similar to the following method snippet, however instead for a class that captures generic types:
public <X, Y, Z> static void someMethod(ObjectInterface<X, Y, Z> object) {
// code that uses inferred generic type parameters X, Y and Z...
}
The code in this snippet will capture types and assign them to the generic parameter types X
, Y
and Z
. This allows the use of the generic type variables inside the body of the code and makes the method more flexible in use. In this snippet, if the method is called without specifying the types (i.e. not parameterized), then Java will infer the types, that is someMethod(instaceOfImplementedObject)
will work and the types will be inferred.
My problem is, I have the following (simplified) structure for an object interface and objects that implement the interface:
public interface ObjectInterface<X, Y, Z> {
//...
}
class ImplementedObject implements ObjectInterface<SomeType1, SomeType2, SomeType3> {
//...
}
Then I have other classes that have to capture quite a few generic type variables, of which one of them is an object that implements ObjectInterface<X, Y, Z>
. Within such a class, I also need to have a handle on the types (X
, Y
, Z
) that is defined in the object that was captured.
The following (not ideal, and very simplified) code works:
public class ClassWorks<X, Y, Z, N extends ObjectInterface<X, Y, Z>> {
// code body uses X, Y, Z and N...
}
However, this is very cumbersome for the person trying to use/initiate this class, even in this simplified version, for example:
public class ImplementedObject implements ObjectInterface<Integer, Double, String> {
//...
}
public class RandomExample {
public static void main(String[] args) {
ObjectInterface<Integer, Double, String> implementedObj = new ImplementedObject();
ClassWorks<Integer, Double, String, ImplementedObject>> example = new ClassWorks<Integer, Double, String, ImplementedObject>(/* possible params */);
}
}
Is there a way to "extract" or capture these types so that they are inferred instead of explicit as it is for ClassWorks
in the working example?
Possibly something similar to the following (Note this does not work):
pulic class WishfullClass<N extends ObjectInterface<X, Y, Z>> {
// type N is captured; X, Y and Z is not explicitly captured.
// code uses type N, as well as X, Y and Z
// where, X, Y and Z is inferred somehow from N.
}
Edit: So an implemented example of WishfullClass would be:
public class ImplementedObject implements ObjectInterface<Integer, Double, String> {
//...
}
public class WishfullExample {
public static void main(String[] args) {
ObjectInterface<Integer, Double, String> implementedObj = new ImplementedObject();
WishFullClass<ImplementedObject> example = new WishfullClass<ImplementedObject>(/* possible params */);
}
}
ie. The compiler should know that ImplementedObject
from the declaration of the class that it implements ObjectInterface<Integer, Double, String>
as X, Y and Z.
Please note, these above are very simplified and in the real code are not the only parameters that need capturing, so those three extra parameters make quite a big difference; also the implemented object also captures generic types,
So ideally I would like to just collectively capture the object that extend the ObjectInterface<X, Y, Z>
and have X
, Y
and Z
inferred. Is there a way to do this?
I.e. The snippet of someMethod shows how you can infer X, Y and Z for the scope of a method. My question refers to, is there a way to infer X, Y and Z for the entire scope of the class by only capturing a type that extends ObjectInterface
.
I had some trouble wording/explaining this question, so if there is any uncertainty, please ask for clarification :)