@Override void visitClass(Class<?> t) { result.set(t.getComponentType()); } }.visit(type);
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; }
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; }
/** * 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()); }
@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; } },
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; }
/** * 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())); }
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()); } } }
@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() + "]."); } }
@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() + "]."); } }
private static boolean isPrimitives(Class<?> cls) { if (cls.isArray()) { return isPrimitive(cls.getComponentType()); } return isPrimitive(cls); }
@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); }
@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); }
private static boolean isPrimitives(Class<?> cls) { if (cls.isArray()) { return isPrimitive(cls.getComponentType()); } return isPrimitive(cls); }
public static boolean isPrimitives(Class<?> cls) { if (cls.isArray()) { return isPrimitive(cls.getComponentType()); } return isPrimitive(cls); }
public static boolean isPrimitives(Class<?> cls) { if (cls.isArray()) { return isPrimitive(cls.getComponentType()); } return isPrimitive(cls); }
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; }
@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); } } }
@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())); }
@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]); }