I meet a trouble in using generic method
Compiled class:
public class Something<T> {
public static Something newInstance(Class<T> type){};
public <T> void doSomething(T input){};
}
and my method is:
public <S> void doOtherThing(S input){
Something smt = Something.newInstance(input.getClass());
smt.doSomething(input); // Error here
}
It got error at Compile time:
no suitable method found for doSomething(T) T cannot be converted to capture#1 of ? extends java.lang.Object ...
I think there might be a trick to avoid this, please help
Ts.doSomething'sTis not the same as the classSomething'sT, becausedoSomethingis a generic method that declares its ownT. You should name the two variables differently, because they are unrelated to each other. Then the fact thatdoSomethingonly usesTonce as a parameter type, andTis unbounded, means that it takes anything, so it is equivalent topublic void doSomething(Object input){}; theTis useless. Same thing withdoOtherThing-- it is equivalent topublic void doOtherThing(Object input)