List of usage examples for java.lang Class getComponentType
public Class<?> getComponentType()
From source file:Main.java
/** * This method extracts a {@link Parcelable} array from the {@link Bundle}, * and returns it in an array whose type is the exact {@link Parcelable} * subclass. This is needed because {@link Bundle#getParcelable(String)} * returns an array of {@link Parcelable}, and we would get * {@link ClassCastException} when we assign it to {@link Parcelable} * subclass arrays.// w ww . j a v a2 s .c om * * For more info, see <a * href="https://github.com/excilys/androidannotations/issues/1208">this</a> * url. * * @param bundle * the bundle holding the array which is extracted * @param key * the array is associated with this key * @param type * the desired type of the returned array * @param <T> * the element type of the returned array * @return a {@link Parcelable} subclass typed array which holds the objects * from {@link Bundle#getParcelableArray(String)} or * <code>null</code> if {@link Bundle#getParcelableArray(String)} * returned <code>null</code> for the key */ @SuppressWarnings("unchecked") public static <T extends Parcelable> T[] getParcelableArray(Bundle bundle, String key, Class<T[]> type) { Parcelable[] value = bundle.getParcelableArray(key); if (value == null) { return null; } Object copy = Array.newInstance(type.getComponentType(), value.length); System.arraycopy(value, 0, copy, 0, value.length); return (T[]) copy; }
From source file:com.vk.sdk.util.VKJsonHelper.java
public static Object toArray(JSONArray array, Class arrayClass) { Object ret = Array.newInstance(arrayClass.getComponentType(), array.length()); Class<?> subType = arrayClass.getComponentType(); for (int i = 0; i < array.length(); i++) { try {/*from ww w . ja v a 2 s . c o m*/ Object jsonItem = array.get(i); Object objItem = subType.newInstance(); if (jsonItem instanceof JSONObject) { JSONObject jsonItem2 = (JSONObject) jsonItem; if (objItem instanceof VKApiModel) { VKApiModel objItem2 = (VKApiModel) objItem; ((VKApiModel) objItem).parse(jsonItem2); Array.set(ret, i, objItem2); } } } catch (JSONException e) { if (VKSdk.DEBUG) e.printStackTrace(); } catch (InstantiationException e) { if (VKSdk.DEBUG) e.printStackTrace(); } catch (IllegalAccessException e) { if (VKSdk.DEBUG) e.printStackTrace(); } } return ret; }
From source file:Main.java
/** * Returns a class name without "java.lang." and "java.util." prefix, also shows array types in a format like * {@code int[]}; useful for printing class names in error messages. * // www.j a v a 2 s . co m * @param pClass can be {@code null}, in which case the method returns {@code null}. * @param shortenFreeMarkerClasses if {@code true}, it will also shorten FreeMarker class names. The exact rules * aren't specified and might change over time, but right now, {@code freemarker.ext.beans.NumberModel} for * example becomes to {@code f.e.b.NumberModel}. * * @since 2.3.20 */ public static String getShortClassName(Class pClass, boolean shortenFreeMarkerClasses) { if (pClass == null) { return null; } else if (pClass.isArray()) { return getShortClassName(pClass.getComponentType()) + "[]"; } else { String cn = pClass.getName(); if (cn.startsWith("java.lang.") || cn.startsWith("java.util.")) { return cn.substring(10); } else { if (shortenFreeMarkerClasses) { if (cn.startsWith("freemarker.template.")) { return "f.t" + cn.substring(19); } else if (cn.startsWith("freemarker.ext.beans.")) { return "f.e.b" + cn.substring(20); } else if (cn.startsWith("freemarker.core.")) { return "f.c" + cn.substring(15); } else if (cn.startsWith("freemarker.ext.")) { return "f.e" + cn.substring(14); } else if (cn.startsWith("freemarker.")) { return "f" + cn.substring(10); } // Falls through } return cn; } } }
From source file:ArrayConverter.java
public static int getDimensionCount(Class arrayClass) { int count = 0; while (arrayClass != null && arrayClass.isArray()) { count += 1;/* w w w. jav a 2s. co m*/ arrayClass = arrayClass.getComponentType(); } return count; }
From source file:Main.java
public static String getDesc(Class<?> paramClass) { if (paramClass.isPrimitive()) { return getPrimitiveLetter(paramClass); }/*from www . j a va2 s . c o m*/ if (paramClass.isArray()) { return "[" + getDesc(paramClass.getComponentType()); } return "L" + getType(paramClass) + ";"; }
From source file:Main.java
private static Object[] copyOf(Object[] obj, int newSize, Class newType) { Object[] copy = ((Object) newType == (Object) Object[].class) ? (Object[]) new Object[newSize] : (Object[]) Array.newInstance(newType.getComponentType(), newSize); return copy;/*from w ww .j av a 2 s .c om*/ }
From source file:org.codehaus.griffon.runtime.domain.GriffonDomainConfigurationUtil.java
public static boolean isBasicType(Class<?> propType) { if (propType == null) return false; if (propType.isArray()) { return isBasicType(propType.getComponentType()); }/*from ww w . ja v a2 s. co m*/ return BASIC_TYPES.contains(propType.getName()); }
From source file:org.carrot2.workbench.core.helpers.SimpleXmlMemento.java
/** * Mimics SimpleXML's naming for classes without {@link Root#name()}. *//*from ww w .j ava2 s .com*/ private static String getClassName(Class<?> type) { if (type.isArray()) type = type.getComponentType(); final String name = type.getSimpleName(); if (type.isPrimitive()) return name; else return Introspector.decapitalize(name); }
From source file:ArrayDemo.java
/** * Copy an array and return the copy./* www . java2 s . co m*/ * * @param input The array to copy. * * @return The coppied array. * * @throws IllegalArgumentException If input is not an array. */ public static Object copyArray(final Object input) { final Class type = input.getClass(); if (!type.isArray()) { throw new IllegalArgumentException(); } final int length = Array.getLength(input); final Class componentType = type.getComponentType(); final Object result = Array.newInstance(componentType, length); for (int idx = 0; idx < length; idx++) { Array.set(result, idx, Array.get(input, idx)); } return result; }
From source file:org.grails.ide.eclipse.runonserver.RunOnServerPlugin.java
public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy;/* w w w. j av a2s . c om*/ }