developer tip

Java unchecked : varargs 매개 변수에 대해 확인되지 않은 일반 배열 생성

copycodes 2020. 9. 1. 07:38
반응형

Java unchecked : varargs 매개 변수에 대해 확인되지 않은 일반 배열 생성


Java 코드에서 확인되지 않은 경고를 표시하도록 Netbeans를 설정했지만 다음 줄에서 오류를 이해하지 못했습니다.

private List<String> cocNumbers;
private List<String> vatNumbers;
private List<String> ibans;
private List<String> banks;
...
List<List<String>> combinations = Utils.createCombinations(cocNumbers, vatNumbers, ibans);

제공 :

[unchecked] unchecked generic array creation for varargs parameter of type List<String>[]

방법 출처 :

/**
 * Returns a list of all possible combinations of the entered array of lists.
 *
 * Example: [["A", "B"], ["0", "1", "2"]]
 * Returns: [["A", "0"], ["A", "1"], ["A", "2"], ["B", "0"], ["B", "1"], ["B", "2"]]
 *
 * @param <T> The type parameter
 * @param elements An array of lists
 * @return All possible combinations of the entered lists
 */
public static <T> List<List<T>> createCombinations(List<T>... elements) {
    List<List<T>> returnLists = new ArrayList<>();

    int[] indices = new int[elements.length];
    for (int i = 0; i < indices.length; i++) {
        indices[i] = 0;
    }

    returnLists.add(generateCombination(indices, elements));
    while (returnLists.size() < countCombinations(elements)) {
        gotoNextIndex(indices, elements);
        returnLists.add(generateCombination(indices, elements));
    }

    return returnLists;
}

코드에 확인되지 않은 경고를 남겨 두는 것은 좋은 생각이 아니라고 생각하므로 정확히 무엇이 잘못되고 어떻게 고칠 수 있습니까?

언급하는 것을 잊었지만 Java 7을 사용하고 있습니다.

편집 : 또한 방법에 다음이 있음을 알 수 있습니다.

[unchecked] Possible heap pollution from parameterized vararg type List<T>
  where T is a type-variable:
    T extends Object declared in method <T>createCombinations(List<T>...)

위에서 언급 한 janoh.janoh와 같이 Java의 varargs는 배열에 대한 구문 설탕과 호출 사이트에서 배열의 암시 적 생성에 불과합니다. 그래서

List<List<String>> combinations =
    Utils.createCombinations(cocNumbers, vatNumbers, ibans);

실제로

List<List<String>> combinations =
    Utils.createCombinations(new List<String>[]{cocNumbers, vatNumbers, ibans});

But as you may know, new List<String>[] is not allowed in Java, for reasons that have been covered in many other questions, but mainly have to do with the fact that arrays know their component type at runtime, and check at runtime whether elements added match its component type, but this check is not possible for parameterized types.

Anyway, rather than failing, the compiler still creates the array. It does something similar to this:

List<List<String>> combinations =
    Utils.createCombinations((List<String>[])new List<?>[]{cocNumbers, vatNumbers, ibans});

This is potentially unsafe, but not necessarily unsafe. Most varargs methods simply iterate over the varargs elements and read them. In this case, it doesn't care about the runtime type of the array. This is the case with your method. Since you are on Java 7, you should add the @SafeVarargs annotation to your method, and you won't get this warning anymore. This annotation basically says, this method only cares about the types of the elements, not the type of the array.

However, there are some varargs methods that do use the runtime type of the array. In this case, it is potentially unsafe. That's why the warning is there.


Because java compiler uses an implicit array creation for varargs, and java doesn't allow a generic array creation (because type argument is not reifiable).

The code below is correct (these operations are allowed with arrays), so unchecked warning is needed:

public static <T> List<List<T>> createCombinations(List<T> ... lists) {
    ((Object[]) lists)[0] = new ArrayList<Integer>();
    // place your code here
}

See a comprehensive explanation here

참고URL : https://stackoverflow.com/questions/21132692/java-unchecked-unchecked-generic-array-creation-for-varargs-parameter

반응형