Java Object Type Case castNonNullArray( T [] arr)

Here you can find the source of castNonNullArray( T [] arr)

Description

cast Non Null Array

License

Open Source License

Declaration

private static <T extends  Object>  T  [] castNonNullArray(
            T  [] arr) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

public class Main {
    private static <T extends /*@Nullable*/ Object> /*@NonNull*/ T /*@NonNull*/ [] castNonNullArray(
            T /*@Nullable*/ [] arr) {
        assert arr != null : "Misuse of castNonNullArray: called with a null array argument";
        for (int i = 0; i < arr.length; ++i) {
            assert arr[i] != null : "Misuse of castNonNull: called with a null array element";
            checkIfArray(arr[i]);/*from  w  w w.j  av a 2  s. c o m*/
        }
        return (/*@NonNull*/ T[]) arr;
    }

    private static void checkIfArray(/*@NonNull*/ Object ref) {
        assert ref != null : "Misuse of checkIfArray: called with a null argument";
        Class<?> comp = ref.getClass().getComponentType();
        if (comp != null) {
            // comp is non-null for arrays, otherwise null.
            if (comp.isPrimitive()) {
                // Nothing to do for arrays of primitive type: primitives are
                // never null.
            } else {
                castNonNullArray((Object[]) ref);
            }
        }
    }
}

Related

  1. castingNum(double n)
  2. castingUtil(String value, Class classCasting)
  3. castInstance(Object o, Class clazz)
  4. castInttoLong(int x)
  5. castNode(Class jsonNode, Class classz)
  6. castNullableParameterTo(String parameterName, T value, Class requiredType)
  7. castNumber(Number num, Class clazz)
  8. castObject(Class clazz, Object object)
  9. castObject(Object fromObj, Class toClass)