/** * Use the given ClassLoader rather than using the system class */ @Override protected Class<?> resolveClass(ObjectStreamClass objectstreamclass) throws IOException, ClassNotFoundException { String s = objectstreamclass.getName(); if (s.startsWith("[")) { int i; for (i = 1; s.charAt(i) == '['; i++); Class<?> class1; if (s.charAt(i) == 'L') { class1 = loader.loadClass(s.substring(i + 1, s.length() - 1)); } else { if (s.length() != i + 1) throw new ClassNotFoundException(s); class1 = primitiveType(s.charAt(i)); } int ai[] = new int[i]; for (int j = 0; j < i; j++) ai[j] = 0; return Array.newInstance(class1, ai).getClass(); } else { return loader.loadClass(s); } }
/** * Returns a typed array containing a snapshot of all values of the Subject. * <p>The method follows the conventions of Collection.toArray by setting the array element * after the last value to null (if the capacity permits). * <p>The method is thread-safe. * @param array the target array to copy values into if it fits * @return the given array if the values fit into it or a new array containing all values */ @SuppressWarnings("unchecked") public T[] getValues(T[] array) { Object o = value.get(); if (o == null || NotificationLite.isComplete(o) || NotificationLite.isError(o)) { if (array.length != 0) { array[0] = null; } return array; } T v = NotificationLite.getValue(o); if (array.length != 0) { array[0] = v; if (array.length != 1) { array[1] = null; } } else { array = (T[])Array.newInstance(array.getClass().getComponentType(), 1); array[0] = v; } return array; }
@Override public Object prepare(Object value) { if (!ObjectUtils.isArray(value)) { return value; } int length = Array.getLength(value); Collection<Object> result = new ArrayList<Object>(length); for (int i = 0; i < length; i++) { result.add(Array.get(value, i)); } return result; }
private void update() { removeAll(); valid = model.isMatrix(); if(valid) { Object[] array = (Object[]) model.getValues(); for(Object line : array) { int len = Array.getLength(line); for(int i = 0; i < len; i++) { Object e = Array.get(line, i); Label label = new Label(e.toString()); // TODO array deepToString label.setForegroundColor(ColorConstants.black); add(label); } } } getLayoutManager().layout(this); repaint(); }
public Object read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } List<E> list = new ArrayList(); in.beginArray(); while (in.hasNext()) { list.add(this.componentTypeAdapter.read(in)); } in.endArray(); Object array = Array.newInstance(this.componentType, list.size()); for (int i = 0; i < list.size(); i++) { Array.set(array, i, list.get(i)); } return array; }
/** * Determine whether the given object is empty. * <p>This method supports the following object types. * <ul> * <li>{@code Optional}: considered empty if {@link Optional#empty()}</li> * <li>{@code Array}: considered empty if its length is zero</li> * <li>{@link CharSequence}: considered empty if its length is zero</li> * <li>{@link Collection}: delegates to {@link Collection#isEmpty()}</li> * <li>{@link Map}: delegates to {@link Map#isEmpty()}</li> * </ul> * <p>If the given object is non-null and not one of the aforementioned * supported types, this method returns {@code false}. * @param obj the object to check * @return {@code true} if the object is {@code null} or <em>empty</em> * @since 4.2 * @see Optional#isPresent() * @see ObjectUtils#isEmpty(Object[]) * @see StringUtils#hasLength(CharSequence) * @see StringUtils#isEmpty(Object) * @see CollectionUtils#isEmpty(java.util.Collection) * @see CollectionUtils#isEmpty(java.util.Map) */ @SuppressWarnings("rawtypes") public static boolean isEmpty( Object obj) { if (obj == null) { return true; } if (obj instanceof Optional) { return !((Optional) obj).isPresent(); } if (obj.getClass().isArray()) { return Array.getLength(obj) == 0; } if (obj instanceof CharSequence) { return ((CharSequence) obj).length() == 0; } if (obj instanceof Collection) { return ((Collection) obj).isEmpty(); } if (obj instanceof Map) { return ((Map) obj).isEmpty(); } // else return false; }
/** * Convert an array of specified values to an array of objects of the * specified class (if possible). If the specified Java class is itself * an array class, this class will be the type of the returned value. * Otherwise, an array will be constructed whose component type is the * specified class. * * @param values Array of values to be converted * @param clazz Java array or element class to be converted to (must not be null) * @return The converted value * * @throws ConversionException if thrown by an underlying Converter */ public Object convert(final String[] values, final Class<?> clazz) { Class<?> type = clazz; if (clazz.isArray()) { type = clazz.getComponentType(); } if (log.isDebugEnabled()) { log.debug("Convert String[" + values.length + "] to class '" + type.getName() + "[]'"); } Converter converter = lookup(type); if (converter == null) { converter = lookup(String.class); } if (log.isTraceEnabled()) { log.trace(" Using converter " + converter); } final Object array = Array.newInstance(type, values.length); for (int i = 0; i < values.length; i++) { Array.set(array, i, converter.convert(type, values[i])); } return (array); }
/** * <code>aObject</code> is a possibly-null object field, and possibly an * array. * * If <code>aObject</code> is an array, then each element may be a * primitive or a possibly-null object. */ public static int hash(int aSeed, Object aObject) { int result = aSeed; if (aObject == null) { result = hash(result, 0); } else if (!isArray(aObject)) { result = hash(result, aObject.hashCode()); } else { int length = Array.getLength(aObject); for (int idx = 0; idx < length; ++idx) { Object item = Array.get(aObject, idx); //recursive call! result = hash(result, item); } } return result; }
final Object toNonNullOpenValue(Object value) throws OpenDataException { final Collection valueCollection = (Collection) value; if (valueCollection instanceof SortedSet) { Comparator comparator = ((SortedSet) valueCollection).comparator(); if (comparator != null) { final String msg = "Cannot convert SortedSet with non-null comparator: " + comparator; throw openDataException(msg, new IllegalArgumentException(msg)); } } final Object[] openArray = (Object[]) Array.newInstance(getOpenClass().getComponentType(), valueCollection.size()); int i = 0; for (Object o : valueCollection) openArray[i++] = elementConverter.toOpenValue(o); return openArray; }
private static void checkValue(Object o1, Object o2) throws Exception { assertEquals(o1.getClass().isAnnotation(), o2.getClass().isAnnotation()); if (o1.getClass().isAnnotation()) { assertEquals(((Annotation) o1).annotationType(), ((Annotation) o2).annotationType()); } else { assertEquals(o1.getClass(), o2.getClass()); } if (o1.getClass().isArray()) { assertEquals(Array.getLength(o1), Array.getLength(o2)); for (int c = 0; c < Array.getLength(o1); c++) { checkValue(Array.get(o1, c), Array.get(o2, c)); } } else if (o1.getClass().isAnnotation()) { checkAnnotation((Annotation) o1, (Annotation) o2); } else { assertEquals(o1, o2); } }
/** * 检查指定的集合/数组/枚举为空 * <p> * 此方法可以处理如下对象: * <ul> * <li>Collection * <li>Map * <li>Array * <li>Enumeration * </ul> * <p> * * @param object * @return true * @throws IllegalArgumentException * @since 1.0 */ public static boolean isEmpty(final Object object) { if (object == null) { return true; } else if (object instanceof Collection<?>) { return ((Collection<?>) object).isEmpty(); } else if (object instanceof Map<?, ?>) { return ((Map<?, ?>) object).isEmpty(); } else if (object instanceof Object[]) { return ((Object[]) object).length == 0; } else if (object instanceof Iterator<?>) { return ((Iterator<?>) object).hasNext() == false; } else if (object instanceof Enumeration<?>) { return ((Enumeration<?>) object).hasMoreElements() == false; } else { try { return Array.getLength(object) == 0; } catch (final IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } }
/** * 配列の値をIN句として加工してバインドする * * @param transformContext transformコンテキスト * @param values バインドする値の配列 */ private void bindArray(final TransformContext transformContext, final Object values) { int length = Array.getLength(values); if (length == 0) { throw new ParameterNotFoundRuntimeException("Parameter is not set. [" + expression + "]"); } transformContext.addSqlPart("(?"); transformContext.addBindVariable(Array.get(values, 0)); for (int i = 1; i < length; i++) { transformContext.addSqlPart(", ?"); transformContext.addBindVariable(Array.get(values, i)); } transformContext.addSqlPart(")/*").addSqlPart(expression).addSqlPart("*/"); transformContext.addBindName(expression); }
@SuppressWarnings("unused") private static final boolean rangeCheck(final Object array, final Object index) { if(!(index instanceof Number)) { return false; } final Number n = (Number)index; final int intIndex = n.intValue(); final double doubleValue = n.doubleValue(); if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE return false; } if(0 <= intIndex && intIndex < Array.getLength(array)) { return true; } throw new ArrayIndexOutOfBoundsException("Array index out of range: " + n); }
@SuppressWarnings("unchecked") private static <T> T[] copyOfRange(T[] original, int start, int end) { final int originalLength = original.length; // For exception priority compatibility. if (start > end) { throw new IllegalArgumentException(); } if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } final int resultLength = end - start; final int copyLength = Math.min(resultLength, originalLength - start); final T[] result = (T[]) Array .newInstance(original.getClass().getComponentType(), resultLength); System.arraycopy(original, start, result, 0, copyLength); return result; }
private void addToData(int index, T item) { if (index > mSize) { throw new IndexOutOfBoundsException( "cannot add item to " + index + " because size is " + mSize); } if (mSize == mData.length) { // we are at the limit enlarge T[] newData = (T[]) Array.newInstance(mTClass, mData.length + CAPACITY_GROWTH); System.arraycopy(mData, 0, newData, 0, index); newData[index] = item; System.arraycopy(mData, index, newData, index + 1, mSize - index); mData = newData; } else { // just shift, we fit System.arraycopy(mData, index, mData, index + 1, mSize - index); mData[index] = item; } mSize++; }
/** * Return an array of the values in this CatalogMap. * This will be generally faster than using an iterator because * we will cache the array locally * @return */ @SuppressWarnings("unchecked") public T[] values() { if (m_fastArray == null) { synchronized (this) { if (m_fastArray == null) { int capacity = this.size(); T arr[] = (T[])Array.newInstance(this.m_cls, capacity); int i = 0; for (T t : m_items.values()) { arr[i++] = t; } // FOR m_fastArray = arr; } } // SYNCH } return m_fastArray; }
public Serializable disassemble(CollectionPersister persister) throws HibernateException { int length = Array.getLength(array); Serializable[] result = new Serializable[length]; for ( int i=0; i<length; i++ ) { result[i] = persister.getElementType().disassemble( Array.get(array,i), getSession() ); } /*int length = tempList.size(); Serializable[] result = new Serializable[length]; for ( int i=0; i<length; i++ ) { result[i] = persister.getElementType().disassemble( tempList.get(i), session ); }*/ return result; }
@Test public void testArrayMerger() throws Exception { String[] stringArray1 = {"1", "2", "3"}; String[] stringArray2 = {"4", "5", "6"}; String[] stringArray3 = {}; Object result = ArrayMerger.INSTANCE.merge(stringArray1, stringArray2, stringArray3); Assert.assertTrue(result.getClass().isArray()); Assert.assertEquals(6, Array.getLength(result)); Assert.assertTrue(String.class.isInstance(Array.get(result, 0))); for(int i = 0; i < 6; i++) { Assert.assertEquals(String.valueOf(i + 1), Array.get(result, i)); } int[] intArray1 = {1, 2, 3}; int[] intArray2 = {4, 5, 6}; int[] intArray3 = {7}; result = MergerFactory.getMerger(int[].class).merge(intArray1, intArray2, intArray3); Assert.assertTrue(result.getClass().isArray()); Assert.assertEquals(7, Array.getLength(result)); Assert.assertTrue(int.class == result.getClass().getComponentType()); for (int i = 0; i < 7; i++) { Assert.assertEquals(i + 1, Array.get(result, i)); } }
/** * Determines whether two arrays are equal by comparing each of their * elements using <tt>Object.equals()</tt>. */ private static boolean arrayEquals(Object a1, Object a2) { int len; if ((len = Array.getLength(a1)) != Array.getLength(a2)) return false; for (int j = 0; j < len; j++) { Object i1 = Array.get(a1, j); Object i2 = Array.get(a2, j); if (i1 == null || i2 == null) { if (i1 != i2) return false; } else if (!i1.equals(i2)) { return false; } } return true; }
/** * 对基本类型进行类型转换 */ @Override @SuppressWarnings("unused") protected Object doConvertValue(Object source, Class<?> toType, Object... params) { /** * 如果是基础类型,则直接返回 */ if (source != null && (!PrimitiveTypeUtil.isPriType(source.getClass()) || !PrimitiveTypeUtil.isPriType(toType))) { return null; } /** * 如果都是数组类型,则构造数组 */ if (source != null && source.getClass().isArray() && toType.isArray()) { Object result; Class<?> componentType = toType.getComponentType(); result = Array.newInstance(componentType, Array.getLength(source)); for (int i = 0; i < Array.getLength(source); i++) { Array.set(result, i, convert(Array.get(source, i),componentType, params)); } return result; } return doConvert(source, toType); }
/** * Returns the given array if newsize is the same as existing. * Returns a new array of given size, containing as many elements of * the original array as it can hold. */ public static Object resizeArrayIfDifferent(Object source, int newsize) { int oldsize = Array.getLength(source); if (oldsize == newsize) { return source; } Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize); if (oldsize < newsize) { newsize = oldsize; } System.arraycopy(source, 0, newarray, 0, newsize); return newarray; }
/** * Construct an ArrayIterator that will iterate over a range of values * in the specified array. * * @param array the array to iterate over. * @param startIndex the index to start iterating at. * @param endIndex the index to finish iterating at. * @throws IllegalArgumentException if <code>array</code> is not an array. * @throws NullPointerException if <code>array</code> is <code>null</code> * @throws IndexOutOfBoundsException if either index is invalid */ public ArrayIterator(final Object array, final int startIndex, final int endIndex) { super(); this.array = array; this.startIndex = startIndex; this.endIndex = endIndex; this.index = startIndex; final int len = Array.getLength(array); checkBound(startIndex, len, "start"); checkBound(endIndex, len, "end"); if (endIndex < startIndex) { throw new IllegalArgumentException("End index must not be less than start index."); } }
@SuppressWarnings("unchecked") protected static <T> T resolveAnnotationValue(Class<T> expectedType, AnnotationValue value) { if (value == null) { return null; } if (expectedType.isArray()) { ArrayList<Object> result = new ArrayList<>(); List<AnnotationValue> l = (List<AnnotationValue>) value.getValue(); for (AnnotationValue el : l) { result.add(resolveAnnotationValue(expectedType.getComponentType(), el)); } return (T) result.toArray((Object[]) Array.newInstance(expectedType.getComponentType(), result.size())); } Object unboxedValue = value.getValue(); if (unboxedValue != null) { if (expectedType == TypeMirror.class && unboxedValue instanceof String) { /* * Happens if type is invalid when using the ECJ compiler. The ECJ does not match * AP-API specification here. */ return null; } if (!expectedType.isAssignableFrom(unboxedValue.getClass())) { throw new ClassCastException(unboxedValue.getClass().getName() + " not assignable from " + expectedType.getName()); } } return (T) unboxedValue; }
private static boolean isEmptyObject(Object object){ if(object == null){ return true; }else if(object instanceof String){ return ((String)object).trim().equals("") || ((String)object).trim().equals("null"); }else if(object instanceof Collection<?>){ return ((Collection<?>)object).isEmpty(); }else if(object instanceof Map<?,?>){ return ((Map<?,?>)object).isEmpty(); }else if(object.getClass().isArray()){ return Array.getLength(object) == 0; }else{ return false; } }
/** * 判断对象是否为空 * * @param obj 对象 * @return {@code true}: 为空<br>{@code false}: 不为空 */ public static boolean isEmpty(Object obj) { if (obj == null) { return true; } if (obj instanceof String && obj.toString().length() == 0) { return true; } if (obj.getClass().isArray() && Array.getLength(obj) == 0) { return true; } if (obj instanceof Collection && ((Collection) obj).isEmpty()) { return true; } if (obj instanceof Map && ((Map) obj).isEmpty()) { return true; } if (obj instanceof SparseArray && ((SparseArray) obj).size() == 0) { return true; } if (obj instanceof SparseBooleanArray && ((SparseBooleanArray) obj).size() == 0) { return true; } if (obj instanceof SparseIntArray && ((SparseIntArray) obj).size() == 0) { return true; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { if (obj instanceof SparseLongArray && ((SparseLongArray) obj).size() == 0) { return true; } } return false; }
/** * Converts an Attribute List to an array of passed type. * * @param annotatedValues The list to convert * @param type The arrays target type * @param <T> generic Type * @return an array of passed type containing all casted elements of passed annotatedValues list or null if passed annotatedValues are null. * @throws ClassCastException will be thrown if Attributes of List cannot be cast to passed type */ public static <T> T[] convertAndCastAttributeValueListToArray(List<Attribute> annotatedValues, Class<T> type) { if (annotatedValues == null) { return null; } T[] result = (T[]) Array.newInstance(type, annotatedValues.size()); for (int i = 0; i < annotatedValues.size(); i++) { result[i] = (T) annotatedValues.get(i).getValue(); } return result; }
@Override public Object parse(String input, ParserHelper helper) { if (!helper.getRawTargetClass().isArray()) { return TRY_NEXT; } List<String> strList = helper.split(input); Class<?> componentType = helper.getComponentClass(); Object result = Array.newInstance(componentType, strList.size()); for (int i = 0; i < strList.size(); i++) { Object element = helper.parse(strList.get(i), componentType); Array.set(result, i, element); } return result; }
public void serializeBody(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException { int len = Array.getLength(array); for( int i=0; i<len; i++ ) { Object item = Array.get(array,i); // TODO: check the namespace URI. target.startElement("","item",null,null); if(item==null) { target.writeXsiNilTrue(); } else { target.childAsXsiType(item,"arrayItem",itemBeanInfo, false); } target.endElement(); } }
private static Class<?> toClass(Type o) { if (o instanceof GenericArrayType) return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()), 0) .getClass(); return (Class<?>)o; }
public static <T> T[] toArray(List<T> list){ int length = list.size(); if(length == 0){ return (T[])list.toArray(); } Class<?> componentType = list.get(0).getClass(); T[] array = (T[]) Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { array[i] = list.get(i); } return array; }
public static <T> T[] concatenate(final T[] a, final T[] b) { int aLen = a.length; int bLen = b.length; @SuppressWarnings("unchecked") T[] rtn = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen); System.arraycopy(a, 0, rtn, 0, aLen); System.arraycopy(b, 0, rtn, aLen, bLen); return rtn; }
/** * Construct a JSONArray from an array * * @throws JSONException * If not an array. */ public JSONArray(Object array) throws JSONException { this(); if (array.getClass().isArray()) { int length = Array.getLength(array); for (int i = 0; i < length; i += 1) { this.put(JSONObject.wrap(Array.get(array, i))); } } else { throw new JSONException("JSONArray initial value should be a string or collection or array."); } }
public ArrayDeserializer(Class componentType) { _componentType = componentType; if (_componentType != null) { try { _type = Array.newInstance(_componentType, 0).getClass(); } catch (Exception e) { } } if (_type == null) _type = Object[].class; }
/** * Returns discovered objects all at once. * * @return * can be empty but never null. * * @throws ServiceConfigurationError */ @SuppressWarnings({"unchecked"}) public T[] toArray() { List<T> result = new ArrayList<T>(); for (T t : this) { result.add(t); } return result.toArray((T[])Array.newInstance(serviceClass,result.size())); }
@Override public Transaction<T> remove(@NonNull Collection<T> items) { mActions.add(list -> { @SuppressWarnings("unchecked") final T[] array = items.toArray((T[]) Array.newInstance(mItemClass, items.size())); Arrays.sort(array, mComparator); for (T item : array) { mSortedList.remove(item); } }); return this; }
/** * Remove a value from an array */ private V removeFromArray( V value ) { checkAndDeserialize(); // First check that the value is not already present in the ValueHolder int pos = findPos( value ); if ( pos < 0 ) { // The value does not exists : nothing to do return null; } // Ok, we just have to delete the new element at the right position // First, copy the array V[] newValueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), valueArray.length - 1 ); System.arraycopy( valueArray, 0, newValueArray, 0, pos ); System.arraycopy( valueArray, pos + 1, newValueArray, pos, valueArray.length - pos - 1 ); // Get the removed element V removedValue = valueArray[pos]; // And switch the arrays valueArray = newValueArray; return removedValue; }
public static int[] addAll(int[] array, int... elems) { int srcLength = getLength(array); int elemsLength = Array.getLength(elems); if (srcLength < elemsLength) { throw new ArrayIndexOutOfBoundsException("src length is " + srcLength + ", but elemes length is " + elemsLength); } int[] expanedArray = (int[]) expandSpace(array, elemsLength, Integer.TYPE); System.arraycopy(elems, 0, expanedArray, srcLength, elemsLength); return expanedArray; }
/** * Checks whether an object is empty, in a null safe manner Checks cases of * String, Collection, Map, Array objects * * @param value The object to check * @return <tt>true</tt> if the object is empty or <tt>null</tt> */ public static boolean isEmpty(Object value) { if (value == null) { return true; } else if (value instanceof String) { return (((String) value).trim().length() == 0); } else if (value instanceof Collection) { return ((Collection<?>) value).isEmpty(); } else if (value instanceof Map) { return ((Map<?, ?>) value).isEmpty(); } else { return value.getClass().isArray() && (Array.getLength(value) == 0); } }