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:com.higgses.griffin.annotation.app.GinInjector.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, ViewFinder finder) { Class<?> handlerType = handler.getClass(); // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { GinInjectView viewInject = field.getAnnotation(GinInjectView.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.id(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); }// w ww.ja v a 2 s . co m } 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("id"); 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:net.kemuri9.sling.filesystemprovider.impl.PersistenceHelper.java
/** * Create a {@link JSONObject} that represents the specified object. * @param obj the object to create a JSONObject representation for. * @return the JSONObject representation of the object. * @throws JSONException if an error occurs on creating the JSON data *//*from w ww . j ava2 s.c o m*/ static JSONObject createJSONPropertyObject(Object obj) throws JSONException { String type = Object.class.getName().toString(); boolean isArray = false; if (obj != null) { if (obj.getClass().isArray()) { isArray = true; Class<?> elemType = obj.getClass().getComponentType(); if (elemType.isArray()) { throw new IllegalArgumentException("nested array types are not supported"); } type = elemType.getName(); } else { type = obj.getClass().getName().toString(); } } JSONObject jsonObj = new JSONObject(); jsonObj.put(FSPConstants.JSON_KEY_TYPE, type); boolean isBinary = false; if (isArray) { int arrSize = Array.getLength(obj); JSONArray arr = new JSONArray(); jsonObj.put(FSPConstants.JSON_KEY_VALUES, arr); for (int arr_i = 0; arr_i < arrSize; ++arr_i) { Object arr_val_i = Array.get(obj, arr_i); Object storage = convertToJSONStorage(arr_val_i); if (storage instanceof JSONStorage) { JSONStorage jsonStore = (JSONStorage) storage; isBinary |= jsonStore.isBinary; storage = jsonStore.value; } arr.put(storage); } } else { // singly valued type Object storage = convertToJSONStorage(obj); if (storage instanceof JSONStorage) { JSONStorage jsonStore = (JSONStorage) storage; storage = jsonStore.value; isBinary = jsonStore.isBinary; } jsonObj.put(FSPConstants.JSON_KEY_VALUE, storage); } if (isBinary) { jsonObj.put(FSPConstants.JSON_KEY_BINARY, true); } return jsonObj; }
From source file:net.solarnetwork.web.support.JSONView.java
private Collection<?> getPrimitiveCollection(Object array) { int len = Array.getLength(array); List<Object> result = new ArrayList<Object>(len); for (int i = 0; i < len; i++) { result.add(Array.get(array, i)); }// w w w . j av a2 s . c o m return result; }
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 .j av a 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:de.alpharogroup.lang.object.CloneObjectExtensions.java
/** * Try to clone the given object./*w ww. j a v a 2 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:org.apache.cayenne.log.CommonsJdbcEventLogger.java
void sqlLiteralForObject(StringBuilder buffer, Object object) { if (object == null) { buffer.append("NULL"); } else if (object instanceof String) { buffer.append('\''); // lets escape quotes String literal = (String) object; if (literal.length() > TRIM_VALUES_THRESHOLD) { literal = literal.substring(0, TRIM_VALUES_THRESHOLD) + "..."; }/*www .j a v a 2s .c om*/ int curPos = 0; int endPos = 0; while ((endPos = literal.indexOf('\'', curPos)) >= 0) { buffer.append(literal.substring(curPos, endPos + 1)).append('\''); curPos = endPos + 1; } if (curPos < literal.length()) buffer.append(literal.substring(curPos)); buffer.append('\''); } // handle byte pretty formatting else if (object instanceof Byte) { IDUtil.appendFormattedByte(buffer, ((Byte) object).byteValue()); } else if (object instanceof Number) { // process numeric value (do something smart in the future) buffer.append(object); } else if (object instanceof java.sql.Date) { buffer.append('\'').append(object).append('\''); } else if (object instanceof java.sql.Time) { buffer.append('\'').append(object).append('\''); } else if (object instanceof java.util.Date) { long time = ((java.util.Date) object).getTime(); buffer.append('\'').append(new java.sql.Timestamp(time)).append('\''); } else if (object instanceof java.util.Calendar) { long time = ((java.util.Calendar) object).getTimeInMillis(); buffer.append(object.getClass().getName()).append('(').append(new java.sql.Timestamp(time)).append(')'); } else if (object instanceof Character) { buffer.append(((Character) object).charValue()); } else if (object instanceof Boolean) { buffer.append('\'').append(object).append('\''); } else if (object instanceof Enum<?>) { // buffer.append(object.getClass().getName()).append("."); buffer.append(((Enum<?>) object).name()).append("="); if (object instanceof ExtendedEnumeration) { Object value = ((ExtendedEnumeration) object).getDatabaseValue(); if (value instanceof String) buffer.append("'"); buffer.append(value); if (value instanceof String) buffer.append("'"); } else { buffer.append(((Enum<?>) object).ordinal()); // FIXME -- this isn't quite right } } else if (object instanceof SQLParameterBinding) { sqlLiteralForObject(buffer, ((SQLParameterBinding) object).getValue()); } else if (object.getClass().isArray()) { buffer.append("< "); int len = Array.getLength(object); boolean trimming = false; if (len > TRIM_VALUES_THRESHOLD) { len = TRIM_VALUES_THRESHOLD; trimming = true; } for (int i = 0; i < len; i++) { if (i > 0) { buffer.append(","); } sqlLiteralForObject(buffer, Array.get(object, i)); } if (trimming) { buffer.append("..."); } buffer.append('>'); } else { buffer.append(object.getClass().getName()).append("@").append(System.identityHashCode(object)); } }
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 {// w w w . j av a 2s . com 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 .ja va2 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.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; }//ww w. jav a 2s .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: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 www . j a v a 2 s .c om } else { outputArray = (Object[]) position; } return outputArray; } else { return new Object[] { position }; } }