List of usage examples for java.lang.reflect Array set
public static native void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:com.g3net.tool.CollectionUtils.java
public static Map<Object, Object[]> mergeMapIntoGivenMap(Map givenMap, Map map) { Set keys = givenMap.keySet(); Map<Object, Object[]> newMap = new HashMap<Object, Object[]>(); for (Object key : keys) { Object value = givenMap.get(key); if (value == null) continue; if (value.getClass().isArray()) { Object[] objs = ObjectUtils.toObjectArray(value); newMap.put(key, objs);//from w ww . j a va 2s . c o m } else { Object[] newArray = (Object[]) Array.newInstance(value.getClass(), 1); Array.set(newArray, 0, value); newMap.put(key, newArray); } } keys = map.keySet(); for (Object key : keys) { Object value = map.get(key); if (value == null) continue; if (value.getClass().isArray()) { Object[] objs = ObjectUtils.toObjectArray(value); Object[] old = newMap.get(key); if (old != null) { Object[] newObjs = (Object[]) ArrayUtils.append(old, objs); newMap.put(key, newObjs); } else { newMap.put(key, objs); } } else { Object[] newArray = (Object[]) Array.newInstance(value.getClass(), 1); Array.set(newArray, 0, value); Object[] old = newMap.get(key); if (old != null) { Object[] newObjs = (Object[]) ArrayUtils.append(old, newArray); newMap.put(key, newObjs); } else { newMap.put(key, newArray); } } } return newMap; }
From source file:org.openTwoFactor.clientExt.net.sf.ezmorph.array.CharacterObjectArrayMorpher.java
public Object morph(Object array) { if (array == null) { return null; }//from www.j ava 2 s. c o m if (CHARACTER_OBJECT_ARRAY_CLASS.isAssignableFrom(array.getClass())) { // no conversion needed return (Character[]) array; } if (array.getClass().isArray()) { int length = Array.getLength(array); int dims = getDimensions(array.getClass()); int[] dimensions = createDimensions(dims, length); Object result = Array.newInstance(Character.class, dimensions); if (dims == 1) { CharMorpher morpher = null; if (isUseDefault()) { if (defaultValue == null) { for (int index = 0; index < length; index++) { Array.set(result, index, null); } return result; } else { morpher = new CharMorpher(defaultValue.charValue()); } } else { morpher = new CharMorpher(); } for (int index = 0; index < length; index++) { Array.set(result, index, new Character(morpher.morph(Array.get(array, index)))); } } else { for (int index = 0; index < length; index++) { Array.set(result, index, morph(Array.get(array, index))); } } return result; } else { throw new MorphException("argument is not an array: " + array.getClass()); } }
From source file:com.xpfriend.fixture.cast.temp.ObjectOperatorBase.java
private Object toArrayInternal(Class<?> componentType, String textValue) { String[] textValues = textValue.split("\\|"); if (String.class.equals(componentType)) { return textValues; }/*from w ww . j ava 2s . c o m*/ Object values = Array.newInstance(componentType, textValues.length); for (int i = 0; i < Array.getLength(values); i++) { Array.set(values, i, TypeConverter.changeType(textValues[i], componentType)); } return values; }
From source file:egovframework.rte.itl.webservice.service.impl.MessageConverterImpl.java
@SuppressWarnings("unchecked") public Object convertToValueObject(Object source, Type type) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException { LOG.debug("convertToValueObject(source = " + source + ", type = " + type + ")"); if (type instanceof PrimitiveType) { LOG.debug("Type is a Primitive Type"); return source; } else if (type instanceof ListType) { LOG.debug("Type is a List Type"); ListType listType = (ListType) type; Object[] components = ((Collection<?>) source).toArray(); Class<?> arrayClass = classLoader.loadClass(listType); Object array = Array.newInstance(arrayClass.getComponentType(), components.length); for (int i = 0; i < components.length; i++) { Array.set(array, i, convertToValueObject(components[i], listType.getElementType())); }//from w w w. j ava 2 s . com return array; } else if (type instanceof RecordType) { LOG.debug("Type is a Record(Map) Type"); RecordType recordType = (RecordType) type; Map<String, Object> map = (Map<String, Object>) source; Class<?> recordClass = classLoader.loadClass(recordType); Object record = recordClass.newInstance(); for (Entry<String, Object> entry : map.entrySet()) { Object fieldValue = convertToValueObject(entry.getValue(), recordType.getFieldType(entry.getKey())); recordClass.getField(entry.getKey()).set(record, fieldValue); } return record; } LOG.error("Type is invalid"); throw new InstantiationException(); }
From source file:com.nfwork.dbfound.json.converter.NumberArrayConverter.java
public Object convert(Object array) { if (array == null) { return null; }//from ww w.java 2 s. co m if (array.getClass().isArray()) { int length = Array.getLength(array); int dims = getDimensions(array.getClass()); int[] dimensions = createDimensions(dims, length); Object result = Array.newInstance(type, dimensions); AbstractPrimitiveConverter converter = null; if (Byte.class.isAssignableFrom(type)) { converter = defaultValue != null && isUseDefault() ? new ByteConverter(defaultValue.byteValue()) : new ByteConverter(); } else if (Short.class.isAssignableFrom(type)) { converter = defaultValue != null && isUseDefault() ? new ShortConverter(defaultValue.shortValue()) : new ShortConverter(); } else if (Integer.class.isAssignableFrom(type)) { converter = defaultValue != null && isUseDefault() ? new IntConverter(defaultValue.intValue()) : new IntConverter(); } else if (Long.class.isAssignableFrom(type)) { converter = defaultValue != null && isUseDefault() ? new LongConverter(defaultValue.longValue()) : new LongConverter(); } else if (Float.class.isAssignableFrom(type)) { converter = defaultValue != null && isUseDefault() ? new FloatConverter(defaultValue.floatValue()) : new FloatConverter(); } else if (Double.class.isAssignableFrom(type)) { converter = defaultValue != null && isUseDefault() ? new DoubleConverter(defaultValue.doubleValue()) : new DoubleConverter(); } if (dims == 1) { for (int index = 0; index < length; index++) { try { Object value = Array.get(array, index); Object converted = MethodUtils.invokeMethod(converter, "convert", value); Array.set(result, index, converted); } catch (Exception e) { throw new ConversionException(e); } } } else { for (int index = 0; index < length; index++) { Array.set(result, index, convert(Array.get(array, index))); } } return result; } else { throw new ConversionException("argument is not an array: " + array.getClass()); } }
From source file:net.radai.beanz.codecs.ArrayCodec.java
public Object decodeArray(Collection<String> strCollection) { Object result = ReflectionUtil.instatiateArray(getElementType(), strCollection.size()); int i = 0;//from w w w . jav a2 s . com for (String strValue : strCollection) { Object value = elementCodec.decode(strValue); Array.set(result, i++, value); //this is primitive-safe } return result; }
From source file:org.openvpms.component.system.common.jxpath.OpenVPMSTypeConverter.java
@SuppressWarnings("unchecked") @Override/*from w w w. j a va 2 s . c o m*/ public Object convert(Object object, Class toType) { if (object == null) { if (toType.isPrimitive()) { return convertNullToPrimitive(toType); } return null; } if (toType == Object.class) { if (object instanceof NodeSet) { return convert(((NodeSet) object).getValues(), toType); } else if (object instanceof Pointer) { return convert(((Pointer) object).getValue(), toType); } return object; } Class fromType = object.getClass(); if (fromType.equals(toType) || toType.isAssignableFrom(fromType)) { return object; } if (fromType.isArray()) { int length = Array.getLength(object); if (toType.isArray()) { Class cType = toType.getComponentType(); Object array = Array.newInstance(cType, length); for (int i = 0; i < length; i++) { Object value = Array.get(object, i); Array.set(array, i, convert(value, cType)); } return array; } else if (Collection.class.isAssignableFrom(toType)) { Collection collection = allocateCollection(toType); for (int i = 0; i < length; i++) { collection.add(Array.get(object, i)); } return unmodifiableCollection(collection); } else { if (length > 0) { Object value = Array.get(object, 0); return convert(value, toType); } else { return convert("", toType); } } } else if (object instanceof Collection) { int length = ((Collection) object).size(); if (toType.isArray()) { Class cType = toType.getComponentType(); Object array = Array.newInstance(cType, length); Iterator it = ((Collection) object).iterator(); for (int i = 0; i < length; i++) { Object value = it.next(); Array.set(array, i, convert(value, cType)); } return array; } else if (Collection.class.isAssignableFrom(toType)) { Collection collection = allocateCollection(toType); collection.addAll((Collection) object); return unmodifiableCollection(collection); } else { if (length > 0) { Object value; if (object instanceof List) { value = ((List) object).get(0); } else { Iterator it = ((Collection) object).iterator(); value = it.next(); } return convert(value, toType); } else { return convert("", toType); } } } else if (object instanceof NodeSet) { return convert(((NodeSet) object).getValues(), toType); } else if (object instanceof Pointer) { return convert(((Pointer) object).getValue(), toType); } else if (toType == String.class) { return object.toString(); } else if (object instanceof Boolean) { if (toType == boolean.class) { return object; } boolean value = ((Boolean) object).booleanValue(); return allocateNumber(toType, value ? 1 : 0); } else if (object instanceof BigDecimal) { BigDecimal value = (BigDecimal) object; if (toType == boolean.class || toType == Boolean.class) { return value == BigDecimal.ZERO ? Boolean.FALSE : Boolean.TRUE; } if (toType.isPrimitive() || Number.class.isAssignableFrom(toType)) { return allocateNumber(toType, value); } } else if (object instanceof BigInteger) { BigInteger value = (BigInteger) object; if (toType == boolean.class || toType == Boolean.class) { return value == BigInteger.ZERO ? Boolean.FALSE : Boolean.TRUE; } if (toType.isPrimitive() || Number.class.isAssignableFrom(toType)) { return allocateNumber(toType, value); } } else if (object instanceof Number) { double value = ((Number) object).doubleValue(); if (toType == boolean.class || toType == Boolean.class) { return value == 0.0 ? Boolean.FALSE : Boolean.TRUE; } if (toType.isPrimitive() || Number.class.isAssignableFrom(toType)) { return allocateNumber(toType, value); } } else if (object instanceof Character) { if (toType == char.class) { return object; } } else if (object instanceof String) { Object value = convertStringToPrimitive(object, toType); if (value != null) { return value; } } Converter converter = ConvertUtils.lookup(toType); if (converter != null) { return converter.convert(toType, object); } throw new JXPathTypeConversionException("Cannot convert " + object.getClass() + " to " + toType); }
From source file:net.sf.qooxdoo.rpc.RemoteCallUtils.java
/** * Converts JSON types to "normal" java types. * * @param obj the object to convert (must not be * <code>null</code>, but can be * <code>JSONObject.NULL</code>). * @param targetType the desired target type (must not be * <code>null</code>). * * @return the converted object.//from ww w. j av a 2 s. c o m * * @exception IllegalArgumentException thrown if the desired * conversion is not possible. */ public Object toJava(Object obj, Class targetType) { try { if (obj == JSONObject.NULL) { if (targetType == Integer.TYPE || targetType == Double.TYPE || targetType == Boolean.TYPE || targetType == Long.TYPE || targetType == Float.TYPE) { // null does not work for primitive types throw new Exception(); } return null; } if (obj instanceof JSONArray) { Class componentType; if (targetType == null || targetType == Object.class) { componentType = null; } else { componentType = targetType.getComponentType(); } JSONArray jsonArray = (JSONArray) obj; int length = jsonArray.length(); Object retVal = Array.newInstance((componentType == null ? Object.class : componentType), length); for (int i = 0; i < length; ++i) { Array.set(retVal, i, toJava(jsonArray.get(i), componentType)); } return retVal; } if (obj instanceof JSONObject) { JSONObject jsonObject = (JSONObject) obj; JSONArray names = jsonObject.names(); if (targetType == Map.class || targetType == HashMap.class || targetType == null || targetType == Object.class) { HashMap retVal = new HashMap(); if (names != null) { int length = names.length(); String name; for (int i = 0; i < length; ++i) { name = names.getString(i); retVal.put(name, toJava(jsonObject.get(name), null)); } } return retVal; } Object bean; String requestedTypeName = jsonObject.optString("class", null); if (requestedTypeName != null) { Class clazz = resolveClassHint(requestedTypeName, targetType); if (clazz == null || !targetType.isAssignableFrom(clazz)) { throw new Exception(); } bean = clazz.newInstance(); // TODO: support constructor parameters } else { bean = targetType.newInstance(); } if (names != null) { int length = names.length(); String name; PropertyDescriptor desc; for (int i = 0; i < length; ++i) { name = names.getString(i); if (!"class".equals(name)) { desc = PropertyUtils.getPropertyDescriptor(bean, name); if (desc != null && desc.getWriteMethod() != null) { PropertyUtils.setSimpleProperty(bean, name, toJava(jsonObject.get(name), desc.getPropertyType())); } } } } return bean; } if (targetType == null || targetType == Object.class) { return obj; } Class actualTargetType; Class sourceType = obj.getClass(); if (targetType == Integer.TYPE) { actualTargetType = Integer.class; } else if (targetType == Boolean.TYPE) { actualTargetType = Boolean.class; } else if ((targetType == Double.TYPE || targetType == Double.class) && Number.class.isAssignableFrom(sourceType)) { return new Double(((Number) obj).doubleValue()); // TODO: maybe return obj directly if it's a Double } else if ((targetType == Float.TYPE || targetType == Float.class) && Number.class.isAssignableFrom(sourceType)) { return new Float(((Number) obj).floatValue()); } else if ((targetType == Long.TYPE || targetType == Long.class) && Number.class.isAssignableFrom(sourceType)) { return new Long(((Number) obj).longValue()); } else { actualTargetType = targetType; } if (!actualTargetType.isAssignableFrom(sourceType)) { throw new Exception(); } return obj; } catch (IllegalArgumentException e) { throw e; } catch (Exception e) { throw new IllegalArgumentException("Cannot convert " + (obj == null ? null : obj.getClass().getName()) + " to " + (targetType == null ? null : targetType.getName())); } }
From source file:org.apache.click.util.RequestTypeConverter.java
/** * Return the converted value for the given value object and target type. * * @param value the value object to convert * @param toType the target class type to convert the value to * @return a converted value into the specified type */// ww w. j av a2 s . c o m protected Object convertValue(Object value, Class<?> toType) { Object result = null; if (value != null) { // If array -> array then convert components of array individually 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) OgnlOps.longValue(value)); } else if ((toType == Double.class) || (toType == Double.TYPE)) { result = new Double(OgnlOps.doubleValue(value)); } else if ((toType == Boolean.class) || (toType == Boolean.TYPE)) { result = Boolean.valueOf(value.toString()); } else if ((toType == Byte.class) || (toType == Byte.TYPE)) { result = Byte.valueOf((byte) OgnlOps.longValue(value)); } else if ((toType == Character.class) || (toType == Character.TYPE)) { result = Character.valueOf((char) OgnlOps.longValue(value)); } else if ((toType == Short.class) || (toType == Short.TYPE)) { result = Short.valueOf((short) OgnlOps.longValue(value)); } else if ((toType == Long.class) || (toType == Long.TYPE)) { result = Long.valueOf(OgnlOps.longValue(value)); } else if ((toType == Float.class) || (toType == Float.TYPE)) { result = new Float(OgnlOps.doubleValue(value)); } else if (toType == BigInteger.class) { result = OgnlOps.bigIntValue(value); } else if (toType == BigDecimal.class) { result = bigDecValue(value); } else if (toType == String.class) { result = OgnlOps.stringValue(value); } else if (toType == java.util.Date.class) { long time = getTimeFromDateString(value.toString()); if (time > Long.MIN_VALUE) { result = new java.util.Date(time); } } else if (toType == java.sql.Date.class) { long time = getTimeFromDateString(value.toString()); if (time > Long.MIN_VALUE) { result = new java.sql.Date(time); } } else if (toType == java.sql.Time.class) { long time = getTimeFromDateString(value.toString()); if (time > Long.MIN_VALUE) { result = new java.sql.Time(time); } } else if (toType == java.sql.Timestamp.class) { long time = getTimeFromDateString(value.toString()); if (time > Long.MIN_VALUE) { result = new java.sql.Timestamp(time); } } } } else { if (toType.isPrimitive()) { result = OgnlRuntime.getPrimitiveDefaultValue(toType); } } return result; }
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 {/* w ww . j ava 2 s . c om*/ 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; }