Java – Is it possible to solve the “A generic array of T is created for a varargs parameter” compiler warning

genericsjava

This is a simplified version of the code in question, one generic class uses another class with generic type parameters and needs to pass one of the generic types to a method with varargs parameters:

class Assembler<X, Y> {
    void assemble(X container, Y... args) { ... }
}

class Component<T> {
    void useAssembler(T something) {

        Assembler<String, T> assembler = new Assembler<String, T>();

        //generates warning:
        // Type safety : A generic array of T is
        // created for a varargs parameter
        assembler.assemble("hello", something);
    }

}

Is there any correct way to pass along the generic parameter to a varargs method without encountering this warning?

Of course something like

assembler.assemble("hello", new T[] { something });

does not work since you cannot create generic arrays.

Best Answer

In Java 6, other than adding @SuppressWarnings("unchecked"), I don't think so.

This bug report has more information but it boils down to the compiler not liking arrays of generic types.