java.lang.reflect.Array.getLength()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(186)

本文整理了Java中java.lang.reflect.Array.getLength()方法的一些代码示例,展示了Array.getLength()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Array.getLength()方法的具体详情如下:
包路径:java.lang.reflect.Array
类名称:Array
方法名:getLength

Array.getLength介绍

[英]Returns the length of the array. Equivalent to array.length.
[中]返回数组的长度。等价于数组。长

代码示例

代码示例来源:origin: libgdx/libgdx

/** Returns the length of the supplied array. */
static public int getLength (Object array) {
  return java.lang.reflect.Array.getLength(array);
}

代码示例来源:origin: libgdx/libgdx

/** Returns the length of the supplied array. */
static public int getLength (Object array) {
  return java.lang.reflect.Array.getLength(array);
}

代码示例来源:origin: org.mockito/mockito-core

public boolean hasNext() {
  return currentIndex < Array.getLength(value);
}

代码示例来源:origin: org.mockito/mockito-core

static boolean areArrayLengthsEqual(Object o1, Object o2) {
  return Array.getLength(o1) == Array.getLength(o2);
}

代码示例来源:origin: org.mockito/mockito-core

public static Object[] createObjectArray(Object array) {
    if (array instanceof Object[]) {
      return (Object[]) array;
    }
    Object[] result = new Object[Array.getLength(array)];
    for (int i = 0; i < Array.getLength(array); i++) {
      result[i] = Array.get(array, i);
    }
    return result;
  }
}

代码示例来源:origin: apache/incubator-dubbo

public static int getSize(Object object) {
  if (object == null) {
    return 0;
  }
  if (object instanceof Collection<?>) {
    return ((Collection<?>) object).size();
  } else if (object instanceof Map<?, ?>) {
    return ((Map<?, ?>) object).size();
  } else if (object.getClass().isArray()) {
    return Array.getLength(object);
  } else {
    return -1;
  }
}

代码示例来源:origin: apache/incubator-dubbo

public static int getSize(Object object) {
  if (object == null) {
    return 0;
  }
  if (object instanceof Collection<?>) {
    return ((Collection<?>) object).size();
  } else if (object instanceof Map<?, ?>) {
    return ((Map<?, ?>) object).size();
  } else if (object.getClass().isArray()) {
    return Array.getLength(object);
  } else {
    return -1;
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Checks if the specified array is neither null nor empty.
 *
 * @param array  the array to check
 * @throws IllegalArgumentException if {@code array} is either {@code null} or empty
 */
private static void validateArray(final Object array) {
  Validate.isTrue(array != null, "The Array must not be null");
  Validate.isTrue(Array.getLength(array) != 0, "Array cannot be empty.");
}

代码示例来源:origin: square/retrofit

@Override void apply(RequestBuilder builder, @Nullable Object values) throws IOException {
  if (values == null) return; // Skip null values.
  for (int i = 0, size = Array.getLength(values); i < size; i++) {
   //noinspection unchecked
   ParameterHandler.this.apply(builder, (T) Array.get(values, i));
  }
 }
};

代码示例来源:origin: spring-projects/spring-framework

private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType, Class<?> elementType)
    throws NoSuchMethodException {
  Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType());
  Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array));
  for (int i = 0; i < array.length; i++) {
    resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
  }
  return resultColl;
}

代码示例来源:origin: apache/incubator-druid

@Override
 public Object apply(@Nullable Object input)
 {
  if (input == null || Array.getLength(input) == 0) {
   return Collections.singletonList("null");
  }
  return Collections.singletonList(input);
 }
}

代码示例来源:origin: org.mockito/mockito-core

static boolean areArrayElementsEqual(Object o1, Object o2) {
  for (int i = 0; i < Array.getLength(o1); i++) {
    if (!areEqual(Array.get(o1, i), Array.get(o2, i))) return false;
  }
  return true;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check if the supplied value is the first entry in the array represented by the possibleArray value.
 * @param value the value to check for in the array
 * @param possibleArray an array object that may have the supplied value as the first element
 * @return true if the supplied value is the first entry in the array
 */
private static boolean isFirstEntryInArray(Object value, @Nullable Object possibleArray) {
  if (possibleArray == null) {
    return false;
  }
  Class<?> type = possibleArray.getClass();
  if (!type.isArray() || Array.getLength(possibleArray) == 0 ||
      !ClassUtils.isAssignableValue(type.getComponentType(), value)) {
    return false;
  }
  Object arrayValue = Array.get(possibleArray, 0);
  return (type.getComponentType().isPrimitive() ? arrayValue.equals(value) : arrayValue == value);
}

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public String toString(Object value) {
  List<String> elements = new ArrayList<String>(Array.getLength(value));
  for (int index = 0; index < Array.getLength(value); index++) {
    elements.add(toString(value, index));
  }
  return RenderingDispatcher.CURRENT.toSourceString(elements);
}

代码示例来源:origin: spring-projects/spring-framework

private Object growArrayIfNecessary(Object array, int index, String name) {
  if (!isAutoGrowNestedPaths()) {
    return array;
  }
  int length = Array.getLength(array);
  if (index >= length && index < this.autoGrowCollectionLimit) {
    Class<?> componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, index + 1);
    System.arraycopy(array, 0, newArray, 0, length);
    for (int i = length; i < Array.getLength(newArray); i++) {
      Array.set(newArray, i, newValue(componentType, null, name));
    }
    setPropertyValue(name, newArray);
    Object defaultValue = getPropertyValue(name);
    Assert.state(defaultValue != null, "Default value must not be null");
    return defaultValue;
  }
  else {
    return array;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return null;
  }
  if (sourceType.isAssignableTo(targetType)) {
    return source;
  }
  if (Array.getLength(source) == 0) {
    return null;
  }
  Object firstElement = Array.get(source, 0);
  return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
}

代码示例来源:origin: junit-team/junit4

private void addArrayValues(ParameterSignature sig, String name, List<PotentialAssignment> list, Object array) {
  for (int i = 0; i < Array.getLength(array); i++) {
    Object value = Array.get(array, i);
    if (sig.canAcceptValue(value)) {
      list.add(PotentialAssignment.forValue(name + "[" + i + "]", value));
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return Optional.empty();
  }
  else if (source instanceof Optional) {
    return source;
  }
  else if (targetType.getResolvableType().hasGenerics()) {
    Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType));
    if (target == null || (target.getClass().isArray() && Array.getLength(target) == 0) ||
          (target instanceof Collection && ((Collection<?>) target).isEmpty())) {
      return Optional.empty();
    }
    return Optional.of(target);
  }
  else {
    return Optional.of(source);
  }
}

代码示例来源:origin: junit-team/junit4

private Object getToStringableArrayElement(Object array, int length, int index) {
  if (index < length) {
    Object element = Array.get(array, index);
    if (isArray(element)) {
      return objectWithToString(componentTypeName(element.getClass()) + "[" + Array.getLength(element) + "]");
    } else {
      return element;
    }
  } else {
    return END_OF_ARRAY_SENTINEL;
  }
}

代码示例来源:origin: prestodb/presto

@Override
public T deserialize(JsonParser p, DeserializationContext ctxt, T existing) throws IOException
{
  T newValue = deserialize(p, ctxt);
  if (existing == null) {
    return newValue;
  }
  int len = Array.getLength(existing);
  if (len == 0) {
    return newValue;
  }
  return _concat(existing, newValue);
}

相关文章