List of usage examples for java.lang.reflect Array setInt
public static native void setInt(Object array, int index, int i) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:com.flexive.faces.renderer.FxSelectRenderer.java
/** * Convert select many values to given array class * * @param context faces context//from www. j av a2s. c om * @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 w w w . java2 s. co m 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:com.sun.faces.config.ManagedBeanFactory.java
/** * <li><p> Call the property getter, if it exists.</p></li> * <p/>/*ww w . j a v a 2 s . c o m*/ * <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: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// w ww . j a v a2 s .co m Array.set(array, i, f.newInstance()); } return array; }; }
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 w w w . j a v a2s.c o m 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); } }
From source file:org.kchine.r.server.DirectJNI.java
public static Object getJavaArrayFromRArray__(RArray array) { int[] dim = array.getDim(); RVector vector = array.getValue();/*from ww w .j ava 2 s . c om*/ Class<?> componentType = null; if (vector instanceof RInteger) componentType = int.class; else if (vector instanceof RNumeric) componentType = double.class; else if (vector instanceof RChar) componentType = String.class; else if (vector instanceof RLogical) componentType = boolean.class; Object result = null; try { result = Array.newInstance(componentType, dim); } catch (Exception e) { e.printStackTrace(); } Vector<Integer> v = new Vector<Integer>(); int p = 1; for (int i = dim.length - 1; i > 0; --i) { p = p * dim[i]; v.add(0, p); } for (int bi = 0; bi < p * dim[0]; ++bi) { int bindex = bi; int[] indexes = new int[dim.length]; for (int i = 0; i < indexes.length - 1; ++i) { indexes[i] = bindex / v.elementAt(i); bindex = bindex % v.elementAt(i); } indexes[indexes.length - 1] = bindex; Object arrayTail = null; if (dim.length == 1) { arrayTail = result; } else { arrayTail = Array.get(result, indexes[0]); for (int i = 1; i < indexes.length - 1; ++i) arrayTail = Array.get(arrayTail, indexes[i]); } if (vector instanceof RInteger) Array.setInt(arrayTail, indexes[indexes.length - 1], ((RInteger) vector).getValue()[bi]); else if (vector instanceof RNumeric) Array.setDouble(arrayTail, indexes[indexes.length - 1], ((RNumeric) vector).getValue()[bi]); else if (vector instanceof RChar) Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[bi]); else if (vector instanceof RLogical) Array.setBoolean(arrayTail, indexes[indexes.length - 1], ((RLogical) vector).getValue()[bi]); } return result; }
From source file:org.kchine.r.server.DirectJNI.java
public static Object getJavaArrayFromRArray(RArray array) { int[] dim = array.getDim(); RVector vector = array.getValue();/*from w w w .j av a 2s. com*/ Class<?> componentType = null; if (vector instanceof RInteger) componentType = int.class; else if (vector instanceof RNumeric) componentType = double.class; else if (vector instanceof RChar) componentType = String.class; else if (vector instanceof RLogical) componentType = boolean.class; Object result = null; try { result = Array.newInstance(componentType, dim); } catch (Exception e) { e.printStackTrace(); } Vector<Integer> v1 = new Vector<Integer>(); int p1 = 1; for (int i = dim.length - 1; i > 0; --i) { p1 = p1 * dim[i]; v1.add(0, p1); } Vector<Integer> v2 = new Vector<Integer>(); int p2 = 1; for (int i = 0; i < dim.length - 1; ++i) { p2 = p2 * dim[i]; v2.add(0, p2); } for (int bi = 0; bi < p1 * dim[0]; ++bi) { int bindex = bi; int[] indexes = new int[dim.length]; for (int i = 0; i < indexes.length - 1; ++i) { indexes[i] = bindex / v1.elementAt(i); bindex = bindex % v1.elementAt(i); } indexes[indexes.length - 1] = bindex; Object arrayTail = null; if (dim.length == 1) { arrayTail = result; } else { arrayTail = Array.get(result, indexes[0]); for (int i = 1; i < indexes.length - 1; ++i) arrayTail = Array.get(arrayTail, indexes[i]); } int linearVectorIndex = 0; for (int i = (indexes.length - 1); i > 0; --i) linearVectorIndex += indexes[i] * v2.elementAt((indexes.length - 1) - i); linearVectorIndex += indexes[0]; // System.out.println("linearVectorIndex:"+linearVectorIndex); if (vector instanceof RInteger) Array.setInt(arrayTail, indexes[indexes.length - 1], ((RInteger) vector).getValue()[linearVectorIndex]); else if (vector instanceof RNumeric) Array.setDouble(arrayTail, indexes[indexes.length - 1], ((RNumeric) vector).getValue()[linearVectorIndex]); else if (vector instanceof RChar) Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[linearVectorIndex]); else if (vector instanceof RLogical) Array.setBoolean(arrayTail, indexes[indexes.length - 1], ((RLogical) vector).getValue()[linearVectorIndex]); } return result; }