List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:com.haulmont.cuba.web.app.ui.jmxcontrol.util.AttributeEditor.java
protected List objectToStringArray(Object value) { if (value instanceof Collection) { return new ArrayList((Collection) value); }/*from w w w . ja v a 2 s . c om*/ int length = Array.getLength(value); List<Object> output = new ArrayList<>(length); for (int i = 0; i < length; i++) { output.add(Array.get(value, i)); } return output; }
From source file:de.cosmocode.lucene.DefaultLuceneQuery.java
@Override protected DefaultLuceneQuery addArgumentAsArray(Object values, final QueryModifier modifier) { if (values == null || !values.getClass().isArray() || Array.getLength(values) == 0) { setLastSuccessful(false);//from www . j a va 2 s .c om return this; } final int arrayLength = Array.getLength(values); beforeIteration(modifier); // add all items final QueryModifier valueModifier = modifier.getMultiValueModifier(); for (int i = 0; i < arrayLength; i++) { addArgument(Array.get(values, i), valueModifier); } afterIteration(); return this; }
From source file:com.google.corp.productivity.specialprojects.android.comm.SerializableCookieStore.java
private static void putJs(JSONArray array, int i, String key, Object value) { if (value != null) { JSONObject object = array.optJSONObject(i); try {//from w w w . java 2 s . com if (object == null) { object = new JSONObject(); array.put(i, object); } if (value.getClass().isArray()) { // android's version of org.json doesn't instantiate array // automatically. JSONArray newArray = new JSONArray(); int n = Array.getLength(value); for (int j = 0; j < n; j++) { newArray.put(Array.get(value, j)); } value = newArray; } object.put(key, value); } catch (JSONException e) { // Ignore. } } }
From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.ArrayConverter.java
/** * {@inheritDoc}// ww w .j av a 2 s .com */ @Override public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { boolean canConvert = expectedClass.isArray(); if (canConvert) { final Class<?> componentClass = expectedClass.getComponentType(); if (value instanceof Iterable<?>) { final Collection<?> coll = (Collection<?>) value; for (final Object element : coll) { canConvert = canConvert && globalDelegate.canConvertValueForJava(element, componentClass); if (!canConvert) { break; } } } else if (value.getClass().isArray()) { final int length = Array.getLength(value); for (int idx = 0; idx < length && canConvert; idx++) { canConvert = canConvert && globalDelegate.canConvertValueForJava(Array.get(value, idx), componentClass); } } else { canConvert = false; } } return canConvert; }
From source file:com.manydesigns.elements.forms.TableForm.java
public void readFromObject(Object obj) { Class clazz = obj.getClass(); if (clazz.isArray()) { // Tratta obj come un array // Scorre tutti gli ellementi dell'array obj, // indipendentemente da quante righe ci sono nell table form. // Eventualmente lancia Eccezione. final int arrayLength = Array.getLength(obj); for (int i = 0; i < arrayLength; i++) { Object currentObj = Array.get(obj, i); rows[i].readFromObject(currentObj); }/* w w w. j ava2 s . co m*/ // Scorre le rimanenti righe del table form, // passano null come ottetto di bind. for (int i = arrayLength; i < rows.length; i++) { rows[i].readFromObject(null); } } else if (Collection.class.isAssignableFrom(clazz)) { // Tratta obj come collection Collection collection = (Collection) obj; int i = 0; for (Object currentObj : collection) { rows[i].readFromObject(currentObj); i++; } for (; i < rows.length; i++) { rows[i].readFromObject(null); } } }
From source file:ArraysX.java
/** * Duplicates the specified array./*ww w. j ava 2s .co m*/ * * <p>The array could be an array of objects or primiitives. * * @param ary the array * @param jb the beginning index (included) * @param je the ending index (excluded) * @return an array duplicated from ary * @exception IllegalArgumentException if ary is not any array * @exception IndexOutOfBoundsException if out of bounds */ public static final Object duplicate(Object ary, int jb, int je) { int len = Array.getLength(ary); if (jb < 0 || je > len || jb > je) throw new IndexOutOfBoundsException(jb + " or " + je + " exceeds " + len); len = je - jb; Object dst = Array.newInstance(ary.getClass().getComponentType(), len); System.arraycopy(ary, jb, dst, 0, len); return dst; }
From source file:edu.umn.cs.spatialHadoop.nasa.HDFRecordReader3.java
/** * Reads next NASA object from the array * @param key/*from w w w . j a v a 2 s . c o m*/ * @param shape * @return * @throws IOException */ protected boolean nextObject(NASADataset key, NASAShape shape) throws IOException { if (dataArray == null) return false; // Key doesn't need to be changed because all points in the same dataset // have the same key while (position < Array.getLength(dataArray)) { // Set the x and y to longitude and latitude by doing the correct projection int row = position / nasaDataset.resolution; int col = position % nasaDataset.resolution; if (shape instanceof Point) { Point pt = (Point) shape; pt.y = (90 - nasaDataset.v * 10) - (double) row * 10 / nasaDataset.resolution; pt.x = (nasaDataset.h * 10 - 180) + (double) (col) * 10 / nasaDataset.resolution; pt.x /= Math.cos(pt.y * Math.PI / 180); } else if (shape instanceof Rectangle) { Rectangle rect = (Rectangle) shape; rect.y2 = (90 - nasaDataset.v * 10) - (double) row * 10 / nasaDataset.resolution; rect.y1 = (90 - nasaDataset.v * 10) - (double) (row + 1) * 10 / nasaDataset.resolution; double[] xs = new double[4]; xs[0] = xs[1] = (nasaDataset.h * 10 - 180) + (double) (col) * 10 / nasaDataset.resolution; xs[2] = xs[3] = (nasaDataset.h * 10 - 180) + (double) (col + 1) * 10 / nasaDataset.resolution; // Project all four corners and select the min-max for the rectangle xs[0] /= Math.cos(rect.y1 * Math.PI / 180); xs[1] /= Math.cos(rect.y2 * Math.PI / 180); xs[2] /= Math.cos(rect.y1 * Math.PI / 180); xs[3] /= Math.cos(rect.y2 * Math.PI / 180); rect.x1 = rect.x2 = xs[0]; for (double x : xs) { if (x < rect.x1) rect.x1 = x; if (x > rect.x2) rect.x2 = x; } } // if (projector != null) // projector.project(shape); shape.setTimestamp(key.time); // Read next value shape.setValue(dataArray[position]); position++; if (!skipFillValue || shape.getValue() != fillValue) return true; } return false; }
From source file:com.dnw.json.J.java
/** * Tries to convert the given value to a JSON compatible value. A global internal cache will be * used to improve the performance.// w ww . ja va 2s . co m * * @author manbaum * @since Oct 11, 2014 * @param value the value to convert. * @return the result value. */ public final static Object convert(Object value) { if (value == null) return value; else if (value instanceof CharSequence) return ((CharSequence) value).toString(); else if (value instanceof Character) return value.toString(); else if (value instanceof Number) return value; else if (value instanceof Boolean) return value; else if (value instanceof M) return ((M) value).map; else if (value instanceof Map) { Map<?, ?> src = (Map<?, ?>) value; Map<String, Object> map = new HashMap<String, Object>(); for (Map.Entry<?, ?> e : src.entrySet()) { String key = String.valueOf(e.getKey()); map.put(key, convert(e.getValue())); } return map; } else if (value instanceof L) return ((L) value).list; else if (value instanceof Iterable) { Iterable<?> src = (List<?>) value; List<Object> list = new ArrayList<Object>(); for (Object v : src) { list.add(convert(v)); } return list; } else if (value.getClass().isArray()) { List<Object> list = new ArrayList<Object>(); int length = Array.getLength(value); for (int i = 0; i < length; i++) { list.add(convert(Array.get(value, i))); } return list; } else return tryConverter(value); }
From source file:es.caib.zkib.jxpath.util.BasicTypeConverter.java
/** * Converts the supplied object to the specified * type. Throws a runtime exception if the conversion is * not possible.//from ww w . j a v a2s. c o m * @param object to convert * @param toType destination class * @return converted object */ public Object convert(Object object, final Class toType) { if (object == null) { return toType.isPrimitive() ? convertNullToPrimitive(toType) : null; } if (toType == Object.class) { if (object instanceof NodeSet) { return convert(((NodeSet) object).getValues(), toType); } if (object instanceof Pointer) { return convert(((Pointer) object).getValue(), toType); } return object; } final Class useType = TypeUtils.wrapPrimitive(toType); Class fromType = object.getClass(); if (useType.isAssignableFrom(fromType)) { return object; } if (fromType.isArray()) { int length = Array.getLength(object); if (useType.isArray()) { Class cType = useType.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; } if (Collection.class.isAssignableFrom(useType)) { Collection collection = allocateCollection(useType); for (int i = 0; i < length; i++) { collection.add(Array.get(object, i)); } return unmodifiableCollection(collection); } if (length > 0) { Object value = Array.get(object, 0); return convert(value, useType); } return convert("", useType); } if (object instanceof Collection) { int length = ((Collection) object).size(); if (useType.isArray()) { Class cType = useType.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; } if (Collection.class.isAssignableFrom(useType)) { Collection collection = allocateCollection(useType); collection.addAll((Collection) object); return unmodifiableCollection(collection); } 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, useType); } return convert("", useType); } if (object instanceof NodeSet) { return convert(((NodeSet) object).getValues(), useType); } if (object instanceof Pointer) { return convert(((Pointer) object).getValue(), useType); } if (useType == String.class) { return object.toString(); } if (object instanceof Boolean) { if (Number.class.isAssignableFrom(useType)) { return allocateNumber(useType, ((Boolean) object).booleanValue() ? 1 : 0); } if ("java.util.concurrent.atomic.AtomicBoolean".equals(useType.getName())) { try { return useType.getConstructor(new Class[] { boolean.class }) .newInstance(new Object[] { object }); } catch (Exception e) { throw new JXPathTypeConversionException(useType.getName(), e); } } } if (object instanceof Number) { double value = ((Number) object).doubleValue(); if (useType == Boolean.class) { return value == 0.0 ? Boolean.FALSE : Boolean.TRUE; } if (Number.class.isAssignableFrom(useType)) { return allocateNumber(useType, value); } } if (object instanceof String) { Object value = convertStringToPrimitive(object, useType); if (value != null || "".equals(object)) { return value; } } Converter converter = ConvertUtils.lookup(useType); if (converter != null) { return converter.convert(useType, object); } throw new JXPathTypeConversionException("Cannot convert " + object.getClass() + " to " + useType); }
From source file:com.l2jfree.util.Introspection.java
private static void deepToString(Object obj, StringBuilder dest, Set<Object> dejaVu) throws SecurityException, NoSuchMethodException { if (obj == null) { dest.append("null"); return;/*from w w w. j a v a 2 s . c o m*/ } if (obj.getClass().isArray()) { final int length = Array.getLength(obj); if (length == 0) { dest.append("[]"); return; } if (dejaVu == null) dejaVu = new HashSet<Object>(); dejaVu.add(obj); dest.append('['); for (int i = 0; i < length; i++) { if (i != 0) dest.append(", "); final Object element = Array.get(obj, i); if (dejaVu.contains(element)) dest.append("[...]"); else deepToString(element, dest, dejaVu); } dest.append(']'); dejaVu.remove(obj); } else { if (obj.getClass().getMethod("toString").getDeclaringClass() == Object.class) dest.append(Introspection.toString(obj)); else dest.append(obj.toString()); } }