java.lang.Class.getComponentType()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(183)

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

Class.getComponentType介绍

[英]Returns a Class object which represents the component type if this class represents an array type. Returns null if this class does not represent an array type. The component type of an array type is the type of the elements of the array.
[中]

代码示例

代码示例来源:origin: google/guava

@Override
 void visitClass(Class<?> t) {
  result.set(t.getComponentType());
 }
}.visit(type);

代码示例来源:origin: stackoverflow.com

public <T> T[] concatenate (T[] a, T[] b) {
  int aLen = a.length;
  int bLen = b.length;

  @SuppressWarnings("unchecked")
  T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen+bLen);
  System.arraycopy(a, 0, c, 0, aLen);
  System.arraycopy(b, 0, c, aLen, bLen);

  return c;
}

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

private Object convertDataArrayToTargetArray(Object[] array, Class<?> targetClass) throws NoSuchMethodException {
  Class<?> targetType = targetClass.getComponentType();
  Method fromMethod = targetType.getMethod("from", array.getClass().getComponentType());
  Object resultArray = Array.newInstance(targetType, array.length);
  for (int i = 0; i < array.length; i++) {
    Array.set(resultArray, i, ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
  }
  return resultArray;
}

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

/**
 * Check if the given class represents an array of primitives,
 * i.e. boolean, byte, char, short, int, long, float, or double.
 * @param clazz the class to check
 * @return whether the given class is a primitive array class
 */
public static boolean isPrimitiveArray(Class<?> clazz) {
  Assert.notNull(clazz, "Class must not be null");
  return (clazz.isArray() && clazz.getComponentType().isPrimitive());
}

代码示例来源:origin: google/guava

@Override
 Type usedInGenericType(Type type) {
  checkNotNull(type);
  if (type instanceof Class) {
   Class<?> cls = (Class<?>) type;
   if (cls.isArray()) {
    return new GenericArrayTypeImpl(cls.getComponentType());
   }
  }
  return type;
 }
},

代码示例来源: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: spring-projects/spring-framework

/**
 * Check if the given class represents an array of primitive wrappers,
 * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
 * @param clazz the class to check
 * @return whether the given class is a primitive wrapper array class
 */
public static boolean isPrimitiveWrapperArray(Class<?> clazz) {
  Assert.notNull(clazz, "Class must not be null");
  return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType()));
}

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

private void appendType(StringBuilder sb, Class<?> type, boolean useLongTypeName) {
    if (type.isArray()) {
      appendType(sb, type.getComponentType(), useLongTypeName);
      sb.append("[]");
    }
    else {
      sb.append(useLongTypeName ? type.getName() : type.getSimpleName());
    }
  }
}

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

@Override
public void arrayBegin() throws ParseException {
  mStack.push(mType);
  if (mType.isArray()) {
    mType = mType.getComponentType();
  } else if (mType == Object.class || Collection.class.isAssignableFrom(mType)) {
    mType = Object.class;
  } else {
    throw new ParseException("Convert error, can not load json array data into class [" + mType.getName() + "].");
  }
}

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

@Override
public void arrayBegin() throws ParseException {
  mStack.push(mType);
  if (mType.isArray()) {
    mType = mType.getComponentType();
  } else if (mType == Object.class || Collection.class.isAssignableFrom(mType)) {
    mType = Object.class;
  } else {
    throw new ParseException("Convert error, can not load json array data into class [" + mType.getName() + "].");
  }
}

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

private static boolean isPrimitives(Class<?> cls) {
  if (cls.isArray()) {
    return isPrimitive(cls.getComponentType());
  }
  return isPrimitive(cls);
}

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

@Override
public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
  // Process the component type of an array.
  Class<?> componentType = clazz.getComponentType();
  TypeDefinitionBuilder.build(componentType, componentType, typeCache);
  final String canonicalName = clazz.getCanonicalName();
  return new TypeDefinition(canonicalName);
}

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

@Override
public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
  // Process the component type of an array.
  Class<?> componentType = clazz.getComponentType();
  TypeDefinitionBuilder.build(componentType, componentType, typeCache);
  final String canonicalName = clazz.getCanonicalName();
  return new TypeDefinition(canonicalName);
}

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

private static boolean isPrimitives(Class<?> cls) {
  if (cls.isArray()) {
    return isPrimitive(cls.getComponentType());
  }
  return isPrimitive(cls);
}

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

public static boolean isPrimitives(Class<?> cls) {
  if (cls.isArray()) {
    return isPrimitive(cls.getComponentType());
  }
  return isPrimitive(cls);
}

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

public static boolean isPrimitives(Class<?> cls) {
  if (cls.isArray()) {
    return isPrimitive(cls.getComponentType());
  }
  return isPrimitive(cls);
}

代码示例来源:origin: google/guava

private TypeToken<? extends T> getArraySubtype(Class<?> subclass) {
 // array is covariant. component type is subtype, so is the array type.
 TypeToken<?> componentSubtype = getComponentType().getSubtype(subclass.getComponentType());
 @SuppressWarnings("unchecked") // component type is subtype, so is array type.
 TypeToken<? extends T> result =
   (TypeToken<? extends T>)
     // If we are passed with int[].class, don't turn it to GenericArrayType
     of(newArrayClassOrGenericArrayType(componentSubtype.runtimeType));
 return result;
}

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

@Test
public void testBeanPropertyIsArray() {
  PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class);
  for (PropertyDescriptor descriptor : descriptors) {
    if ("containedBeans".equals(descriptor.getName())) {
      assertTrue("Property should be an array", descriptor.getPropertyType().isArray());
      assertEquals(descriptor.getPropertyType().getComponentType(), ContainedBean.class);
    }
  }
}

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

@Test
public void getComponentTypeForClassArray() throws Exception {
  Field field = Fields.class.getField("arrayClassType");
  ResolvableType type = ResolvableType.forField(field);
  assertThat(type.isArray(), equalTo(true));
  assertThat(type.getComponentType().getType(),
      equalTo((Type) ((Class) field.getGenericType()).getComponentType()));
}

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

@Test
public void testSetupArguments() {
  Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
      new Class<?>[] {String[].class}, "a", "b", "c");
  assertEquals(1, newArray.length);
  Object firstParam = newArray[0];
  assertEquals(String.class,firstParam.getClass().getComponentType());
  Object[] firstParamArray = (Object[]) firstParam;
  assertEquals(3,firstParamArray.length);
  assertEquals("a",firstParamArray[0]);
  assertEquals("b",firstParamArray[1]);
  assertEquals("c",firstParamArray[2]);
}

相关文章

Class类方法