Example usage for java.lang Class getComponentType

List of usage examples for java.lang Class getComponentType

Introduction

In this page you can find the example usage for java.lang Class getComponentType.

Prototype

public Class<?> getComponentType() 

Source Link

Document

Returns the Class representing the component type of an array.

Usage

From source file:SystemUtils.java

public static Object[] CombineArrays(Object[] arr1, Object[] arr2) {
    Object[] combinedArr = null;//from  ww w. jav a 2 s. c  om
    Class arrClass;
    int arr1Len, arr2Len, totalLen;

    if (arr1 != null || arr2 != null) {
        if (arr1 != null) {
            arrClass = arr1.getClass();
        } else {
            arrClass = arr2.getClass();
        }

        arr1Len = (arr1 != null ? arr1.length : 0);
        arr2Len = (arr2 != null ? arr2.length : 0);

        totalLen = arr1Len + arr2Len;

        combinedArr = (Object[]) Array.newInstance(arrClass.getComponentType(), totalLen);

        if (arr1Len > 0) {
            System.arraycopy(arr1, 0, combinedArr, 0, arr1Len);
        }

        if (arr2Len > 0) {
            System.arraycopy(arr2, 0, combinedArr, arr1Len, arr2Len);
        }
    }

    return combinedArr;
}

From source file:org.apache.axis2.jaxws.runtime.description.marshal.impl.PackageSetBuilder.java

/**
 * Return the package associated with the class name.  The className may not be specified (in
 * which case a null Package is returned). if class has unnamed package return ""
 *
 * @param cls Class/*from  w w w  . j  av  a 2s.c o m*/
 * @return String or null 
 */
public static String getPackageFromClass(Class cls) {
    String pkgName = null;
    if (cls == null) {
        pkgName = null;
    } else if (cls.isArray()) {
        pkgName = getPackageFromClass(cls.getComponentType());
    } else if (cls.isPrimitive()) {
        pkgName = null;
    } else {
        pkgName = (cls.getPackage() == null) ? "" : cls.getPackage().getName();
    }
    return pkgName;
}

From source file:info.novatec.testit.livingdoc.converter.ArrayConverter.java

@Override
public boolean canConvertTo(Class<?> type) {
    return isArray(type) && TypeConversion.supports(type.getComponentType())
            && !type.getComponentType().isPrimitive();
}

From source file:com.silverwrist.dynamo.app.ApplicationContainer.java

private static final Class getUltimateComponent(Class klass) {
    while (klass.isArray())
        klass = klass.getComponentType();
    return klass;

}

From source file:jef.tools.ArrayUtils.java

/**
 * ?//from  ww  w .j av  a2 s.c om
 * 
 * @param obj
 * @param containerType
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T[] toObject(Object obj, Class<T> containerType) {
    Class<?> c = obj.getClass();
    Assert.isTrue(c.isArray());
    Class<?> priType = c.getComponentType();
    if (priType == containerType) {
        return (T[]) obj;
    }
    return cast(toObject(obj), containerType);
}

From source file:jef.tools.ArrayUtils.java

/**
 * ??/*  ww  w  . j  av a  2  s  .  c  om*/
 * 
 * @param objs
 * @return
 */
public static Object toPrimitive(Object[] obj) {
    Class<?> c = obj.getClass();
    Assert.isTrue(c.isArray());
    Class<?> objType = c.getComponentType();
    if (objType == Boolean.class) {
        return toPrimitive((Boolean[]) obj);
    } else if (objType == Byte.class) {
        return toPrimitive((Byte[]) obj);
    } else if (objType == Character.class) {
        return toPrimitive((Character[]) obj);
    } else if (objType == Integer.class) {
        return toPrimitive((Integer[]) obj);
    } else if (objType == Long.class) {
        return toPrimitive((Long[]) obj);
    } else if (objType == Float.class) {
        return toPrimitive((Float[]) obj);
    } else if (objType == Double.class) {
        return toPrimitive((Double[]) obj);
    } else if (objType == Short.class) {
        return toPrimitive((Short[]) obj);
    } else {
        throw new IllegalArgumentException();
    }
}

From source file:jef.tools.ArrayUtils.java

/**
 * ?//  w  ww.ja  va 2 s  . com
 * 
 * @param obj
 * @return
 */
public static Object[] toObject(Object obj) {
    Class<?> c = obj.getClass();
    Assert.isTrue(c.isArray());
    Class<?> priType = c.getComponentType();
    if (!priType.isPrimitive())
        return (Object[]) obj;
    if (priType == Boolean.TYPE) {
        return toObject((boolean[]) obj);
    } else if (priType == Byte.TYPE) {
        return toObject((byte[]) obj);
    } else if (priType == Character.TYPE) {
        return toObject((char[]) obj);
    } else if (priType == Integer.TYPE) {
        return toObject((int[]) obj);
    } else if (priType == Long.TYPE) {
        return toObject((long[]) obj);
    } else if (priType == Float.TYPE) {
        return toObject((float[]) obj);
    } else if (priType == Double.TYPE) {
        return toObject((double[]) obj);
    } else if (priType == Short.TYPE) {
        return toObject((short[]) obj);
    }
    throw new IllegalArgumentException();
}

From source file:org.eclipse.wb.internal.swing.databinding.model.generic.GenericUtils.java

private static IGenericType getObjectType(ClassLoader classLoader, ITypeBinding binding) throws Exception {
    Class<?> rawType = ReflectionUtils.getClassByName(classLoader,
            AstNodeUtils.getFullyQualifiedName(binding, true));
    String typeName = null;/*  w  ww. ja  v  a2s.  c  o  m*/
    if (binding instanceof DesignerTypeBinding) {
        // none
    } else if (binding.isWildcardType() && binding.getBound() == null) {
        return ClassGenericType.WILDCARD;
    } else {
        typeName = resolveTypeName(binding);
        if (binding.isParameterizedType()) {
            GenericTypeContainer genericType = new GenericTypeContainer(rawType);
            for (ITypeBinding subBinding : binding.getTypeArguments()) {
                genericType.getSubTypes().add(getObjectType(classLoader, subBinding));
            }
            return genericType;
        }
        if (binding.isArray() && rawType.isArray() && !rawType.getComponentType().isPrimitive()) {
            GenericTypeContainer genericType = new GenericTypeContainer(rawType, binding.getDimensions());
            genericType.getSubTypes().add(getObjectType(classLoader, binding.getElementType()));
            return genericType;
        }
    }
    return new ClassGenericType(rawType, typeName, null);
}

From source file:org.code_house.service.jolokia.Jolokia.java

private Object map(Class<?> type, Object value) {
    if (type.isArray()) {
        JSONArray array = (JSONArray) value;
        Object[] javaArray = (Object[]) Array.newInstance(type.getComponentType(), array.size());
        int index = 0;
        for (Object o : array) {
            Array.set(javaArray, index++, map(type.getComponentType(), o));
        }/*from   w w w  .j a va 2 s .  co m*/
        return javaArray;
    } else if (String.class.equals(type)) {
        return (String) value;
    }
    return null;
}

From source file:springfox.documentation.swagger.readers.parameter.ParameterMultiplesReader.java

@Override
public void apply(ParameterContext context) {

    MethodParameter methodParameter = context.methodParameter();
    ApiParam apiParam = methodParameter.getParameterAnnotation(ApiParam.class);

    Boolean allowMultiple;/*from www  .j  av  a  2 s .  co  m*/
    Class<?> parameterType = methodParameter.getParameterType();
    if (null != apiParam && !(parameterType != null && parameterType.isArray()
            && parameterType.getComponentType().isEnum())) {
        allowMultiple = apiParam.allowMultiple();
        context.parameterBuilder().allowMultiple(allowMultiple);
    }
}