List of usage examples for java.lang Class getComponentType
public Class<?> getComponentType()
From source file:org.diorite.cfg.system.TemplateCreator.java
/** * Create new template for given class if it is unknown type. * (not map/array/iterable etc..)/*from w w w . ja v a 2 s. c om*/ * * @param type type/class to check */ @SuppressWarnings("ObjectEquality") public static void checkTemplate(final Type type) { if (type instanceof Class) { Class<?> c = (Class<?>) type; do { if ((c == null) || (Enum.class.isAssignableFrom(c)) || (c == Object.class) || (c == Object[].class) || (c.isArray() && (DioriteReflectionUtils.getPrimitive(c.getComponentType()).isPrimitive() || String.class.isAssignableFrom(c.getComponentType()))) || (TemplateElements.getElement(c) != TemplateElements.getDefaultTemplatesHandler())) { return; } // generate and cache template if (c.isArray()) { while (c.isArray()) { c = c.getComponentType(); } checkTemplate(c); } getTemplate(c, true, true, false); c = c.getSuperclass(); } while (true); } else if (type instanceof ParameterizedType) { final ParameterizedType pType = (ParameterizedType) type; for (final Type t : pType.getActualTypeArguments()) { checkTemplate(t); } } }
From source file:com.github.dozermapper.core.util.MappingUtils.java
@SuppressWarnings("unchecked") private static <T> T[] prepareIndexedArray(Class<T> collectionType, Object existingCollection, Object collectionEntry, int index) { T[] result;// w ww .j a v a2 s . com if (existingCollection == null) { result = (T[]) Array.newInstance(collectionType.getComponentType(), index + 1); } else { int originalLenth = ((Object[]) existingCollection).length; result = (T[]) Array.newInstance(collectionType.getComponentType(), Math.max(index + 1, originalLenth)); System.arraycopy(existingCollection, 0, result, 0, originalLenth); } result[index] = (T) collectionEntry; return result; }
From source file:com.base.dao.sql.ReflectionUtils.java
public static Object convertValue(Object value, Class toType) { Object result = null;/*w ww .j av a 2 s . c om*/ if (value != null) { if (value.getClass().isArray() && toType.isArray()) { Class componentType = toType.getComponentType(); result = Array.newInstance(componentType, Array.getLength(value)); for (int i = 0, icount = Array.getLength(value); i < icount; i++) { Array.set(result, i, convertValue(Array.get(value, i), componentType)); } } else { if ((toType == Integer.class) || (toType == Integer.TYPE)) result = Integer.valueOf((int) longValue(value)); if ((toType == Double.class) || (toType == Double.TYPE)) result = new Double(doubleValue(value)); if ((toType == Boolean.class) || (toType == Boolean.TYPE)) result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE; if ((toType == Byte.class) || (toType == Byte.TYPE)) result = Byte.valueOf((byte) longValue(value)); if ((toType == Character.class) || (toType == Character.TYPE)) result = new Character((char) longValue(value)); if ((toType == Short.class) || (toType == Short.TYPE)) result = Short.valueOf((short) longValue(value)); if ((toType == Long.class) || (toType == Long.TYPE)) result = Long.valueOf(longValue(value)); if ((toType == Float.class) || (toType == Float.TYPE)) result = new Float(doubleValue(value)); if (toType == BigInteger.class) result = bigIntValue(value); if (toType == BigDecimal.class) result = bigDecValue(value); if (toType == String.class) result = stringValue(value); if (toType == Date.class) { result = DateUtils.toDate(stringValue(value)); } if (Enum.class.isAssignableFrom(toType)) result = enumValue((Class<Enum>) toType, value); } } else { if (toType.isPrimitive()) { result = primitiveDefaults.get(toType); } } return result; }
From source file:com.manydesigns.elements.util.Util.java
public static <T, U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy;//from w ww . j a v a2 s . c o m }
From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java
/** * recursively populate array out of hierarchy of JSON Objects * * @param arrayClass original array class * @param json json object in question * @return//from www . j a v a 2 s . co m */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static Object populateRecursive(Class arrayClass, Object json) throws JSONException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { if (arrayClass.isArray() && json instanceof JSONArray) { final int length = ((JSONArray) json).length(); final Class componentType = arrayClass.getComponentType(); Object retval = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(retval, i, populateRecursive(componentType, ((JSONArray) json).get(i))); } return retval; } else { // this is leaf object, JSON needs to be unmarshalled, if (json instanceof JSONObject) { return unmarshall((JSONObject) json, arrayClass); } else { // while all others can be returned verbatim return json; } } }
From source file:org.apache.geode.management.internal.cli.util.JsonUtil.java
private static Object toArray(Object value, Class<?> parameterType) throws GfJsonException { Class arrayComponentType = parameterType.getComponentType(); if (isPrimitiveOrWrapper(arrayComponentType)) { if (value instanceof JSONArray) { try { JSONArray jsonArray = (JSONArray) value; Object jArray = Array.newInstance(arrayComponentType, jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { Array.set(jArray, i, jsonArray.get(i)); }//from w ww . j a v a2s . co m return jArray; } catch (ArrayIndexOutOfBoundsException e) { throw new GfJsonException(e); } catch (IllegalArgumentException e) { throw new GfJsonException(e); } catch (JSONException e) { throw new GfJsonException(e); } } else { throw new GfJsonException("Expected JSONArray for array type"); } } else throw new GfJsonException( "Array contains non-primitive element. Non-primitive elements are not supported in json array"); }
From source file:org.apache.solr.hadoop.MorphlineBasicMiniMRTest.java
protected static <T> T[] concat(T[]... arrays) { if (arrays.length <= 0) { throw new IllegalArgumentException(); }/*from w ww .ja va 2s . c o m*/ Class clazz = null; int length = 0; for (T[] array : arrays) { clazz = array.getClass(); length += array.length; } T[] result = (T[]) Array.newInstance(clazz.getComponentType(), length); int pos = 0; for (T[] array : arrays) { System.arraycopy(array, 0, result, pos, array.length); pos += array.length; } return result; }
From source file:org.apache.myfaces.wap.renderkit.RendererUtils.java
public static Object convertUISelectManyToObject(FacesContext context, UISelectMany component, Object value) { if (!(value instanceof String[])) throw new ClassCastException("Selected value must be a String[] type."); String[] submittedValue = (String[]) value; ValueBinding vb = component.getValueBinding("value"); Class valueType = null; Class arrayComponentType = null; if (vb != null) { valueType = vb.getType(context); if (valueType != null && valueType.isArray()) { arrayComponentType = valueType.getComponentType(); }/* w w w. j a v a 2 s . c o m*/ } Converter converter = component.getConverter(); if (converter == null) { if (valueType == null) { // No converter, and no idea of expected type // --> return the submitted String array return submittedValue; } if (List.class.isAssignableFrom(valueType)) { // expected type is a List // --> according to javadoc of UISelectMany we assume that the element type // is java.lang.String, and copy the String array to a new List int len = submittedValue.length; List lst = new ArrayList(len); for (int i = 0; i < len; i++) { lst.add(submittedValue[i]); } return lst; } if (arrayComponentType == null) { throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array"); } if (String.class.equals(arrayComponentType)) return submittedValue; //No conversion needed for String type if (Object.class.equals(arrayComponentType)) return submittedValue; //No conversion for Object class try { converter = context.getApplication().createConverter(arrayComponentType); } catch (FacesException e) { log.error("No Converter for type " + arrayComponentType.getName() + " found"); return submittedValue; } } // Now, we have a converter... if (valueType == null) { // ...but have no idea of expected type // --> so let's convert it to an Object array int len = submittedValue.length; Object[] convertedValues = new Object[len]; for (int i = 0; i < len; i++) { convertedValues[i] = converter.getAsObject(context, component, submittedValue[i]); } return convertedValues; } if (List.class.isAssignableFrom(valueType)) { // Curious case: According to specs we should assume, that the element type // of this List is java.lang.String. But there is a Converter set for this // component. Because the user must know what he is doing, we will convert the values. int len = submittedValue.length; List lst = new ArrayList(len); for (int i = 0; i < len; i++) { lst.add(converter.getAsObject(context, component, submittedValue[i])); } return lst; } if (arrayComponentType == null) { throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array"); } if (arrayComponentType.isPrimitive()) { //primitive array int len = submittedValue.length; Object convertedValues = Array.newInstance(arrayComponentType, len); for (int i = 0; i < len; i++) { Array.set(convertedValues, i, converter.getAsObject(context, component, submittedValue[i])); } return convertedValues; } else { //Object array int len = submittedValue.length; Object[] convertedValues = new Object[len]; for (int i = 0; i < len; i++) { convertedValues[i] = converter.getAsObject(context, component, submittedValue[i]); } return convertedValues; } }
From source file:nl.ucan.navigate.NestedPath.java
private static Class getCollectionReturnType(String property, Class clasz) throws IntrospectionException { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(property, clasz); Method method = propertyDescriptor.getReadMethod(); Class klass = GenericCollectionTypeResolver.getCollectionReturnType(method); if (klass == null) { klass = GenericTypeResolver.resolveReturnType(method, clasz); if (klass.isArray()) { return klass.getComponentType(); } else {// ww w .j av a 2 s . com throw new IllegalStateException("unsupported return type"); } } else return klass; }
From source file:com.link_intersystems.lang.Conversions.java
/** * * @param from/*from w ww. j a v a 2 s.c o m*/ * @param to * @return true if from is a subtype of t. * @since 1.0.0.0 */ public static boolean isWideningReference(Class<?> from, Class<?> to) { Assert.notNull("from", from); Assert.notNull("to", to); if (from.isArray() && to.isArray()) { from = from.getComponentType(); to = to.getComponentType(); } return to.isAssignableFrom(from); }