0

I have a class defined like this

class MyTransformer<V, M extends SomeBaseM> extends Transformer<V,M> {
  ...
}

In all cases but one I would instantiate it like this

MyTransformer<Foo,Bar> t = new MyTransformer(...);

There is this one case where I get the generic types as a Class instead:

void annoyingCaller( Class<? extends V> vClass, Class<? extends SomeBaseM> M, ... ) {
  MyTransformer<?,?> t = new MyTransformer(...);
}

Is there anything I can do to populate these generic type (via reflection perhaps?)

Assaf Moldavsky
  • 1,681
  • 1
  • 19
  • 30

2 Answers2

1

Does this work for you?

void annoyingCaller( Class<? extends V> vClass, Class<? extends SomeBaseM> M, ... ) {
  MyTransformer<Class<? super V>, Class<? super SomeBaseM>> t 
         = new MyTransformer<>(...);
}
markspace
  • 10,621
  • 3
  • 25
  • 39
  • for this signature to work void annoyingCaller( Class extends V> vClass, Class extends SomeBaseM> M, ... ) you will have to either parameterize the method or the class it is in. Something or someone has to know what is V and M, I am trying to manually infer V and M from a String class name – Assaf Moldavsky Mar 13 '19 at 15:40
0

You were almost there I suppose.
I don't think this will be useful however, as you'll need to pass in specific Classes, and if you can do that, you already know the generic types.

<V, M extends SomeBaseM> MyTransformer<V, M> annoyingCaller(
        Class<? extends V> v,
        Class<? extends M> m) {
    return new MyTransformer<V, M>();
}

If, instead, you don't need to return the instance, but you just need it as an internal process, it's fine.

void <V, M extends SomeBaseM> annoyingCaller(
        Class<? extends V> v,
        Class<? extends M> m) {
    final MyTransformer<V, M> transformer = new MyTransformer<>();
    transformer.method();
    ...
}
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • Note that the OP is returning type `void`. Dunno if that's on purpose or not. – markspace Mar 12 '19 at 23:18
  • Maybe, I'll stick to this for now. He can adjust as needed. – LppEdd Mar 12 '19 at 23:18
  • Care to explain the downvote? Not referred to you markspace – LppEdd Mar 12 '19 at 23:48
  • with this example you just pass the responsibility / knowledge upward to the caller, so it is the say as saying, "I don't care what you do, I need the types, you go figure out how to get them", but in this context it is my responsibility to get the types bases on String classnames if that makes sense – Assaf Moldavsky Mar 13 '19 at 15:38
  • @AssafMoldavsky well then it's not possible. You cannot declare generic types via Strings. – LppEdd Mar 13 '19 at 15:44
  • @AssafMoldavsky in another comment you wrote "Something or someone has to know what is V and M". Yes, the upper level, which calls your method. – LppEdd Mar 13 '19 at 15:51
  • @LppEdd so the caller will have to solve the same problem as I do, so it seems to me that this is just not possible and I have to change the design – Assaf Moldavsky Mar 13 '19 at 16:09
  • @AssafMoldavsky but what's the real problem you're trying to solve? – LppEdd Mar 13 '19 at 16:12
  • @LppEdd imagine the scenario that I get the clsas as a string from a restful API, I am not able to populate these generic types from that API call because my knowledge of that the class / type is in a form of a String "java.lang.Integer" or some other class – Assaf Moldavsky Mar 13 '19 at 16:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189959/discussion-between-lppedd-and-assaf-moldavsky). – LppEdd Mar 13 '19 at 16:14
  • @LppEdd the real issue is that I have a real class like this with two generic types, but in one use case this class needs to be instantiated from an API call, the endpoint gets the the class name as a parameter. There is just no way for me to instantiate this class it seems like – Assaf Moldavsky Mar 13 '19 at 16:14
  • 1
    we resolved it over chat with @LppEdd, what i wanted to do will not be possible, somebody has to know the actual type at runtime – Assaf Moldavsky Mar 13 '19 at 16:25