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

前端 未结 8 1550
Happy的楠姐
Happy的楠姐 2020-11-28 04:31

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 metho

相关标签:
8条回答
  • 2020-11-28 05:11

    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.

    0 讨论(0)
  • 2020-11-28 05:14

    You can add @SafeVarargs to method since Java 7, and you don't have to annotate on client code.

    class Assembler<X, Y> {
    
        @SafeVarargs
        final void assemble(X container, Y... args) {
            //has to be final...
        }
    }
    
    0 讨论(0)
提交回复
热议问题