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?)

3
  • Obviously, you should not use raw types. I assume it's just a typo: MyTransformer<Foo,Bar> t = new MyTransformer<>(...); Commented Mar 12, 2019 at 23:14
  • Might be a dupe: stackoverflow.com/questions/2723397/… Commented Mar 12, 2019 at 23:15
  • MyTransformer<V, SomeBaseM> t = new MyTransformer(...); should work Commented Mar 12, 2019 at 23:23

2 Answers 2

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<>(...);
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
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();
    ...
}

12 Comments

Note that the OP is returning type void. Dunno if that's on purpose or not.
Maybe, I'll stick to this for now. He can adjust as needed.
Care to explain the downvote? Not referred to you markspace
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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.