List of usage examples for java.lang.reflect Array setShort
public static native void setShort(Object array, int index, short s) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:Main.java
public static void main(String args[]) { short[] array = new short[] { 1, 2, 3 }; Array.setShort(array, 1, (short) 9); System.out.println(Arrays.toString(array)); }
From source file:ArrayTroubleToo.java
public static void main(String... args) { Object o = new int[2]; Array.setShort(o, 0, (short) 2); // widening, succeeds Array.setLong(o, 1, 2L); // narrowing, fails }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
private static void loadObject(Object array, Class type, int index, JSONArray jsonArray) throws JSONSerializationException, JSONException { if (type.isPrimitive()) { if (type == Integer.TYPE) { Array.setInt(array, index, jsonArray.getInt(index)); } else if (type == Long.TYPE) { Array.setLong(array, index, jsonArray.getInt(index)); } else if (type == Short.TYPE) { Array.setShort(array, index, (short) jsonArray.getInt(index)); } else if (type == Boolean.TYPE) { Array.setBoolean(array, index, jsonArray.getBoolean(index)); } else if (type == Double.TYPE) { Array.setDouble(array, index, jsonArray.getDouble(index)); } else if (type == Float.TYPE) { Array.setFloat(array, index, (float) jsonArray.getDouble(index)); } else if (type == Character.TYPE) { char ch = jsonArray.getString(index).charAt(0); Array.setChar(array, index, ch); } else if (type == Byte.TYPE) { Array.setByte(array, index, (byte) jsonArray.getInt(index)); } else {/*from w w w. ja v a 2 s . c om*/ throw new JSONSerializationException("Unknown primitive: " + type); } } else if (type == String.class) { Array.set(array, index, jsonArray.getString(index)); } else if (JSONSerializable.class.isAssignableFrom(type)) { JSONObject jObj = jsonArray.getJSONObject(index); JSONSerializable serObj = deSerialize(type, jObj); Array.set(array, index, serObj); } else if (type.isArray()) { Class componentClass = type.getComponentType(); JSONArray subArray = jsonArray.getJSONArray(index); int len = subArray.length(); Object newArray = Array.newInstance(componentClass, len); for (int k = 0; k < len; ++k) { loadObject(newArray, componentClass, k, subArray); } } }
From source file:com.icesoft.faces.renderkit.dom_html_basic.MenuRenderer.java
protected Object convertArray(FacesContext facesContext, UISelectMany uiSelectMany, Class componentType, String[] newSubmittedValues) throws ConverterException { // component type of String means no conversion is necessary if (componentType.equals(String.class)) { return newSubmittedValues; }//from w w w. j a v a 2s . co m // if newSubmittedValue is null return zero-length array if (newSubmittedValues == null) { return Array.newInstance(componentType, 0); } // create the array with specified component length int numberOfValues = newSubmittedValues.length; Object convertedValues = Array.newInstance(componentType, numberOfValues); // Determine if a converter is explicitly registered with the component Converter converter = uiSelectMany.getConverter(); if (converter == null) { // Determine if there is a default converter for the class converter = getConverterForClass(componentType); } if (converter == null) { // we don't need to convert base Object types if (componentType.equals(Object.class)) { return newSubmittedValues; } else { throw new ConverterException("Converter is null"); } } for (int index = 0; index < numberOfValues; index++) { // convert the next element Object nextConvertedElement = converter.getAsObject(facesContext, uiSelectMany, newSubmittedValues[index]); if (!componentType.isPrimitive()) { Array.set(convertedValues, index, nextConvertedElement); } else if (componentType.equals(Boolean.TYPE)) { Array.setBoolean(convertedValues, index, ((Boolean) nextConvertedElement).booleanValue()); } else if (componentType.equals(Integer.TYPE)) { Array.setInt(convertedValues, index, ((Integer) nextConvertedElement).intValue()); } else if (componentType.equals(Long.TYPE)) { Array.setLong(convertedValues, index, ((Long) nextConvertedElement).longValue()); } else if (componentType.equals(Short.TYPE)) { Array.setShort(convertedValues, index, ((Short) nextConvertedElement).shortValue()); } else if (componentType.equals(Byte.TYPE)) { Array.setByte(convertedValues, index, ((Byte) nextConvertedElement).byteValue()); } else if (componentType.equals(Float.TYPE)) { Array.setFloat(convertedValues, index, ((Float) nextConvertedElement).floatValue()); } else if (componentType.equals(Double.TYPE)) { Array.setDouble(convertedValues, index, ((Double) nextConvertedElement).doubleValue()); } else if (componentType.equals(Character.TYPE)) { Array.setChar(convertedValues, index, ((Character) nextConvertedElement).charValue()); } } return convertedValues; }
From source file:com.sun.faces.renderkit.html_basic.MenuRenderer.java
protected Object handleArrayCase(FacesContext context, UISelectMany uiSelectMany, Class arrayClass, String[] newValues) throws ConverterException { Object result = null;/*w ww . jav a2 s .c o m*/ Class elementType = null; Converter converter = null; int i = 0, len = (null != newValues ? newValues.length : 0); elementType = arrayClass.getComponentType(); // Optimization: If the elementType is String, we don't need // conversion. Just return newValues. if (elementType.equals(String.class)) { return newValues; } try { result = Array.newInstance(elementType, len); } catch (Exception e) { throw new ConverterException(e); } // bail out now if we have no new values, returning our // oh-so-useful zero-length array. if (null == newValues) { return result; } // obtain a converter. // attached converter takes priority if (null == (converter = uiSelectMany.getConverter())) { // Otherwise, look for a by-type converter if (null == (converter = Util.getConverterForClass(elementType, context))) { // if that fails, and the attached values are of Object type, // we don't need conversion. if (elementType.equals(Object.class)) { return newValues; } String valueStr = ""; for (i = 0; i < newValues.length; i++) { valueStr = valueStr + " " + newValues[i]; } Object[] params = { valueStr, "null Converter" }; throw new ConverterException(Util.getExceptionMessage(Util.CONVERSION_ERROR_MESSAGE_ID, params)); } } Util.doAssert(null != result); if (elementType.isPrimitive()) { for (i = 0; i < len; i++) { if (elementType.equals(Boolean.TYPE)) { Array.setBoolean(result, i, ((Boolean) converter.getAsObject(context, uiSelectMany, newValues[i])).booleanValue()); } else if (elementType.equals(Byte.TYPE)) { Array.setByte(result, i, ((Byte) converter.getAsObject(context, uiSelectMany, newValues[i])).byteValue()); } else if (elementType.equals(Double.TYPE)) { Array.setDouble(result, i, ((Double) converter.getAsObject(context, uiSelectMany, newValues[i])).doubleValue()); } else if (elementType.equals(Float.TYPE)) { Array.setFloat(result, i, ((Float) converter.getAsObject(context, uiSelectMany, newValues[i])).floatValue()); } else if (elementType.equals(Integer.TYPE)) { Array.setInt(result, i, ((Integer) converter.getAsObject(context, uiSelectMany, newValues[i])).intValue()); } else if (elementType.equals(Character.TYPE)) { Array.setChar(result, i, ((Character) converter.getAsObject(context, uiSelectMany, newValues[i])).charValue()); } else if (elementType.equals(Short.TYPE)) { Array.setShort(result, i, ((Short) converter.getAsObject(context, uiSelectMany, newValues[i])).shortValue()); } else if (elementType.equals(Long.TYPE)) { Array.setLong(result, i, ((Long) converter.getAsObject(context, uiSelectMany, newValues[i])).longValue()); } } } else { for (i = 0; i < len; i++) { if (log.isDebugEnabled()) { Object converted = converter.getAsObject(context, uiSelectMany, newValues[i]); log.debug("String value: " + newValues[i] + " converts to : " + converted.toString()); } Array.set(result, i, converter.getAsObject(context, uiSelectMany, newValues[i])); } } return result; }
From source file:com.flexive.faces.renderer.FxSelectRenderer.java
/** * Convert select many values to given array class * * @param context faces context/* w w w. ja v a2 s . co m*/ * @param uiSelectMany select many component * @param arrayClass the array class * @param newValues new values to convert * @return converted values * @throws ConverterException on errors */ protected Object convertSelectManyValues(FacesContext context, UISelectMany uiSelectMany, Class arrayClass, String[] newValues) throws ConverterException { Object result; Converter converter; int len = (null != newValues ? newValues.length : 0); Class elementType = arrayClass.getComponentType(); // Optimization: If the elementType is String, we don't need // conversion. Just return newValues. if (elementType.equals(String.class)) return newValues; try { result = Array.newInstance(elementType, len); } catch (Exception e) { throw new ConverterException(e); } // bail out now if we have no new values, returning our // oh-so-useful zero-length array. if (null == newValues) return result; // obtain a converter. // attached converter takes priority if (null == (converter = uiSelectMany.getConverter())) { // Otherwise, look for a by-type converter if (null == (converter = FxJsfComponentUtils.getConverterForClass(elementType, context))) { // if that fails, and the attached values are of Object type, // we don't need conversion. if (elementType.equals(Object.class)) return newValues; StringBuffer valueStr = new StringBuffer(); for (int i = 0; i < len; i++) { if (i == 0) valueStr.append(newValues[i]); else valueStr.append(' ').append(newValues[i]); } throw new ConverterException("Could not get a converter for " + String.valueOf(valueStr)); } } if (elementType.isPrimitive()) { for (int i = 0; i < len; i++) { if (elementType.equals(Boolean.TYPE)) { Array.setBoolean(result, i, ((Boolean) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Byte.TYPE)) { Array.setByte(result, i, ((Byte) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Double.TYPE)) { Array.setDouble(result, i, ((Double) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Float.TYPE)) { Array.setFloat(result, i, ((Float) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Integer.TYPE)) { Array.setInt(result, i, ((Integer) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Character.TYPE)) { Array.setChar(result, i, ((Character) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Short.TYPE)) { Array.setShort(result, i, ((Short) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Long.TYPE)) { Array.setLong(result, i, ((Long) converter.getAsObject(context, uiSelectMany, newValues[i]))); } } } else { for (int i = 0; i < len; i++) Array.set(result, i, converter.getAsObject(context, uiSelectMany, newValues[i])); } return result; }
From source file:org.crank.javax.faces.component.MenuRenderer.java
protected Object convertSelectManyValues(FacesContext context, UISelectMany uiSelectMany, Class<?> arrayClass, String[] newValues) throws ConverterException { Object result = null;//from ww w.jav a 2 s . com Converter converter = null; int len = (null != newValues ? newValues.length : 0); Class<?> elementType = arrayClass.getComponentType(); // Optimization: If the elementType is String, we don't need // conversion. Just return newValues. if (elementType.equals(String.class)) { return newValues; } try { result = Array.newInstance(elementType, len); } catch (Exception e) { throw new ConverterException(e); } // bail out now if we have no new values, returning our // oh-so-useful zero-length array. if (null == newValues) { return result; } // obtain a converter. // attached converter takes priority if (null == (converter = uiSelectMany.getConverter())) { // Otherwise, look for a by-type converter if (null == (converter = Util.getConverterForClass(elementType, context))) { // if that fails, and the attached values are of Object type, // we don't need conversion. if (elementType.equals(Object.class)) { return newValues; } StringBuffer valueStr = new StringBuffer(); for (int i = 0; i < len; i++) { if (i == 0) { valueStr.append(newValues[i]); } else { valueStr.append(' ').append(newValues[i]); } } Object[] params = { valueStr.toString(), "null Converter" }; throw new ConverterException( MessageUtils.getExceptionMessage(MessageUtils.CONVERSION_ERROR_MESSAGE_ID, params)); } } assert (null != result); if (elementType.isPrimitive()) { for (int i = 0; i < len; i++) { if (elementType.equals(Boolean.TYPE)) { Array.setBoolean(result, i, ((Boolean) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Byte.TYPE)) { Array.setByte(result, i, ((Byte) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Double.TYPE)) { Array.setDouble(result, i, ((Double) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Float.TYPE)) { Array.setFloat(result, i, ((Float) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Integer.TYPE)) { Array.setInt(result, i, ((Integer) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Character.TYPE)) { Array.setChar(result, i, ((Character) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Short.TYPE)) { Array.setShort(result, i, ((Short) converter.getAsObject(context, uiSelectMany, newValues[i]))); } else if (elementType.equals(Long.TYPE)) { Array.setLong(result, i, ((Long) converter.getAsObject(context, uiSelectMany, newValues[i]))); } } } else { for (int i = 0; i < len; i++) { if (logger.isLoggable(Level.FINE)) { Object converted = converter.getAsObject(context, uiSelectMany, newValues[i]); logger.fine("String value: " + newValues[i] + " converts to : " + converted.toString()); } Array.set(result, i, converter.getAsObject(context, uiSelectMany, newValues[i])); } } return result; }
From source file:org.janusgraph.graphdb.serializer.SerializerTest.java
License:asdf
private static Factory getArrayFactory(final Class ct, final Factory f) { return () -> { final int length = random.nextInt(100); final Object array = Array.newInstance(ct, length); for (int i = 0; i < length; i++) { if (ct == boolean.class) Array.setBoolean(array, i, (Boolean) f.newInstance()); else if (ct == byte.class) Array.setByte(array, i, (Byte) f.newInstance()); else if (ct == short.class) Array.setShort(array, i, (Short) f.newInstance()); else if (ct == int.class) Array.setInt(array, i, (Integer) f.newInstance()); else if (ct == long.class) Array.setLong(array, i, (Long) f.newInstance()); else if (ct == float.class) Array.setFloat(array, i, (Float) f.newInstance()); else if (ct == double.class) Array.setDouble(array, i, (Double) f.newInstance()); else if (ct == char.class) Array.setChar(array, i, (Character) f.newInstance()); else//from w w w . j a va2 s . c o m Array.set(array, i, f.newInstance()); } return array; }; }
From source file:com.sun.faces.config.ManagedBeanFactory.java
/** * <li><p> Call the property getter, if it exists.</p></li> * <p/>/*from w ww . j a va 2 s .c om*/ * <li><p>If the getter returns null or doesn't exist, create a * java.util.ArrayList(), otherwise use the returned Object (an array or * a java.util.List).</p></li> * <p/> * <li><p>If a List was returned or created in step 2), add all * elements defined by nested <value> elements in the order * they are listed, converting values defined by nested * <value> elements to the type defined by * <value-class>. If a <value-class> is not defined, use * the value as-is (i.e., as a java.lang.String). Add null for each * <null-value> element.</p></li> * <p/> * <li><p> If an array was returned in step 2), create a * java.util.ArrayList and copy all elements from the returned array to * the new List, auto-boxing elements of a primitive type. Add all * elements defined by nested <value> elements as described in step * 3).</p></li> * <p/> * <li><p> If a new java.util.List was created in step 2) and the * property is of type List, set the property by calling the setter * method, or log an error if there is no setter method.</p></li> * <p/> * <li><p> If a new java.util.List was created in step 4), convert * the * List to array of the same type as the property and set the * property by * calling the setter method, or log an error if there * is no setter * method.</p></li> */ private void setArrayOrListPropertiesIntoBean(Object bean, ManagedPropertyBean property) throws Exception { Object result = null; boolean getterIsNull = true, getterIsArray = false; List valuesForBean = null; Class valueType = java.lang.String.class, propertyType = null; String propertyName = property.getPropertyName(); try { // see if there is a getter result = PropertyUtils.getProperty(bean, propertyName); getterIsNull = (null == result) ? true : false; propertyType = PropertyUtils.getPropertyType(bean, propertyName); getterIsArray = propertyType.isArray(); } catch (NoSuchMethodException nsme) { // it's valid to not have a getter. } // the property has to be either a List or Array if (!getterIsArray) { if (null != propertyType && !java.util.List.class.isAssignableFrom(propertyType)) { throw new FacesException( Util.getExceptionMessageString(Util.MANAGED_BEAN_CANNOT_SET_LIST_ARRAY_PROPERTY_ID, new Object[] { propertyName, managedBean.getManagedBeanName() })); } } // // Deal with the possibility of the getter returning existing // values. // // if the getter returned non-null if (!getterIsNull) { // if what it returned was an array if (getterIsArray) { valuesForBean = new ArrayList(); for (int i = 0, len = Array.getLength(result); i < len; i++) { // add the existing values valuesForBean.add(Array.get(result, i)); } } else { // if what it returned was not a List if (!(result instanceof List)) { // throw an exception throw new FacesException( Util.getExceptionMessageString(Util.MANAGED_BEAN_EXISTING_VALUE_NOT_LIST_ID, new Object[] { propertyName, managedBean.getManagedBeanName() })); } valuesForBean = (List) result; } } else { // getter returned null result = valuesForBean = new ArrayList(); } // at this point valuesForBean contains the existing values from // the bean, or no values if the bean had no values. In any // case, we can proceed to add values from the config file. valueType = copyListEntriesFromConfigToList(property.getListEntries(), valuesForBean); // at this point valuesForBean has the values to be set into the // bean. if (getterIsArray) { // convert back to Array result = Array.newInstance(valueType, valuesForBean.size()); for (int i = 0, len = valuesForBean.size(); i < len; i++) { if (valueType == Boolean.TYPE) { Array.setBoolean(result, i, ((Boolean) valuesForBean.get(i)).booleanValue()); } else if (valueType == Byte.TYPE) { Array.setByte(result, i, ((Byte) valuesForBean.get(i)).byteValue()); } else if (valueType == Double.TYPE) { Array.setDouble(result, i, ((Double) valuesForBean.get(i)).doubleValue()); } else if (valueType == Float.TYPE) { Array.setFloat(result, i, ((Float) valuesForBean.get(i)).floatValue()); } else if (valueType == Integer.TYPE) { Array.setInt(result, i, ((Integer) valuesForBean.get(i)).intValue()); } else if (valueType == Character.TYPE) { Array.setChar(result, i, ((Character) valuesForBean.get(i)).charValue()); } else if (valueType == Short.TYPE) { Array.setShort(result, i, ((Short) valuesForBean.get(i)).shortValue()); } else if (valueType == Long.TYPE) { Array.setLong(result, i, ((Long) valuesForBean.get(i)).longValue()); } else { Array.set(result, i, valuesForBean.get(i)); } } } else { result = valuesForBean; } if (getterIsNull || getterIsArray) { PropertyUtils.setProperty(bean, propertyName, result); } }
From source file:alice.tuprolog.lib.OOLibrary.java
public boolean java_array_set_primitive_3(PTerm obj_id, PTerm i, PTerm what) throws JavaException { Struct objId = (Struct) obj_id.getTerm(); Number index = (Number) i.getTerm(); what = what.getTerm();/*from ww w . j ava 2s. c om*/ Object obj = null; if (!index.isInteger()) { throw new JavaException(new IllegalArgumentException(index.toString())); } try { Class<?> cl = null; String objName = alice.util.Tools.removeApices(objId.toString()); obj = currentObjects.get(objName); if (obj != null) { cl = obj.getClass(); } else { throw new JavaException(new IllegalArgumentException(objId.toString())); } if (!cl.isArray()) { throw new JavaException(new IllegalArgumentException(objId.toString())); } String name = cl.toString(); switch (name) { case "class [I": { if (!(what instanceof Number)) { throw new JavaException(new IllegalArgumentException(what.toString())); } byte v = (byte) ((Number) what).intValue(); Array.setInt(obj, index.intValue(), v); break; } case "class [D": { if (!(what instanceof Number)) { throw new JavaException(new IllegalArgumentException(what.toString())); } double v = ((Number) what).doubleValue(); Array.setDouble(obj, index.intValue(), v); break; } case "class [F": { if (!(what instanceof Number)) { throw new JavaException(new IllegalArgumentException(what.toString())); } float v = ((Number) what).floatValue(); Array.setFloat(obj, index.intValue(), v); break; } case "class [L": { if (!(what instanceof Number)) { throw new JavaException(new IllegalArgumentException(what.toString())); } long v = ((Number) what).longValue(); Array.setFloat(obj, index.intValue(), v); break; } case "class [C": { String s = what.toString(); Array.setChar(obj, index.intValue(), s.charAt(0)); break; } case "class [Z": { String s = what.toString(); switch (s) { case "true": Array.setBoolean(obj, index.intValue(), true); break; case "false": Array.setBoolean(obj, index.intValue(), false); break; default: throw new JavaException(new IllegalArgumentException(what.toString())); } break; } case "class [B": { if (!(what instanceof Number)) { throw new JavaException(new IllegalArgumentException(what.toString())); } int v = ((Number) what).intValue(); Array.setByte(obj, index.intValue(), (byte) v); break; } case "class [S": { if (!(what instanceof Number)) { throw new JavaException(new IllegalArgumentException(what.toString())); } short v = (short) ((Number) what).intValue(); Array.setShort(obj, index.intValue(), v); break; } default: throw new JavaException(new Exception()); } return true; } catch (Exception ex) { throw new JavaException(ex); } }