cannot select Parameterized Type

后端 未结 2 1223
感情败类
感情败类 2020-12-20 12:59

I want to create a rest to communicate between server and client.

The constructor given below:

public class RestHelper {
    public Rest         


        
相关标签:
2条回答
  • 2020-12-20 13:25

    I got the solution.

    ResultContainData<Boolean> result = new ResultContainData<>();
    RestHelper<User, ResultContainData<Boolean>> helper = new RestHelper<>(url, user, (Class<ResultContainData<Boolean>>)result.getClass());
    

    It's working for me. I am still looking for a better solution.

    0 讨论(0)
  • 2020-12-20 13:26

    You can only learn the value of I and R by capturing them in a subclass definition - otherwise they are erased at runtime. Ex:

    class MyStringRestHelper extends RestHelper<String, String> {
    

    Then using something like TypeTools you can resolve the values of I and R:

    Class<?>[] typeArgs = TypeResolver.resolveRawArguments(RestHelper.class, MyStringRestHelper.class);
    Class<?> i = typeArgs[0];
    Class<?> r = typeArgs[1];
    assert i == r == String.class;
    
    0 讨论(0)
提交回复
热议问题