List of usage examples for java.lang.reflect Array get
public static native Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:org.terasoluna.gfw.web.el.ObjectToMapConverter.java
/** * Convert any array to {@code List<Object>} * @param arrayObject array to convert// ww w . j a v a2 s. co m * @return list */ private static List<Object> arrayObjectToList(final Object arrayObject) { // 'arrayObject' must be array return new AbstractList<Object>() { @Override public Object get(int index) { return Array.get(arrayObject, index); } @Override public int size() { return Array.getLength(arrayObject); } }; }
From source file:com.bstek.dorado.view.output.JsonBuilder.java
/** * /* w ww. ja v a 2 s . c o m*/ */ public JsonBuilder value(Object o) { if (o == null) { outputValue("null", false); } else if (o instanceof Number || o instanceof Boolean) { if (o instanceof Float && (Float.isNaN((Float) o))) { outputValue("undefined", false); } if (o instanceof Double && (Double.isNaN((Double) o))) { outputValue("undefined", false); } else { outputValue(o.toString(), false); } } else if (o.getClass().isArray()) { array(); int len = Array.getLength(o); for (int i = 0; i < len; i++) { value(Array.get(o, i)); } endArray(); } else { String s = o.toString(); if (s.length() == 0) { outputValue("\"\"", false); } else { outputValue(StringEscapeUtils.escapeJavaScript(s), true); } } return this; }
From source file:com.twosigma.beaker.sqlsh.utils.QueryExecutor.java
private QueryResult executeQuery(int currentIterationIndex, BeakerParseResult queryLine, Connection conn, NamespaceClient namespaceClient) throws SQLException, ReadVariableException { QueryResult queryResult = new QueryResult(); String sql = queryLine.getResultQuery(); try (Statement statement = conn.createStatement()) { this.statement = statement; for (BeakerInputVar parameter : queryLine.getInputVars()) { if (parameter.getErrorMessage() != null) throw new ReadVariableException(parameter.getErrorMessage()); Object obj;// w w w. j a v a 2s . c om try { obj = namespaceClient.get(parameter.objectName); if (!parameter.isArray() && !parameter.isObject()) { sql = setObject(sql, obj); } else if (!parameter.isArray() && parameter.isObject()) { sql = setObject(sql, getValue(obj, parameter.getFieldName())); } else if (parameter.isArray()) { int index; if (currentIterationIndex > 0 && parameter.isAll()) { index = currentIterationIndex; } else { index = parameter.index; } if (!parameter.isObject()) { if (obj instanceof List) { sql = setObject(sql, ((List) obj).get(index)); } else if (obj.getClass().isArray()) { Object arrayElement = Array.get(obj, index); sql = setObject(sql, arrayElement); } } else { if (obj instanceof List) { sql = setObject(sql, getValue(((List) obj).get(index), parameter.getFieldName())); } else if (obj.getClass().isArray()) { Object arrayElement = Array.get(obj, index); sql = setObject(sql, getValue(arrayElement, parameter.getFieldName())); } } } } catch (Exception e) { throw new ReadVariableException(parameter.objectName, e); } } boolean hasResultSet = statement.execute(sql); if (hasResultSet) { ResultSet rs = statement.getResultSet(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { queryResult.getColumns().add(rs.getMetaData().getColumnName(i)); queryResult.getTypes().add(rs.getMetaData().getColumnClassName(i)); } while (rs.next()) { if (rs.getMetaData().getColumnCount() != 0) { List<Object> row = new ArrayList<Object>(); queryResult.getValues().add(row); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { if (java.sql.Date.class.getName().equals(rs.getMetaData().getColumnClassName(i))) { java.sql.Date sqlDate = rs.getDate(i); row.add(sqlDate == null ? null : new Date(sqlDate.getTime())); } else { row.add(rs.getObject(i)); } } } } } } catch (SQLException e) { //Logger.getLogger(QueryExecutor.class.getName()).log(Level.SEVERE, null, e); try { conn.rollback(); } catch (Exception e1) { //Logger.getLogger(QueryExecutor.class.getName()).log(Level.SEVERE, null, e1); } throw e; } return queryResult; }
From source file:org.eclipse.dataset.AbstractCompoundDataset.java
public static double[] toDoubleArray(final Object b, final int itemSize) { double[] result = null; // ensure array is of given length if (b instanceof Number) { result = new double[itemSize]; double val = ((Number) b).doubleValue(); for (int i = 0; i < itemSize; i++) result[i] = val; } else if (b instanceof double[]) { result = (double[]) b; if (result.length < itemSize) { result = new double[itemSize]; int ilen = result.length; for (int i = 0; i < ilen; i++) result[i] = ((double[]) b)[i]; }/* w ww . jav a 2 s . co m*/ } else if (b instanceof List<?>) { result = new double[itemSize]; List<?> jl = (List<?>) b; int ilen = jl.size(); if (ilen > 0 && !(jl.get(0) instanceof Number)) { logger.error("Given array was not of a numerical primitive type"); throw new IllegalArgumentException("Given array was not of a numerical primitive type"); } ilen = Math.min(itemSize, ilen); for (int i = 0; i < ilen; i++) { result[i] = toReal(jl.get(i)); } } else if (b.getClass().isArray()) { result = new double[itemSize]; int ilen = Array.getLength(b); if (ilen > 0 && !(Array.get(b, 0) instanceof Number)) { logger.error("Given array was not of a numerical primitive type"); throw new IllegalArgumentException("Given array was not of a numerical primitive type"); } ilen = Math.min(itemSize, ilen); for (int i = 0; i < ilen; i++) result[i] = ((Number) Array.get(b, i)).doubleValue(); } else if (b instanceof Complex) { if (itemSize > 2) { logger.error("Complex number will not fit in compound dataset"); throw new IllegalArgumentException("Complex number will not fit in compound dataset"); } Complex cb = (Complex) b; switch (itemSize) { default: case 0: break; case 1: result = new double[] { cb.getReal() }; break; case 2: result = new double[] { cb.getReal(), cb.getImaginary() }; break; } } else if (b instanceof Dataset) { Dataset db = (Dataset) b; if (db.getSize() != 1) { logger.error("Given dataset must have only one item"); throw new IllegalArgumentException("Given dataset must have only one item"); } return toDoubleArray(db.getObjectAbs(0), itemSize); } else if (b instanceof IDataset) { IDataset db = (Dataset) b; if (db.getSize() != 1) { logger.error("Given dataset must have only one item"); throw new IllegalArgumentException("Given dataset must have only one item"); } return toDoubleArray(db.getObject(new int[db.getRank()]), itemSize); } return result; }
From source file:org.dcm4che3.conf.core.misc.DeepEquals.java
/** * Deeply compare to Arrays []. Both arrays must be of the same type, same length, and all * elements within the arrays must be deeply equal in order to return true. * @param array1 [] type (Object[], String[], etc.) * @param array2 [] type (Object[], String[], etc.) * @param stack add items to compare to the Stack (Stack versus recursion) * @param visited Set of objects already compared (prevents cycles) * @return true if the two arrays are the same length and contain deeply equivalent items. *//* ww w. ja va2 s . c o m*/ private static boolean compareArrays(Object array1, Object array2, LinkedList stack, Set visited) { // Same instance check already performed... int len = Array.getLength(array1); if (len != Array.getLength(array2)) { return false; } // try sorting /*if (len >0 ) { if (Array.get(array1, 0) instanceof Comparable) { Class<?> c = Array.get(array1, 0).getClass(); if (ClassUtils.isPrimitiveOrWrapper(c)) { } else { Arrays.sort((Object[]) array1); Arrays.sort((Object[]) array2); } } }*/ for (int i = 0; i < len; i++) { DualKey dk = new DualKey(Array.get(array1, i), Array.get(array2, i)); if (!visited.contains(dk)) { // push contents for further comparison stack.addFirst(dk); } } return true; }
From source file:org.apache.struts.action.DynaActionForm.java
/** * <p>Return the value of an indexed property with the specified name. * </p>// ww w .j a v a 2 s. co m * * @param name Name of the property whose value is to be retrieved * @param index Index of the value to be retrieved * @return The value of an indexed property with the specified name. * @throws IllegalArgumentException if there is no property of the * specified name * @throws IllegalArgumentException if the specified property exists, but * is not indexed * @throws NullPointerException if no array or List has been * initialized for this property */ public Object get(String name, int index) { Object value = dynaValues.get(name); if (value == null) { throw new NullPointerException("No indexed value for '" + name + "[" + index + "]'"); } else if (value.getClass().isArray()) { return (Array.get(value, index)); } else if (value instanceof List) { return ((List) value).get(index); } else { throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'"); } }
From source file:org.eclipse.january.dataset.DTypeUtils.java
/** * Get dataset type from an object. The following are supported: Java Number objects, Apache common math Complex * objects, Java arrays and lists/*from www . ja v a2 s.com*/ * * @param obj * @return dataset type */ public static int getDTypeFromObject(Object obj) { int dtype = -1; if (obj == null) { return dtype; } if (obj instanceof List<?>) { List<?> jl = (List<?>) obj; int l = jl.size(); for (int i = 0; i < l; i++) { int ldtype = getDTypeFromObject(jl.get(i)); if (ldtype > dtype) { dtype = ldtype; } } } else if (obj.getClass().isArray()) { Class<?> ca = obj.getClass().getComponentType(); if (isClassSupportedAsElement(ca)) { return getDTypeFromClass(ca); } int l = Array.getLength(obj); for (int i = 0; i < l; i++) { Object lo = Array.get(obj, i); int ldtype = getDTypeFromObject(lo); if (ldtype > dtype) { dtype = ldtype; } } } else if (obj instanceof Dataset) { return ((Dataset) obj).getDType(); } else if (obj instanceof ILazyDataset) { dtype = getDTypeFromClass(((ILazyDataset) obj).getElementClass(), ((ILazyDataset) obj).getElementsPerItem()); } else { dtype = getDTypeFromClass(obj.getClass()); } return dtype; }
From source file:org.apache.openaz.xacml.std.annotations.RequestParser.java
public static Collection<AttributeValue<?>> extractValues(String datatype, Field field, Object object) throws IllegalArgumentException, IllegalAccessException, DataTypeException { ///*w ww . j a v a2s. c om*/ // Synchronize? // DataTypeFactory dtFactory = getDataTypeFactory(); if (dtFactory == null) { logger.error("Could not create data type factory"); return null; } // // This is what we will return // Collection<AttributeValue<?>> values = new ArrayList<AttributeValue<?>>(); // // Sanity check the object // Object fieldObject = field.get(object); if (logger.isDebugEnabled()) { logger.debug(fieldObject); } if (fieldObject == null) { if (logger.isDebugEnabled()) { logger.debug("field's object is null."); } return values; } // // Are we working with a collection or an array? // if (field.get(object) instanceof Collection || field.get(object) instanceof Map) { Collection<?> objects = (Collection<?>) field.get(object); if (objects == null || objects.isEmpty()) { if (logger.isTraceEnabled()) { logger.trace("empty collection"); } return values; } if (logger.isTraceEnabled()) { logger.trace("Object is a collection"); } for (Object obj : objects) { values.add(extractValue(datatype, obj)); } } else if (fieldObject.getClass().isArray()) { if (logger.isTraceEnabled()) { logger.trace("Object is an array"); } for (int i = 0; i < Array.getLength(fieldObject); i++) { values.add(extractValue(datatype, Array.get(fieldObject, i))); } } else { values.add(extractValue(datatype, field.get(object))); } return values; }