0

First of all, I know you can pass Type as a parameter by using Class<T>. However, I have a scenario as follows:

Suppose I have a method for creating MyOwnType (the code below compiles):

public MyOwnType<? extends Type,? extends Type> createMyOwnType(String name, Class<? extends Type> T1, Class<? extends Type> T2)

What I really want to do is to use Type variables T1 and T2 to construct MyOwnType:

MyOwnType<T1,T2>

I know you can directly construct MyOwnType without using createMyOwnType method. But I want to use a proper factory pattern to construct MyOwnType for users + a few constraints I will code up.

I don't want to check all parameters one by one say if T1 is some type, and T2 is some type, then constrcut MyOwnType. Because in some cases, I could have many Ts, they all are subtypes of Type. Therefore, I will have T1xT2xT3xT_n combinations to check.

Therefore,my question is that is there a way of creating MyOwnType based on the parameters provided (T1 and T2), something similar like the following code:

MyOwnType<T1,T2> myowntype=new MyOwnType<T1,T2>(name);

The code above won't compile due to the Java compiler cannot find symbol T1,T2.

user1197891
  • 265
  • 1
  • 12
  • the only way this can be done is if you provide the same constructor for every subtype of Type – 0x6C38 Jan 12 '17 at 17:38
  • 1
    Strong suggestion: consider using reflection: [What is reflection and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) – paulsm4 Jan 12 '17 at 17:40
  • 2
    I'm not sure what you expect your code to do. Remember, because of [type erasure](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html), at runtime the method is just `public MyOwnType createMyOwnType(String name, Class T1, Class T2)`, i.e. the return type is just a `MyOwnType` class/interface that doesn't depend on `T1` and `T2`. – Andreas Jan 12 '17 at 17:40

1 Answers1

1

You can use java generics in this case. See example for your case:

class Type {}

// create some classes based on Type
class Car extends Type {}
class Bike extends Type {}
class Boat extends Type {}

@Data
class MyOwnType<T1 extends Type, T2 extends Type > {
    private String name;
    private T1 field1;
    private T2 field2;
}

public class SomeClass {

    public static <T1 extends Type, T2 extends Type> MyOwnType<T1, T2> createMyOwnType(String name) {
        MyOwnType<T1, T2> result = new MyOwnType<>();
        result.setName(name);
        return result;
    }

    public static void main(String[] args) {
        MyOwnType<Car, Boat> sample1 = createMyOwnType("orange");
        Car car = sample1.getField1();
        Boat boat = sample1.getField2();

        MyOwnType<Boat, Bike> sample2 = createMyOwnType("red");

    }
}

Here instead of passing parameters into factory method we just specify desired types in declaring of variables "sample1" and "sample2". I hope it is what you want.

But if you want to create real factory, i.e. use types inside method then it is not possible because Java does not support this. You just can to prepare classes like in my example. Inside method you just know that T1 and T2 extends Type.

Dmitry Gorkovets
  • 2,208
  • 1
  • 10
  • 19
  • Is this something to declare T1 and T2 to be used in the actual method? Thank you. this is what I want. I never know you can use generics in this way. Thank you. – user1197891 Jan 13 '17 at 14:53