List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:org.apache.axis.utils.BeanPropertyDescriptor.java
/** * Set an indexed property value/* w w w. j av a2 s. co m*/ * @param obj is the object * @param i the index * @param newValue is the new value */ public void set(Object obj, int i, Object newValue) throws InvocationTargetException, IllegalAccessException { // Set the new value if (isIndexed()) { IndexedPropertyDescriptor id = (IndexedPropertyDescriptor) myPD; growArrayToSize(obj, id.getIndexedPropertyType(), i); id.getIndexedWriteMethod().invoke(obj, new Object[] { new Integer(i), newValue }); } else { // Not calling 'growArrayToSize' to avoid an extra call to the // property's setter. The setter will be called at the end anyway. // growArrayToSize(obj, myPD.getPropertyType().getComponentType(), i); Object array = get(obj); if (array == null || Array.getLength(array) <= i) { Class componentType = getType().getComponentType(); Object newArray = Array.newInstance(componentType, i + 1); // Copy over the old elements if (array != null) { System.arraycopy(array, 0, newArray, 0, Array.getLength(array)); } array = newArray; } Array.set(array, i, newValue); // Fix for non-indempondent array-type propertirs. // Make sure we call the property's setter. set(obj, array); } }
From source file:de.alpharogroup.lang.object.CloneObjectExtensions.java
/** * Try to clone the given object.//from ww w. jav a2 s. c o m * * @param object * The object to clone. * @return The cloned object or null if the clone process failed. * @throws NoSuchMethodException * Thrown if a matching method is not found or if the name is "<init>"or * "<clinit>". * @throws SecurityException * Thrown if the security manager indicates a security violation. * @throws IllegalAccessException * Thrown if this {@code Method} object is enforcing Java language access control * and the underlying method is inaccessible. * @throws IllegalArgumentException * Thrown if an illegal argument is given * @throws InvocationTargetException * Thrown if the property accessor method throws an exception * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader * @throws InstantiationException * Thrown if one of the following reasons: the class object * <ul> * <li>represents an abstract class</li> * <li>represents an interface</li> * <li>represents an array class</li> * <li>represents a primitive type</li> * <li>represents {@code void}</li> * <li>has no nullary constructor</li> * </ul> * @throws IOException * Signals that an I/O exception has occurred. */ public static Object cloneObject(final Object object) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException, IOException { Object clone = null; // Try to clone the object if it implements Serializable. if (object instanceof Serializable) { clone = SerializedObjectExtensions.copySerializedObject((Serializable) object); if (clone != null) { return clone; } } // Try to clone the object if it is Cloneble. if (clone == null && object instanceof Cloneable) { if (object.getClass().isArray()) { final Class<?> componentType = object.getClass().getComponentType(); if (componentType.isPrimitive()) { int length = Array.getLength(object); clone = Array.newInstance(componentType, length); while (length-- > 0) { Array.set(clone, length, Array.get(object, length)); } } else { clone = ((Object[]) object).clone(); } if (clone != null) { return clone; } } final Class<?> clazz = object.getClass(); final Method cloneMethod = clazz.getMethod("clone", (Class[]) null); clone = cloneMethod.invoke(object, (Object[]) null); if (clone != null) { return clone; } } // Try to clone the object by copying all his properties with // the BeanUtils.copyProperties() method. if (clone == null) { clone = ReflectionExtensions.getNewInstance(object); BeanUtils.copyProperties(clone, object); } return clone; }
From source file:es.caib.zkib.jxpath.util.ValueUtils.java
/** * Grows the collection if necessary to the specified size. Returns * the new, expanded collection./*ww w .j a v a 2 s . co m*/ * @param collection to expand * @param size desired size * @return collection or array */ public static Object expandCollection(Object collection, int size) { if (collection == null) { return null; } if (size < getLength(collection)) { throw new JXPathException("adjustment of " + collection + " to size " + size + " is not an expansion"); } if (collection.getClass().isArray()) { Object bigger = Array.newInstance(collection.getClass().getComponentType(), size); System.arraycopy(collection, 0, bigger, 0, Array.getLength(collection)); return bigger; } if (collection instanceof Collection) { while (((Collection) collection).size() < size) { ((Collection) collection).add(null); } return collection; } throw new JXPathException( "Cannot turn " + collection.getClass().getName() + " into a collection of size " + size); }
From source file:de.ks.flatadocdb.defaults.ReflectionLuceneDocumentExtractor.java
private ReflectionLuceneDocumentExtractor.DocField createArrayDocField(Field f, MethodHandle getter) { return new DocField(f, getter, (id, value) -> { StringBuilder builder = new StringBuilder(); int length = Array.getLength(value); for (int i = 0; i < length; i++) { Object element = Array.get(value, i); builder.append(element);/*ww w. java 2s .c om*/ if (i != length - 1) { builder.append(", "); } } return new StringField(id, builder.toString(), org.apache.lucene.document.Field.Store.YES); }); }
From source file:com.lidroid.xutils.ViewUtils.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, ViewFinder finder) { Class<?> handlerType = handler.getClass(); // inject ContentView ContentView contentView = handlerType.getAnnotation(ContentView.class); if (contentView != null) { try {/*from ww w. j a v a2 s .c o m*/ Method setContentViewMethod = handlerType.getMethod("setContentView", int.class); setContentViewMethod.invoke(handler, contentView.value()); } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.value(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } else { ResInject resInject = field.getAnnotation(ResInject.class); if (resInject != null) { try { Object res = ResLoader.loadRes(resInject.type(), finder.getContext(), resInject.id()); if (res != null) { field.setAccessible(true); field.set(handler, res); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } else { PreferenceInject preferenceInject = field.getAnnotation(PreferenceInject.class); if (preferenceInject != null) { try { Preference preference = finder.findPreference(preferenceInject.value()); if (preference != null) { field.setAccessible(true); field.set(handler, preference); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } } } } } // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { Class<?> annType = annotation.annotationType(); if (annType.getAnnotation(EventBase.class) != null) { method.setAccessible(true); try { // ProGuard-keep class * extends java.lang.annotation.Annotation { *; } Method valueMethod = annType.getDeclaredMethod("value"); Method parentIdMethod = null; try { parentIdMethod = annType.getDeclaredMethod("parentId"); } catch (Throwable e) { } Object values = valueMethod.invoke(annotation); Object parentIds = parentIdMethod == null ? null : parentIdMethod.invoke(annotation); int parentIdsLen = parentIds == null ? 0 : Array.getLength(parentIds); int len = Array.getLength(values); for (int i = 0; i < len; i++) { ViewInjectInfo info = new ViewInjectInfo(); info.value = Array.get(values, i); info.parentId = parentIdsLen > i ? (Integer) Array.get(parentIds, i) : 0; EventListenerManager.addEventMethod(finder, info, annotation, handler, method); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } } } } } }
From source file:com.zhoukl.androidRDP.RdpUtils.RdpAnnotationUtil.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, ViewFinder finder) { Class<?> handlerType = handler.getClass(); // inject ContentView ContentView contentView = handlerType.getAnnotation(ContentView.class); if (contentView != null) { try {//from ww w . j a v a 2s . co m Method setContentViewMethod = handlerType.getMethod("setContentView", int.class); setContentViewMethod.invoke(handler, contentView.value()); } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.value(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } else { ResInject resInject = field.getAnnotation(ResInject.class); if (resInject != null) { try { Object res = ResLoader.loadRes(resInject.type(), finder.getContext(), resInject.id()); if (res != null) { field.setAccessible(true); field.set(handler, res); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } else { PreferenceInject preferenceInject = field.getAnnotation(PreferenceInject.class); if (preferenceInject != null) { try { Preference preference = finder.findPreference(preferenceInject.value()); if (preference != null) { field.setAccessible(true); field.set(handler, preference); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } } } } } // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { Class<?> annType = annotation.annotationType(); if (annType.getAnnotation(EventBase.class) != null) { method.setAccessible(true); try { // ProGuard-keep class * extends java.lang.annotation.Annotation { *; } Method valueMethod = annType.getDeclaredMethod("value"); Method parentIdMethod = null; try { parentIdMethod = annType.getDeclaredMethod("parentId"); } catch (Throwable e) { } Object values = valueMethod.invoke(annotation); Object parentIds = parentIdMethod == null ? null : parentIdMethod.invoke(annotation); int parentIdsLen = parentIds == null ? 0 : Array.getLength(parentIds); int len = Array.getLength(values); for (int i = 0; i < len; i++) { ViewInjectInfo info = new ViewInjectInfo(); info.value = Array.get(values, i); info.parentId = parentIdsLen > i ? (Integer) Array.get(parentIds, i) : 0; EventListenerManager.addEventMethod(finder, info, annotation, handler, method); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } } } } } }
From source file:org.apache.struts.validator.BeanValidatorForm.java
/** * <p>Return the size of an indexed or mapped property.</p> *//* w w w. ja va 2 s . c o m*/ public int size(String name) { Object value = dynaBean.get(name); if (value == null) { return 0; } if (value instanceof Map) { return ((Map) value).size(); } if (value instanceof List) { return ((List) value).size(); } if ((value.getClass().isArray())) { return Array.getLength(value); } return 0; }
From source file:gda.data.scan.datawriter.scannablewriter.SingleScannableWriter.java
private final Object[] getArrayObject(final Object position) { if (position.getClass().isArray()) { final Object[] outputArray; if (position.getClass().getComponentType().isPrimitive()) { final int arrlength = Array.getLength(position); outputArray = new Object[arrlength]; for (int i = 0; i < arrlength; ++i) { outputArray[i] = Array.get(position, i); }//from w w w. j a v a 2 s. c o m } else { outputArray = (Object[]) position; } return outputArray; } else { return new Object[] { position }; } }
From source file:com.google.api.server.spi.ObjectMapperUtil.java
private static boolean isEmpty(Object value) { Class<?> clazz = value.getClass(); if (clazz.isArray()) { int len = Array.getLength(value); for (int i = 0; i < len; i++) { Object element = Array.get(value, i); if (element != null && !isEmpty(element)) { return false; }/*from w w w . j ava 2 s .c om*/ } return true; } else if (Collection.class.isAssignableFrom(clazz)) { Collection<?> c = (Collection<?>) value; for (Object element : c) { if (element != null && !isEmpty(element)) { return false; } } return true; } else if (Map.class.isAssignableFrom(clazz)) { Map<?, ?> m = (Map<?, ?>) value; for (Object entryValue : m.values()) { if (entryValue != null && !isEmpty(entryValue)) { return false; } } return true; } return false; }
From source file:com.alibaba.dubbo.governance.web.common.pulltool.Tool.java
public static int countMapValues(Map<?, ?> map) { int total = 0; if (map != null && map.size() > 0) { for (Object value : map.values()) { if (value != null) { if (value instanceof Number) { total += ((Number) value).intValue(); } else if (value.getClass().isArray()) { total += Array.getLength(value); } else if (value instanceof Collection) { total += ((Collection<?>) value).size(); } else if (value instanceof Map) { total += ((Map<?, ?>) value).size(); } else { total += 1;// w ww . j av a2 s. c o m } } } } return total; }