14

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

1
  • There are a lot of issues here. First there are two separate Ts. doSomething's T is not the same as the class Something's T, because doSomething is a generic method that declares its own T. You should name the two variables differently, because they are unrelated to each other. Then the fact that doSomething only uses T once as a parameter type, and T is unbounded, means that it takes anything, so it is equivalent to public void doSomething(Object input){}; the T is useless. Same thing with doOtherThing -- it is equivalent to public void doOtherThing(Object input) Commented Oct 9, 2016 at 18:49

3 Answers 3

8

Pass the S class as an argument.

public class Something<T>
{
    public static <T> Something<T> newInstance(Class<T> type)
    {
        return new Something<T>();
    }

    public void doSomething(T input){;}

    public <S> void doOtherThing(Class<S> clazz, S input)
    {
        Something<S> smt = Something.newInstance(clazz);
        smt.doSomething(input);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

But really doOtherThing is rather useless if not static. Basically it's just creating a Something instance and applying doSomething, with generic type S instead of T.
2 argument from same class S to use generic, it's slight redundant
Only way to avoid an unchecked cast warning.
Actually, for this example, Class<T> type is unused, so you don't even need that.
Interesting. Does the T in the Something<T> return type reference the argument-inferred generic, or the generic of the lexical class?
|
7

I think input.getClass() need be cast to Class<T>

public <S> void doOtherThing(S input){
      Something smt = Something.newInstance((Class<T>)input.getClass());
      smt.doSomething(input);
}

1 Comment

This is unsafe because of raw type and unchecked casting.
1

Anything like this? (generic type declaration thingy on our newInstance method)

public class Something<T> {
   public static <T> Something<T> newInstance(Class<T> type){ return null; }
   public <T> void doSomething(T input){};
}

1 Comment

Nope, It work fine with non-generic variable (like String)

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.