Instantiate generic type with constructor arguments?

Viewed 3009

I'm facing a very confusing problem right now.

I am developing an extension for a program library. In said library, there are multiple classes A, B, C and D, which all inherit from the same superclass Base.

Now, I'm making individual extensions for each of those classes, A1, B1, C1 and D1, which all inherit from A, B, C and D respectively. In addition to that, they all implement a custom interface I1, which I have also added myself.

The constructor for base (and all child classes, including mine) accepts one argument of the type E.

In order to instantiate those classes in an easy manner, I'm trying to write a handler class that creates an instance when needed. It's supposed to work something like this:

public class Handler
{
    private Base ctype;
    public Handler(Class<T extends Base implements I1> ctype)
    {
          this.ctype = new ctype.class(E.instance);
    }
}

Now, of course it doesn't work that way, but I'm sure I got the idea across.

As of right now, I have a working version of my extension, albeit very sloppy, because instead of a handler class, I'm checking for every type manually and instantiating the proper Object. As I will have to modify many more classes in the future, I'd like to avoid that at all costs, to avoid file clutter.

I'm aware of the ctype.newInstance() method, however since the Base class requires a parameter in its constructor, I'm afraid it won't work. (unless I'm missing something, in which case please point me to it!)

Thank you for your help!

2 Answers
Related