1

Please excuse me if this question seems very simple, I am pretty new to java:) I am having unchecked call to transformer(T) warning and the unchecked method invocation when trying to take a generic interface as a parameter for my method, would appreciate any help in revolving the warnings

I have a generic interface with 2 type parameters, and it has one abstract method that simply transforms from type T to type U

Public interface Transformer<T, U> { abstract U transform(T t); }

I have tried to create a class that implement the Transformer interface below

public class LastDigitsOfHashCode<T> implements Transformer<T, Integer>  {
    private final int i;

    public LastDigitsOfHashCode(int i) {
        this.i = i;
    }

    public Integer transform(T t) {
        int a = (int) Math.pow(10, this.i);
        return Math.abs(t.hashCode() % a);
    }
}

But now I have problems when I want to create another method in a different class called Box that takes in the Transformer interface as the parameter to transform the object from type T to Type U, there is unchecked call to transformer(T) warning and the unchecked method invocation which I have failed to resolve. The Box class is a generic class and has one parameter of type T (this.t)

    public <U> Box<U> map(Transformer<T, U> trans) {
        return new Box<U>(trans.transform(this.t));
    }

Please help, really have got no clue as to how to resolve the warning or how to implement the method properly, thank you!

  • 1
    What's the exact warning and which line causes it? – ernest_k May 27 '20 at 10:12
  • I ran {new LastDigitsOfHashCode(4).transform("string")} and the warning is: unchecked call to transform(T) as a member of the raw type LastDigitsOfHashCode – Guo Jingxue May 27 '20 at 10:37
  • Then the code causing that is not shown in the post. The problem is that you've declared something with `LastDigitsOfHashCode` rather than `LastDigitsOfHashCode<...>` – ernest_k May 27 '20 at 10:40

0 Answers0