Reason: the intended type does not match the upper bound (s)

I tried to find a similar answer, but I have not yet received / not found a solution, and then I will ask you to directly expose my case

I have a static function validate

    private static void validate(AiNode pNode) {
        ...
            for (AiNode child : pNode.mChildren) {
                doValidation(child.mMeshes, child.mMeshes.length, "a", "b");
            }
        }
    }

pNode.mChildrenis an array of AiNode.

This is my doValidation

private static <T> void doValidation(T[] pArray, int size, String firstName, String secondName) {

        // validate all entries
        if (size > 0) {

            if (pArray == null) {

                throw new Error("aiScene." + firstName + " is NULL (aiScene." + secondName + " is " + size + ")");
            }
            for (int i = 0; i < size; i++) {

                if (pArray[i] != null) {

                    validate(parray[i]);
                }
            }
        }
    }

But I keep getting this error

method doValidation in class ValidateDataStructure cannot be applied to given types;
  required: T[],int,String,String
  found: int[],int,String,String
  reason: inferred type does not conform to upper bound(s)
    inferred: int
    upper bound(s): Object
  where T is a type-variable:
    T extends Object declared in method <T>doValidation(T[],int,String,String)
----
(Alt-Enter shows hints)

I think this is due to the fact that Object [] extends primitive type arrays in java, indeed, if I switch T[]to T, it works, but then I can no longer loop it ... I don’t know how to solve it or what solutions Best suited for my case.

The idea is to be different validate(T[] array)from an array Ttype

+4
1

int[], Integer[]. Integer[],

for (AiNode child : pNode.mChildren) {
    Integer[] meshes = Arrays.stream(child.mMeshes).boxed().toArray(Integer::new);
    doValidation(meshes, child.mMeshes.length, "a", "b");
}
+2

Source: https://habr.com/ru/post/1607605/


All Articles