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:cloudnet.util.Dumper.java
/** * * @param o/*from w ww. ja va 2s. co m*/ * @param ctx * @return */ protected static String dump(Object o, DumpContext ctx) { if (o == null) { return "<null>"; } ctx.callCount++; StringBuilder tabs = new StringBuilder(); for (int k = 0; k < ctx.callCount; k++) { tabs.append("\t"); } StringBuilder buffer = new StringBuilder(); Class oClass = o.getClass(); String oSimpleName = getSimpleNameWithoutArrayQualifier(oClass); if (ctx.ignoreList.get(oSimpleName + ":") != null) { return "<Ignored>"; } if (oClass.isArray()) { buffer.append("\n"); buffer.append(tabs.toString().substring(1)); buffer.append("[\n"); int rowCount = ctx.maxArrayElements == 0 ? Array.getLength(o) : FastMath.min(ctx.maxArrayElements, Array.getLength(o)); for (int i = 0; i < rowCount; i++) { buffer.append(tabs.toString()); try { Object value = Array.get(o, i); buffer.append(dumpValue(value, ctx)); } catch (Exception e) { buffer.append(e.getMessage()); } if (i < Array.getLength(o) - 1) { buffer.append(","); } buffer.append("\n"); } if (rowCount < Array.getLength(o)) { buffer.append(tabs.toString()); buffer.append(Array.getLength(o) - rowCount + " more array elements..."); buffer.append("\n"); } buffer.append(tabs.toString().substring(1)); buffer.append("]"); } else { buffer.append("\n"); buffer.append(tabs.toString().substring(1)); buffer.append("{\n"); buffer.append(tabs.toString()); buffer.append("hashCode: " + o.hashCode()); buffer.append("\n"); while (oClass != null && oClass != Object.class) { Field[] fields = oClass.getDeclaredFields(); if (ctx.ignoreList.get(oClass.getSimpleName()) == null) { if (oClass != o.getClass()) { buffer.append(tabs.toString().substring(1)); buffer.append(" Inherited from superclass " + oSimpleName + ":\n"); } for (int i = 0; i < fields.length; i++) { String fSimpleName = getSimpleNameWithoutArrayQualifier(fields[i].getType()); String fName = fields[i].getName(); fields[i].setAccessible(true); buffer.append(tabs.toString()); buffer.append(fName + "(" + fSimpleName + ")"); buffer.append("="); if (ctx.ignoreList.get(":" + fName) == null && ctx.ignoreList.get(fSimpleName + ":" + fName) == null && ctx.ignoreList.get(fSimpleName + ":") == null) { try { Object value = fields[i].get(o); buffer.append(dumpValue(value, ctx)); } catch (Exception e) { buffer.append(e.getMessage()); } buffer.append("\n"); } else { buffer.append("<Ignored>"); buffer.append("\n"); } } oClass = oClass.getSuperclass(); oSimpleName = oClass.getSimpleName(); } else { oClass = null; oSimpleName = ""; } } buffer.append(tabs.toString().substring(1)); buffer.append("}"); } ctx.callCount--; return buffer.toString(); }
From source file:org.openlaszlo.remote.json.LZReturnObject.java
void pushArray(Object object) throws Exception { body.append("["); int length = Array.getLength(object); for (int i = 0; i < length; i++) { if (i > 0) { body.append(","); }//from ww w. j a v a 2s .co m createReturnValue(Array.get(object, i)); } body.append("]"); }
From source file:ArrayIterator.java
/** * Move to next element in the array.//from w w w .j a va 2 s.com * * @return The next object in the array. */ public Object next() { if (pos < size) return Array.get(array, pos++); /* * we screwed up... */ throw new NoSuchElementException("No more elements: " + pos + " / " + size); }
From source file:org.kordamp.ezmorph.array.ObjectArrayMorpher.java
public Object morph(Object array) { if (array == null) { return null; }/*from w w w . j a va2 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(this.target, dimensions); if (dims == 1) { for (int index = 0; index < length; index++) { try { Object value = Array.get(array, index); if (value != null && !morpher.supports(value.getClass())) { throw new MorphException(value.getClass() + " is not supported"); } Object morphed = morphMethod.invoke(morpher, value); Array.set(result, index, morphed); } catch (MorphException me) { throw me; } catch (Exception e) { throw new MorphException(e); } } } 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:org.openengsb.persistence.connector.jpabackend.ConnectorPropertiesWrapperJPAEntity.java
@SuppressWarnings("unchecked") public static ConnectorPropertiesWrapperJPAEntity getFromObject(Object property) { Class<?> clazz = property.getClass(); ConnectorPropertiesWrapperJPAEntity wrapper = new ConnectorPropertiesWrapperJPAEntity(); List<ConnectorPropertyJPAEntity> propList = new ArrayList<ConnectorPropertyJPAEntity>(); wrapper.setProperties(propList);/*ww w . j av a2 s .c om*/ wrapper.setCollectionType(clazz.getName()); if (clazz.isArray()) { Object[] arr; Class<?> compClass = clazz.getComponentType(); if (compClass.isPrimitive()) { compClass = ClassUtils.primitiveToWrapper(compClass); int length = Array.getLength(property); Object wrapperArray = Array.newInstance(compClass, length); for (int i = 0; i < length; i++) { Array.set(wrapperArray, i, Array.get(property, i)); } arr = (Object[]) wrapperArray; } else { arr = (Object[]) property; } loopProperties(Arrays.asList(arr), propList); return wrapper; } else { if (Collection.class.isAssignableFrom(clazz)) { Collection<Object> coll = (Collection<Object>) property; loopProperties(coll, propList); return wrapper; } else { wrapper.setCollectionType(null); propList.add(ConnectorPropertyJPAEntity.getFromObject(property)); return wrapper; } } }
From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java
public static float[] convertToFloatArray(final Object array) { if (array == null) { return null; }/* www . j ava 2 s . c o m*/ if (array instanceof float[]) { return (float[]) array; } if (array instanceof Float[]) { return ArrayUtils.toPrimitive((Float[]) array); } final float[] newArray = new float[Array.getLength(array)]; for (int i = 0; i < newArray.length; i++) { final Number val = (Number) Array.get(array, i); newArray[i] = val.floatValue(); } return newArray; }
From source file:nl.strohalm.cyclos.utils.binding.MapBean.java
public Object get(final String name, final int index) { final Object value = this.get(name); if (value != null) { if (value instanceof List<?>) { return ((List<?>) value).get(index); } else if (value.getClass().isArray()) { return Array.get(value, index); }//from www. ja v a 2 s . c o m } return null; }
From source file:de.tuberlin.uebb.jbop.access.ConstructorBuilderTest.java
/** * Tests that constructorBuilder() of the Testobject is working correctly. * /*from w w w .j a v a2 s . c om*/ * @throws Exception * the exception */ @Test public void testConstructorBuilder() throws Exception { // INIT final ClassNodeBuilder builder = ClassNodeBuilder .createClass("de.tuberlin.uebb.jbop.access.ConstructorBuilderTestClass").// addField("doubleValue", "D").initWith(2.0).withGetter().// addField("intValue", "I").initWith(1).withGetter().// addField("stringValue", Type.getDescriptor(String.class)).initWith("String").withGetter().// addField("doubleArrayValue", "[D").initArrayWith(1.0, 2.0, 3.0).withGetter(); final ClassNode classNode = builder.getClassNode(); final Object testClass = builder.toClass().instance(); // RUN final List<Object> parameterValues = ConstructorBuilder.createConstructor(classNode, testClass); // ASSERT assertEquals(4, parameterValues.size()); assertEquals(2.0, ((Double) parameterValues.get(0)).doubleValue(), .00001); assertEquals(1, ((Integer) parameterValues.get(1)).intValue()); assertEquals("String", parameterValues.get(2)); final Object array = parameterValues.get(3); assertEquals(1.0, ((Double) Array.get(array, 0)).doubleValue(), .00001); assertEquals(2.0, ((Double) Array.get(array, 1)).doubleValue(), .00001); assertEquals(3.0, ((Double) Array.get(array, 2)).doubleValue(), .00001); final Class<?> newClass = getClass(classNode, testClass); final Object testClassWithConstructor = ConstructorUtils.invokeConstructor(newClass, parameterValues.toArray(new Object[parameterValues.size()])); assertEquals(2.0, ((Double) invoke(newClass, "getDoubleValue", testClassWithConstructor)).doubleValue(), .00001); assertEquals(1, ((Integer) invoke(newClass, "getIntValue", testClassWithConstructor)).intValue()); assertEquals("String", invoke(newClass, "getStringValue", testClassWithConstructor)); final Object array2 = invoke(newClass, "getDoubleArrayValue", testClassWithConstructor); assertEquals(1.0, ((Double) Array.get(array2, 0)).doubleValue(), .00001); assertEquals(2.0, ((Double) Array.get(array2, 1)).doubleValue(), .00001); assertEquals(3.0, ((Double) Array.get(array2, 2)).doubleValue(), .00001); }
From source file:ObjectUtils.java
/** * Convert the given array (which may be a primitive array) to an * object array (if necessary of primitive wrapper objects). * <p>A <code>null</code> source value will be converted to an * empty Object array./* w w w. ja v a2 s . c om*/ * @param source the (potentially primitive) array * @return the corresponding object array (never <code>null</code>) * @throws IllegalArgumentException if the parameter is not an array */ public static Object[] toObjectArray(Object source) { if (source instanceof Object[]) { return (Object[]) source; } if (source == null) { return new Object[0]; } if (!source.getClass().isArray()) { throw new IllegalArgumentException("Source is not an array: " + source); } int length = Array.getLength(source); if (length == 0) { return new Object[0]; } Class wrapperType = Array.get(source, 0).getClass(); Object[] newArray = (Object[]) Array.newInstance(wrapperType, length); for (int i = 0; i < length; i++) { newArray[i] = Array.get(source, i); } return newArray; }
From source file:com.astamuse.asta4d.web.form.validation.TypeUnMatchValidator.java
@SuppressWarnings("rawtypes") private void addMessage(List<FormValidationMessage> msgList, Object form, int[] indexes) { List<AnnotatedPropertyInfo> fieldList = AnnotatedPropertyUtil.retrieveProperties(form.getClass()); try {//from w ww . ja v a2 s.com for (AnnotatedPropertyInfo field : fieldList) { CascadeFormField cff = field.getAnnotation(CascadeFormField.class); if (cff != null) { Object subform = field.retrieveValue(form); if (StringUtils.isEmpty(cff.arrayLengthField())) { // simple cascade form addMessage(msgList, subform, indexes); } else { // array cascade form int len = Array.getLength(subform); for (int i = 0; i < len; i++) { addMessage(msgList, Array.get(subform, i), ArrayUtils.add(indexes, i)); } } continue; } ContextDataHolder valueHolder; if (field.getField() != null) { valueHolder = InjectTrace.getInstanceInjectionTraceInfo(form, field.getField()); } else { valueHolder = InjectTrace.getInstanceInjectionTraceInfo(form, field.getSetter()); } if (valueHolder != null) { msgList.add(createTypeUnMatchMessage(form.getClass(), field, valueHolder, indexes)); } } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } }