Example usage for java.lang.reflect Array getLength

List of usage examples for java.lang.reflect Array getLength

Introduction

In this page you can find the example usage for java.lang.reflect Array getLength.

Prototype

@HotSpotIntrinsicCandidate
public static native int getLength(Object array) throws IllegalArgumentException;

Source Link

Document

Returns the length of the specified array object, as an int .

Usage

From source file:Main.java

public static Object[] getArray(Object val) {
    Class<?> valKlass = val.getClass();
    Object[] outputArray = null;/*from   ww w  .j a  v a 2  s .  c  om*/
    for (Class<?> arrKlass : ARRAY_PRIMITIVE_TYPES) {
        if (valKlass.isAssignableFrom(arrKlass)) {
            int arrlength = Array.getLength(val);
            outputArray = new Object[arrlength];
            for (int i = 0; i < arrlength; ++i) {
                outputArray[i] = Array.get(val, i);
            }
            break;
        }
    }
    if (outputArray == null) // not primitive type array
        outputArray = (Object[]) val;
    return outputArray;
}

From source file:MainClass.java

private static void fillArray(Object array) {
    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
        int random = generator.nextInt();
        Array.setInt(array, i, random);
    }//from   w  ww.ja  va  2 s.  com
}

From source file:Main.java

public static final Object[] toObjectArray(Object obj) {
    int length = Array.getLength(obj);
    Object[] array = new Object[length];
    for (int i = 0; i < length; i++) {
        array[i] = Array.get(obj, i);
    }//from w  w w  .java2 s.  c  o m
    return array;
}

From source file:Main.java

/**
 * Expands an array of Objects to double of its current size. Remember to use cast to turn the result into an array of the appropriate class, i.e:
 * //from  w w  w  .j ava 2 s . co m
 *   <code>someArray=(SomeClass [])unlekker.util.Util.expandArray(someArray);
 * @param list Array to be expanded.
 * @return Array of Object [], must be cast to appropriate class.
 */
static public Object expandArray(Object[] list) {
    int newSize = list.length * 2;
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize));
    return temp;
}

From source file:Main.java

/**
 * invoke the leftParameter and get the name property.
 * it will try the Array.length() and Map.get(),
 * then it will try get'Name' and is'Name',
 * last, it will to find the name by invoke field.
 *///from ww w  .j a  v a  2  s.com
public static Object searchProperty(Object leftParameter, String name) throws Exception {
    Class<?> leftClass = leftParameter.getClass();
    Object result;
    if (leftParameter.getClass().isArray() && "length".equals(name)) {
        result = Array.getLength(leftParameter);
    } else if (leftParameter instanceof Map) {
        result = ((Map<Object, Object>) leftParameter).get(name);
    } else {
        try {
            String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = leftClass.getMethod(getter, new Class<?>[0]);
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            result = method.invoke(leftParameter, new Object[0]);
        } catch (NoSuchMethodException e2) {
            try {
                String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
                Method method = leftClass.getMethod(getter, new Class<?>[0]);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(leftParameter, new Object[0]);
            } catch (NoSuchMethodException e3) {
                Field field = leftClass.getField(name);
                result = field.get(leftParameter);
            }
        }
    }
    return result;
}

From source file:Main.java

private static String[] getExternalDirs(Context context) {
    Context mContext = context.getApplicationContext();
    StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    try {/*from w  ww  . j ava 2s  .  c o  m*/
        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Object result = getVolumeList.invoke(mStorageManager);
        final int length = Array.getLength(result);
        final String[] paths = new String[length];
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            paths[i] = (String) getPath.invoke(storageVolumeElement);
        }
        return paths;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int getLengthOfCollection(Object value) {
    if (isArray(value.getClass())) {
        return Array.getLength(value);
    } else {/*w w w  .  ja  va  2 s .  co  m*/
        return ((Collection<?>) value).size();
    }
}

From source file:Main.java

public static List<Object> convertPrimitiveArrayToList(Object primitiveArray) {
    int length = Array.getLength(primitiveArray);
    List<Object> result = new ArrayList<Object>(length);

    // wrap and copy elements
    for (int i = 0; i < length; i++) {
        result.add(Array.get(primitiveArray, i));
    }//from w w w  .  j ava 2s  . c  om
    return result;
}

From source file:Main.java

public static Object expand(Object obj, int i, boolean flag) {
    int j = Array.getLength(obj);
    Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j + i);
    System.arraycopy(obj, 0, obj1, flag ? 0 : i, j);
    return obj1;/*from   w w w  .  j a  v  a 2 s.co m*/
}

From source file:Main.java

public static Object cut(Object obj, int size) {
    int j;// www  . j av  a2 s .  c o  m
    if ((j = Array.getLength(obj)) == 1) {
        return Array.newInstance(obj.getClass().getComponentType(), 0);
    }
    int k;
    if ((k = j - size - 1) > 0) {
        System.arraycopy(obj, size + 1, obj, size, k);
    }
    j--;
    Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j);
    System.arraycopy(obj, 0, obj1, 0, j);
    return obj1;
}