List of usage examples for java.lang.reflect Array get
public static native Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:ArrayEnumerationFactory.java
static public Enumeration makeEnumeration(final Object obj) { Class type = obj.getClass();/*w w w . ja v a 2 s .co m*/ if (!type.isArray()) { throw new IllegalArgumentException(obj.getClass().toString()); } else { return (new Enumeration() { int size = Array.getLength(obj); int cursor; public boolean hasMoreElements() { return (cursor < size); } public Object nextElement() { return Array.get(obj, cursor++); } }); } }
From source file:Main.java
public static Object combineArray(Object firstArray, Object secondArray) { int firstLength = Array.getLength(firstArray); int secondLength = Array.getLength(secondArray); int length = firstLength + secondLength; Class<?> componentType = firstArray.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { if (i < firstLength) { Array.set(newArray, i, Array.get(firstArray, i)); } else {//from ww w . j ava 2s. com Array.set(newArray, i, Array.get(secondArray, i - firstLength)); } } return newArray; }
From source file:Main.java
public static Object[] getArray(Object val) { Class<?> valKlass = val.getClass(); Object[] outputArray = null;/*from w w w. ja va 2 s .c o m*/ 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: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 . j av a 2s .co m return array; }
From source file:Main.java
public static Object getValueFromCollection(Object collection, int index) { if (isArray(collection.getClass())) { return Array.get(collection, index); } else {/* w ww. j av a 2 s. c o m*/ return ((Collection<?>) collection).toArray()[index]; } }
From source file:Main.java
private static String[] getExternalDirs(Context context) { Context mContext = context.getApplicationContext(); StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); try {/* w w w.ja v a2s . 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 Object findElementInstance(Object collection) { if (collection == null) return null; if (collection.getClass().isArray()) { for (int i = 0; i < Array.getLength(collection); i++) { Object o = Array.get(collection, i); if (o != null) { return o; }// ww w .jav a 2 s .c om } } else if (collection instanceof Collection) { for (Object o : ((Collection<?>) collection)) { if (o != null) { return o; } } } return null; }
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 ww . j a v a2 s. co m*/ return result; }
From source file:Main.java
public static boolean overrideClassLoader(ClassLoader cl, File dex, File opt) { try {//from w ww . j av a 2 s . c om ClassLoader bootstrap = cl.getParent(); Field fPathList = BaseDexClassLoader.class.getDeclaredField("pathList"); fPathList.setAccessible(true); Object pathList = fPathList.get(cl); Class cDexPathList = bootstrap.loadClass("dalvik.system.DexPathList"); Field fDexElements = cDexPathList.getDeclaredField("dexElements"); fDexElements.setAccessible(true); Object dexElements = fDexElements.get(pathList); DexClassLoader cl2 = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, bootstrap); Object pathList2 = fPathList.get(cl2); Object dexElements2 = fDexElements.get(pathList2); Object element2 = Array.get(dexElements2, 0); int n = Array.getLength(dexElements) + 1; Object newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), n); Array.set(newDexElements, 0, element2); for (int i = 0; i < n - 1; i++) { Object element = Array.get(dexElements, i); Array.set(newDexElements, i + 1, element); } fDexElements.set(pathList, newDexElements); return true; } catch (Exception e) { Log.e("lcast", "fail to override classloader " + cl + " with " + dex, e); return false; } }
From source file:Main.java
public static String getAvailableStoragePath(Context context) { if (BASE_DIR == null) { try {/*from w w w . j a va2s.c o m*/ BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath(); StorageManager sStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Class<?> smClazz = sStorageManager.getClass(); Method listMethod = smClazz.getDeclaredMethod("getVolumeList"); Object vlObject = listMethod.invoke(sStorageManager); if (vlObject.getClass().isArray()) { String state = null; String path = null; // Class svClazz = // Class.forName("android.os.storage.StorageVolume"); Object svObject = Array.get(vlObject, 1); if (svObject != null) { Method pathMethod = svObject.getClass().getMethod("getPath"); path = (String) pathMethod.invoke(svObject); Method stateMethod = smClazz.getMethod("getVolumeState", new Class[] { String.class }); state = (String) stateMethod.invoke(sStorageManager, path); } if (path != null && state != null && state.equals(Environment.MEDIA_MOUNTED)) { BASE_DIR = path; } else { BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath(); } } } catch (Exception e) { BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath(); } return BASE_DIR; } else { return BASE_DIR; } }